从后面的代码访问母版页中的Web用户控件 [英] accessing a web user control in master page from code behind

查看:51
本文介绍了从后面的代码访问母版页中的Web用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的母版页中有一个名为StudentsPanel的Web用户控件。我把它放在登录视图下面,如下所示。我需要从我页面后面的代码访问它,以便我可以禁用它。已经使用过下面的代码,但我最终得到null结果。任何人都可以告诉我如何访问我的母版页中的Web用户控件?谢谢,下面是我将控件放在母版页中的地方



Hi,i have a web user control called StudentsPanel in my master page.I have placed it under the login view as shown below.I need to access it from code behind in my page so as i can disable it.Have used the code below but i end up getting null results.Can anyone tell me how to access the web user control in my master page?Thanks,Below is where i have placed the control in master page

<asp:LoginView ID="LoginView1" runat="server">
                  <RoleGroups>
                <asp:RoleGroup Roles="Students">
                    <ContentTemplate>
                     <uc1:studentPanel runat="server" ID="students_control" />
                    </ContentTemplate>
                     </asp:RoleGroup>

                </asp:RoleGroup>
            </RoleGroups>
        </asp:LoginView>







这是我在页面中使用的代码




Here is the code am using in my page

LoginView control;

            Page page = (Page)this.Page;
            MasterPage master = (MasterPage)page.Master;

          control= (LoginView)master.FindControl("students_control");[/B]

                       if (control!= null)
                       {
                           Label1.Text = "found";
                       }

推荐答案

您需要将其强制转换为 studentPanel

you need to cast it to type studentPanel
control= (studentPanel)master.FindControl("students_control");



检查。


Check it.


您好,



我认为你没有在母版页中注册用户控件,



Hello,

I think you didnt register you user control in the master page,

<%@ Register TagPrefix="UserControl" TagName="YourUsercontrol" src="~/YourUsercontrol.ascx" %>





一切顺利:)



All the best:)


尝试使用FormCollection类来访问控件。就像这样

Try using FormCollection class to access the controls.Like this
[HttpPost]
       public ActionResult Login(LoginModel model, FormCollection collection)
       {
           string userName = collection["txtUsername"];
           string password = collection["txtPassword"];

}



这可能会有所帮助。


This may help.


这篇关于从后面的代码访问母版页中的Web用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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