C# 如何从代码隐藏中隐藏和显示asp.net中的asp:按钮?

C# 如何从代码隐藏中隐藏和显示asp.net中的asp:按钮?,c#,javascript,asp.net,updatepanel,postback,C#,Javascript,Asp.net,Updatepanel,Postback,我正在开发asp.net web应用程序。 在一个页面中,我有两个asp按钮。 我想在一个条件下显示它们,否则我不想显示它们。 所以我也试着这样做。但它不起作用。 我找不到背后的原因。请告诉我问题出在哪里 隐藏按钮 if (!IsPostBack) { ButtonReplaceId.Style.Add("display", "none"); ButtonAssociateRules.Style.Add("d

我正在开发asp.net web应用程序。 在一个页面中,我有两个asp按钮。 我想在一个条件下显示它们,否则我不想显示它们。 所以我也试着这样做。但它不起作用。 我找不到背后的原因。请告诉我问题出在哪里

隐藏按钮

if (!IsPostBack)
            {
                ButtonReplaceId.Style.Add("display", "none");
                ButtonAssociateRules.Style.Add("display", "none");
            }
显示按钮

protected void ApplyAssociation(object sender, EventArgs e)
{
//Some piece of code
if(a==0)
{
ButtonAssociateRules.Style.Add("display", "block");
ButtonReplaceId.Style.Add("display", "block");
}

}
按钮的aspx

    <div style ="padding-left:400px;">
        <asp:Button ID="ButtonAssociateRules" runat="server" OnClick="AssociateMultipleRulesButtonClick"
            CssClass="search_button_in_vm_intersection" Text="Associate Multiple Rules" 
            OnClientClick="return OnClientClickAssociateRewardRuleFile();" />

        <asp:Button ID="ButtonReplaceId" runat="server" OnClick="ApplyReplaceIfRuleIntersects"
            CssClass="search_button_in_vm_intersection" Text="Replace Previous Rules" 
            OnClientClick="return OnClientClickReplaceRewardRuleFile();" />

    </div>

OnClick事件ApplyAssociation()按钮的aspx


您可以简单地使用
按钮的属性
,它更直接、更干净

ButtonReplaceId.Visible = false;
如果此属性为false,则不会呈现服务器控件。你 在组织页面布局时应考虑到这一点。 如果未呈现容器控件,则它包含的所有控件 即使设置了 个人控制为真。在这种情况下,单个控件 即使已显式设置 这是真的。(即,如果父控件的Visible属性为 如果设置为false,则子控件将继承该设置和设置 优先于任何本地设置。)


您正试图更改当前UpdatePanel中不存在的ajax调用中的控制状态。将按钮放在同一个UpdatePanel中,您就可以更改状态。

由于您使用的是条件更新面板,您可以在将按钮放在更新面板中后尝试这两种方法之一

ButtonReplaceId.Visible = false;
ButtonAssociateRules.Visible = false;
    protected void ApplyAssociation(object sender, EventArgs e)
    {
        //Some piece of code
        if (a == 0)
        {
            ButtonAssociateRules.Style["visibility"] = "hidden";
            ButtonReplaceId.Style["visibility"] = "hidden";
            myUpdatePanel.Update();
        }
    }
    protected void ApplyAssociation(object sender, EventArgs e)
    {
        //Some piece of code
        if (a == 0)
        {
            ButtonAssociateRules.Visible = false;
            ButtonReplaceId.Visible = false;
            myUpdatePanel.Update();
        }
    }
<asp:UpdatePanel ID="myUpdatePanel" runat="server" UpdateMode="Conditional">
     <ContentTemplate>
          <div style="padding-left:400px;">
               <asp:Button ID="ButtonAssociateRules" runat="server" OnClick="AssociateMultipleRulesButtonClick"
                    CssClass="search_button_in_vm_intersection" Text="Associate Multiple Rules" 
                    OnClientClick="return OnClientClickAssociateRewardRuleFile();" />
               <asp:Button ID="ButtonReplaceId" runat="server" OnClick="ApplyReplaceIfRuleIntersects"
                    CssClass="search_button_in_vm_intersection" Text="Replace Previous Rules" 
                    OnClientClick="return OnClientClickReplaceRewardRuleFile();" />
          </div>
     </ContentTemplate>
</asp:UpdatePanel>
下面是更新面板中按钮的示例

    protected void ApplyAssociation(object sender, EventArgs e)
    {
        //Some piece of code
        if (a == 0)
        {
            ButtonAssociateRules.Style["visibility"] = "hidden";
            ButtonReplaceId.Style["visibility"] = "hidden";
            myUpdatePanel.Update();
        }
    }
    protected void ApplyAssociation(object sender, EventArgs e)
    {
        //Some piece of code
        if (a == 0)
        {
            ButtonAssociateRules.Visible = false;
            ButtonReplaceId.Visible = false;
            myUpdatePanel.Update();
        }
    }
<asp:UpdatePanel ID="myUpdatePanel" runat="server" UpdateMode="Conditional">
     <ContentTemplate>
          <div style="padding-left:400px;">
               <asp:Button ID="ButtonAssociateRules" runat="server" OnClick="AssociateMultipleRulesButtonClick"
                    CssClass="search_button_in_vm_intersection" Text="Associate Multiple Rules" 
                    OnClientClick="return OnClientClickAssociateRewardRuleFile();" />
               <asp:Button ID="ButtonReplaceId" runat="server" OnClick="ApplyReplaceIfRuleIntersects"
                    CssClass="search_button_in_vm_intersection" Text="Replace Previous Rules" 
                    OnClientClick="return OnClientClickReplaceRewardRuleFile();" />
          </div>
     </ContentTemplate>
</asp:UpdatePanel>


也尝试过:buttonAsociateRules.Visible=false;ButtonReplaceId.Visible=false;您应该首先调试实际执行的代码的哪一部分,因为您的解决方案以及提供的解决方案(哪一个更好)也应该试过调试,但无法解决它。这就是我在这里发布的原因。已尝试此按钮替换ID.Visible=false;不工作您试图更改当前UpdatePanel中不存在的ajax调用中的控制状态。将按钮放在同一个UpdatePanel中,然后您将能够更改状态。已尝试此按钮替换ID。Visible=false;不起作用