最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to hide and display asp:buttons in asp.net from code behind? - Stack Overflow

programmeradmin1浏览0评论

I am working on asp web application. In one Page I have two asp buttons. I want to display them in one condition otherwise I don't want to display them. So I'm trying to do the same like this. But Its not working. I can't find the reason behind it. Please tell me where is the issue.

To Hide Buttons

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

To display buttons

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

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

aspx of button for OnClick event 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>

I am working on asp.net web application. In one Page I have two asp buttons. I want to display them in one condition otherwise I don't want to display them. So I'm trying to do the same like this. But Its not working. I can't find the reason behind it. Please tell me where is the issue.

To Hide Buttons

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

To display buttons

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

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

aspx of button for OnClick event 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>
Share Improve this question edited Feb 28, 2014 at 12:04 Sudhakar Tillapudi 26.2k5 gold badges42 silver badges69 bronze badges asked Feb 28, 2014 at 12:04 user3264676user3264676 2536 gold badges8 silver badges20 bronze badges 3
  • tried this also: ButtonAssociateRules.Visible = false; ButtonReplaceId.Visible = false; – user3264676 Commented Feb 28, 2014 at 12:06
  • You should first debug which part of code is actually executed cause your solution aswell as the ones provided (which are better) should work – Laurent S. Commented Feb 28, 2014 at 12:08
  • tried debugging too, could not solved it. that is why I have posted here. – user3264676 Commented Feb 28, 2014 at 12:16
Add a comment  | 

3 Answers 3

Reset to default 5

Seeing as you are using a conditional update panel, you can try either of these after putting the buttons inside an update panel.

    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();
        }
    }

Here's an example of your buttons inside an update panel.

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

You can simple use the Visible property of Button which is more straight forward and clean.

ButtonReplaceId.Visible = false;

If this property is false, the server control is not rendered. You should take this into account when organizing the layout of your page. If a container control is not rendered, any controls that it contains will not be rendered even if you set the Visible property of an individual control to true. In that case, the individual control returns false for the Visible property even if you have explicitly set it to true. (That is, if the Visible property of the parent control is set to false, the child control inherits that setting and the setting takes precedence over any local setting.) MSDN.

You are trying to change the state of control in ajax call that is not in current UpdatePanel. Put the buttons in the same UpdatePanel then you will be able to change the state.

ButtonReplaceId.Visible = false;
ButtonAssociateRules.Visible = false;
发布评论

评论列表(0)

  1. 暂无评论