如何隐藏和显示的asp:按钮从code asp.net的背后? [英] How to hide and display asp:buttons in asp.net from code behind?

查看:185
本文介绍了如何隐藏和显示的asp:按钮从code asp.net的背后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的asp.net web应用程序。
在一个页面中,我有两个ASP按钮。
我想在一个条件,以显示他们,否则我不想显示它们。
所以我试图做同样的这个样子。但它不工作。
我无法找到它背后的原因。请告诉我在哪里的问题。

要隐藏按钮

 如果(!的IsPostBack)
            {
                ButtonReplaceId.Style.Add(显示,无);
                ButtonAssociateRules.Style.Add(显示,无);
            }

要显示的按钮

 保护无效ApplyAssociation(对象发件人,EventArgs的发送)
{
//有些片code的
如果(A == 0)
{
ButtonAssociateRules.Style.Add(显示,块);
ButtonReplaceId.Style.Add(显示,块);
}}

对于ASPX按钮

 < D​​IV的风格=填充左:400像素;>
        < ASP:按钮的ID =ButtonAssociateRules=服务器的OnClick =AssociateMultipleRulesButtonClick
            的CssClass =search_button_in_vm_intersection文本=关联多个规则
            的OnClientClick =返回OnClientClickAssociateRewardRuleFile(); />        < ASP:按钮的ID =ButtonReplaceId=服务器的OnClick =ApplyReplaceIfRuleIntersects
            的CssClass =search_button_in_vm_intersection文本=替换previous规则
            的OnClientClick =返回OnClientClickReplaceRewardRuleFile(); />    < / DIV>

按钮的ASPX的OnClick事件ApplyAssociation()

 < ASP:的UpdatePanel =服务器的UpdateMode =条件>
                <&触发器GT;
                    < ASP:AsyncPostBackTrigger控件ID =Button1的事件名称=点击/>
                < /触发器>
                <&的ContentTemplate GT;
                < ASP:表=服务器的CssClass =rule_file_whole边框宽度=0样式=填充顶:30PX;>
                < ASP:的TableRow ID =MerchantRowAssociationHorizo​​ntalAlign =中心>
                            < ASP:TableCell的>
                                < D​​IV的风格=文本对齐:中心>
                                    < ASP:按钮的ID =AssociationMerchant文本=应用协会=服务器的OnClick =ApplyAssociation
                                        的CssClass =search_button_in_vm_associate1的OnClientClick =返回checkValidation()/>
                                < / DIV>
                            < / ASP:TableCell的>
                        < / ASP:&的TableRow GT;
                                            < / ASP:表>
                < /&的ContentTemplate GT;
            < / ASP:的UpdatePanel>


解决方案

看到你使用的是有条件的更新面板,你可以把一个更新面板内的按钮后,无论是尝试这些。

 保护无效ApplyAssociation(对象发件人,EventArgs的发送)
    {
        //有些片code的
        如果(A == 0)
        {
            ButtonAssociateRules.Style [知名度] =隐藏;
            ButtonReplaceId.Style [知名度] =隐藏;
            myUpdatePanel.Update();
        }
    }
    保护无效ApplyAssociation(对象发件人,EventArgs的发送)
    {
        //有些片code的
        如果(A == 0)
        {
            ButtonAssociateRules.Visible = FALSE;
            ButtonReplaceId.Visible = FALSE;
            myUpdatePanel.Update();
        }
    }

下面是一个更新面板内的按钮的例子。

 < ASP:的UpdatePanel ID =myUpdatePanel=服务器的UpdateMode =条件>
     <&的ContentTemplate GT;
          < D​​IV的风格=填充左:400像素;>
               < ASP:按钮的ID =ButtonAssociateRules=服务器的OnClick =AssociateMultipleRulesButtonClick
                    的CssClass =search_button_in_vm_intersection文本=关联多个规则
                    的OnClientClick =返回OnClientClickAssociateRewardRuleFile(); />
               < ASP:按钮的ID =ButtonReplaceId=服务器的OnClick =ApplyReplaceIfRuleIntersects
                    的CssClass =search_button_in_vm_intersection文本=替换previous规则
                    的OnClientClick =返回OnClientClickReplaceRewardRuleFile(); />
          < / DIV>
     < /&的ContentTemplate GT;
< / 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>

解决方案

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>

这篇关于如何隐藏和显示的asp:按钮从code asp.net的背后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆