Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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

XML
<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
C#
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";
                       }
Posted

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

Check it.
 
Share this answer
 
Comments
Member 10398773 27-Jan-14 1:54am    
Hi,this has returned to me null
Karthik_Mahalingam 27-Jan-14 2:03am    
try this
LoginView lv = (LoginView)master.FindControl("LoginView1");
var some = (studentPanel)lv.FindControl("students_control") ;
Member 10398773 28-Jan-14 8:21am    
Sir,i salute you really.Your code has worked like a charm.Wow!i spend nites thinking about this.The part i can ask you is whether i can add some items on it depending on different user.
Karthik_Mahalingam 28-Jan-14 8:36am    
if it helps pls mark it as answer..
yes you can add.
Member 10398773 28-Jan-14 8:44am    
cool.i will try that now.Saw it by using loadcontrol method
Hello,

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

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


All the best:)
 
Share this answer
 
v2
Comments
Member 10398773 27-Jan-14 1:49am    
Its already registered really
Member 10398773 28-Jan-14 7:57am    
Hi,this has not solved it
karthik Udhayakumar 28-Jan-14 8:32am    
hi,I guess you got the solution now!
Member 10398773 28-Jan-14 8:53am    
Yes really.Thank you much.I have used KARTHIK code.Thanks
Try using FormCollection class to access the controls.Like this
C#
[HttpPost]
       public ActionResult Login(LoginModel model, FormCollection collection)
       {
           string userName = collection["txtUsername"];
           string password = collection["txtPassword"];

}

This may help.
 
Share this answer
 
Comments
NF Khan 23-Aug-14 13:14pm    
Similarly i have a user control in which i have DropDown, I am trying to accessing the DropDown items in Master page code behind but i am not able to get it, Below is my code use

Site.Master
<%@ Register TagPrefix="uc" TagName="MyDDL" Src="~/MyUC.ascx" %>
<uc:MyDDL runat="server" ID="SelectDDL" />

Site.Master.cs
DropDownList thisDropDown = this.Master.FindControl("SelectDDL") as DropDownList;

Please help how can i achieve this. I am getting NullException Error.

Thanks in advance
Member 10398773 25-Aug-14 2:52am    
I will try and update if i succeed on that

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900