Referencing user control in Place holder?
-
Hi In the page load of main.aspx, I am adding a user control in a place holder at runtime. As follow: ph.Controls.Add(CType(Me.LoadControl("/lpages/Login.ascx"), lpages.Login)) Here 'lpages' is the name of project. 'login.ascx' is the user control. 'ph' is the place holder. In the user control, I have a public variable for example "strName". I want to access this variable from main.aspx after user control's addition. How can I refer strName from main.aspx code-behind? Please advice Thanks Pankaj Follow your goals, Means will follow you ---Gandhi---
-
Hi In the page load of main.aspx, I am adding a user control in a place holder at runtime. As follow: ph.Controls.Add(CType(Me.LoadControl("/lpages/Login.ascx"), lpages.Login)) Here 'lpages' is the name of project. 'login.ascx' is the user control. 'ph' is the place holder. In the user control, I have a public variable for example "strName". I want to access this variable from main.aspx after user control's addition. How can I refer strName from main.aspx code-behind? Please advice Thanks Pankaj Follow your goals, Means will follow you ---Gandhi---
First, give the control some name:
c = Me.LoadControl("/lpages/Login.ascx");
c.ID = "login";
ph.Controls.Add(CType(c, lpages.Login))and then you need to find control
FindControl("login")
, cast it to thelpages.Login
type and access properties. -- Mariusz 'mAv' Wójcik master e-software engineer (BPC) -
Hi In the page load of main.aspx, I am adding a user control in a place holder at runtime. As follow: ph.Controls.Add(CType(Me.LoadControl("/lpages/Login.ascx"), lpages.Login)) Here 'lpages' is the name of project. 'login.ascx' is the user control. 'ph' is the place holder. In the user control, I have a public variable for example "strName". I want to access this variable from main.aspx after user control's addition. How can I refer strName from main.aspx code-behind? Please advice Thanks Pankaj Follow your goals, Means will follow you ---Gandhi---
[edit]Bah... sorry, I'm a bit redundant here. :doh: mavus74.net beat me to the post as I was writing this.[/edit] There are two ways that come to mind. First, you could save a reference to the control that you're loading and then add it to the placeholder. [You'll have to forgive my VB if the syntax is off, I'm more of a C# kinda guy.]
Dim myControl As lpages.Login = CType(Page.LoadControl("/lpages/Login.ascx"), lpages.Login) ph.Controls.Add(myControl) myControl.strName = "The Control"
You could also add your control to the placeholder, and then locate it positionally.
' Don't need the CType conversion when loading the control to add this way. ' It will be cast to System.Web.UI.Control when added, anyway. ph.Controls.Add(Page.LoadControl("/lpages/Login.ascx")) (CType(ph.Controls.Item(ph.Controls.Count - 1), lpages.Login)).strName = "The Control"
The first way is preferable, as it cuts down the casting involved if you want to reference your control more then a single time. Another construct to be aware of is the
FindControl
method ofSystem.Web.UI.Control
. It will allow you to find a control in the collection of a Control or a Page by the desired control's id. The reason that I didn't suggest it here is that you aren't specifying an id when you add your control to the placeholder. Hope that helps. :) --Jesse -
[edit]Bah... sorry, I'm a bit redundant here. :doh: mavus74.net beat me to the post as I was writing this.[/edit] There are two ways that come to mind. First, you could save a reference to the control that you're loading and then add it to the placeholder. [You'll have to forgive my VB if the syntax is off, I'm more of a C# kinda guy.]
Dim myControl As lpages.Login = CType(Page.LoadControl("/lpages/Login.ascx"), lpages.Login) ph.Controls.Add(myControl) myControl.strName = "The Control"
You could also add your control to the placeholder, and then locate it positionally.
' Don't need the CType conversion when loading the control to add this way. ' It will be cast to System.Web.UI.Control when added, anyway. ph.Controls.Add(Page.LoadControl("/lpages/Login.ascx")) (CType(ph.Controls.Item(ph.Controls.Count - 1), lpages.Login)).strName = "The Control"
The first way is preferable, as it cuts down the casting involved if you want to reference your control more then a single time. Another construct to be aware of is the
FindControl
method ofSystem.Web.UI.Control
. It will allow you to find a control in the collection of a Control or a Page by the desired control's id. The reason that I didn't suggest it here is that you aren't specifying an id when you add your control to the placeholder. Hope that helps. :) --JesseThanks for your suggestions.. After applying them I am getting following exceptions: System.NullReferenceException: Object reference not set to an instance of an object -------This is what I wrote in code:------------ Dim objLogin As lpages.Login objLogin = DirectCast(FindControl("/lpages/Login.ascx"), Login) objLogin.strName = "tomandjerry" ----------------- Moment line 3 is executed, I get NulReference exception. Am I missing something? Please advice. Thanks Pankaj Follow your goals, Means will follow you ---Gandhi---