如何从代码隐藏中隐藏和显示asp.net中的asp:buttons?

2024-03-31

我正在开发 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

<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>

由于您使用的是条件更新面板,因此您可以在将按钮放入更新面板后尝试其中任何一个。

    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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从代码隐藏中隐藏和显示asp.net中的asp:buttons? 的相关文章

随机推荐