从内容页面更改母版页面上按钮的可见性 [英] Changing visibility of a button on masterpage from a content page

查看:72
本文介绍了从内容页面更改母版页面上按钮的可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的母版页(MasterPage.master)上有一个按钮(btn_del),其可见性最初设置为false.我有一个内容页(inbox.aspx),在上面有一个gridview(gv_inbox).我在gv_inbox的标题上有一个复选框(CheckBox_selall),选中该行时选中所有复选框.另外,当我检查CheckBox_selall时,我需要在取消选中CheckBox_selall时将btn_del的可见性设置为true和false.
我已经尝试过一个代码,但是它并没有改变btn_del的可见性..我是c#.net的新手

在这方面请帮助我..提前谢谢..::)我的代码如下所示.

Hi,
I have a button (btn_del) on my masterpage (MasterPage.master) and whose visibility is initially set to false. I have a contentpage (inbox.aspx) on which I have a gridview (gv_inbox). I have a checkbox (CheckBox_selall) on the header of gv_inbox, to select all the checkboxes of the rows, when checked. Also when I check the CheckBox_selall, I need to set the visibility of btn_del to true and to false when I uncheck CheckBox_selall.
I have tried a code but it is not changing the visibility of btn_del.. I am new to c#.net

Please help me in this regard.. Thanx in advance..:) My code is shown below.

protected void CheckBox_selall_CheckedChanged(object sender, EventArgs e)
    {
        Button btn = this.Master.FindControl("btn_del") as Button;
               
         CheckBox chkAll = (CheckBox)gv_inbox.HeaderRow.FindControl("CheckBox_selall");
         if (chkAll.Checked == true)
         {             
             btn.Visible = true;
             foreach (GridViewRow gvRow in gv_inbox.Rows)
             {
                 CheckBox chkSel = (CheckBox)gvRow.FindControl("chkbx_sel");
                 chkSel.Checked = true;
             }
         }
         else
         {
             btn.Visible = false;
             foreach (GridViewRow gvRow in gv_inbox.Rows)
             {
                 CheckBox chkSel = (CheckBox)gvRow.FindControl("chkbx_sel");
                 chkSel.Checked = false;
             }
         }
    }

推荐答案

Button deletebutton=(Button)this.Page.Master.FindControl("btn_del")).Visible = true;



或更多防御方法



or more defensive approch

<pre> Button deletebutton=(Button)this.Page.Master.FindControl("btn_del"))
 if (deletebutton !=null)
{
deletebutton.Visible=false;
}



在运行时,母版页与内容页合并,但由于控件受到了保护,因此不能直接作为母版页成员访问这些控件.母版页上的服务器控件在母版页本地,内容页无法访问它们.为了使它们可访问,服务器控件需要公开为公共属性或方法.请检查下面的代码.

在您的母版页中,将按钮可见性设置为假"
Hi,
At run time, the master page is merged with the content page, but the controls are not directly accessible as master page members because they are protected. The server controls on a master page are local to the master page, which are not accessible by a content page. To make them accessible, the server controls need to be exposed as public properties or methods. Please check the code below.

In your master page make the button visibility to ''false''
<asp:Button ID="btnMaster" runat="server" Text="Show Me" Visible="false" />

        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>




在masterpage.cs页面中,创建公共属性以更改按钮的可见性.




In the masterpage.cs page, create public property to change the button''s visibility.

public bool VisibleValue
        {
            set { btnMaster.Visible = value; }
        }



现在,在inbox.aspx页中,选中已选中的已更改事件复选框,请调用masterpage属性并将其值设置为"true".这将在主页面中将buutton的可见性设置为"true".
Master属性的类型为System.Web.UI.Page,并且不包含派生类的属性.因此,您可以使用以下代码.



Now in the inbox.aspx page, on the checkbox checked changed event call the masterpage property and set the value to ''true''. This will set the buutton''s visibility to ''true'' in the master page.
Master property is of type System.Web.UI.Page, and that does not contain properties of your derived class. So you can use the below code.

protected void  
CheckBox_selall_CheckedChanged(object sender, EventArgs e)
        {
            MasterPage mymaster = Master as MasterPage;
            mymaster .VisibleValue= true;
        }



现在,当您运行代码时,您可以看到选中此复选框后该按钮变为可见.

快乐的编码..:)



Now, when you run the code you can see that the button becomes visible when the checkbox is checked.

Happy coding.. :)


使用下面的代码,我一边检查了它的工作原理,然后找到了控件,检查了它的工作原理,然后调试并确定是否它进入chkAll.Checked方法




Use the below code , i checked on my side , its working , i am able to find the control , check if its working , else debug and find out if its goes inside chkAll.Checked method




protected void CheckBox_selall_CheckedChanged(object sender, EventArgs e)
{
    Button btn = (Button)this.Page.Master.FindControl("btn_del");

     CheckBox chkAll = (CheckBox)gv_inbox.HeaderRow.FindControl("CheckBox_selall");
     if (chkAll.Checked)
     {
         btn.Visible = true;
         foreach (GridViewRow gvRow in gv_inbox.Rows)
         {
             CheckBox chkSel = (CheckBox)gvRow.FindControl("chkbx_sel");
             chkSel.Checked = true;
         }
     }
     else
     {
         btn.Visible = false;
         foreach (GridViewRow gvRow in gv_inbox.Rows)
         {
             CheckBox chkSel = (CheckBox)gvRow.FindControl("chkbx_sel");
             chkSel.Checked = false;
         }
     }
}


这篇关于从内容页面更改母版页面上按钮的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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