如何在代码隐藏的情况下隐藏和显示asp.net中的asp:按钮?

use*_*676 5 javascript c# asp.net postback updatepanel

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

隐藏按钮

if (!IsPostBack)
            {
                ButtonReplaceId.Style.Add("display", "none");
                ButtonAssociateRules.Style.Add("display", "none");
            }
Run Code Online (Sandbox Code Playgroud)

显示按钮

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

}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

用于OnClick事件的按钮的aspx ApplyAssociation()

<asp:UpdatePanel runat="server" UpdateMode="Conditional">
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
                </Triggers>
                <ContentTemplate>
                <asp:Table runat="server" CssClass="rule_file_whole" BorderWidth="0" Style="padding-top: 30px;">
                <asp:TableRow ID="MerchantRowAssociation" HorizontalAlign="Center">
                            <asp:TableCell>
                                <div style="text-align: center">
                                    <asp:Button ID="AssociationMerchant" Text="Apply Association" runat="server" OnClick="ApplyAssociation"
                                        CssClass="search_button_in_vm_associate1 " OnClientClick="return checkValidation()" />
                                </div>
                            </asp:TableCell>
                        </asp:TableRow>
                                            </asp:Table>
                </ContentTemplate>
            </asp:UpdatePanel>
Run Code Online (Sandbox Code Playgroud)

Mig*_*ade 5

看到您正在使用条件更新面板,您可以在将按钮放入更新面板后尝试其中任何一个.

    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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

以下是更新面板中按钮的示例.

<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>
Run Code Online (Sandbox Code Playgroud)