dynamically loading usercontrols
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngcontrolsvb' list.


darren
-- Copied from [aspngfreeforall] to [aspngcontrolsvb] by Mitch Denny <Click here to reveal e-mail address> --

morning all,

i'm trying to dynamically load a usercontrol onto a webform. the
usercontrol is essentially a search function and contains the code
required to display the form, process the search request and display the
results.

the problem is that when i try and dynamically load the control, the id of
the control is added to the id of the textbox on the form, which prevents
me from retrieving the text entered in the box.

i understand why it's doing this (i think!), but is there any way to stop
it?? or do i just have to hardcode the details into the code??

the code i'm using to load the control is:

dim myUserControl as myNamespace.myControl = LoadControl("path_to_myControl.ascx")
frmSearch.Controls.Add(myUserControl)
myUserControl.ID = "ctrlSrch"

if i leave the myUserControl.ID line out, then i just get a control id
added to the form fields.

thanks for any help

darren.

Reply to this message...
 
    
Jeff Widmer
Darren,
Can you post some of the code to your usercontrol?

But I think your problem is that your trying to access the textbox of the
usercontrol from the aspx page that contains the user control. Instead,
just access the text box from within the usercontrol (in other words, do the
work of the usercontrol from within the user control).

Or create a public property of the Usercontrol that exposes the contents of
the textbox. The let the aspx page query this property.

Anyway, this is just guessing. Post some of you code and we should be able
to help you out.
-Jeff

-----Original Message-----
From: darren [mailto:Click here to reveal e-mail address]
Sent: Monday, February 18, 2002 5:20 AM
To: aspngcontrolsvb
Subject: [aspngcontrolsvb] dynamically loading usercontrols

-- Copied from [aspngfreeforall] to [aspngcontrolsvb] by Mitch Denny
<Click here to reveal e-mail address> --

morning all,

i'm trying to dynamically load a usercontrol onto a webform. the
usercontrol is essentially a search function and contains the code
required to display the form, process the search request and display the
results.

the problem is that when i try and dynamically load the control, the id of
the control is added to the id of the textbox on the form, which prevents
me from retrieving the text entered in the box.

i understand why it's doing this (i think!), but is there any way to stop
it?? or do i just have to hardcode the details into the code??

the code i'm using to load the control is:

dim myUserControl as myNamespace.myControl =
LoadControl("path_to_myControl.ascx")
frmSearch.Controls.Add(myUserControl)
myUserControl.ID = "ctrlSrch"

if i leave the myUserControl.ID line out, then i just get a control id
added to the form fields.

thanks for any help

darren.

| [aspngcontrolsvb] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngcontrolsvb.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

Reply to this message...
 
    
darren
On 18 February 2002 at 12:58:48, aspngcontrolsvb <Click here to reveal e-mail address> wrote:

JW> Darren,
JW> Can you post some of the code to your usercontrol?

ok...i've included a sample below...

JW> But I think your problem is that your trying to access the textbox of the
JW> usercontrol from the aspx page that contains the user control. Instead,
JW> just access the text box from within the usercontrol (in other words, do the
JW> work of the usercontrol from within the user control).

i'm trying to get at the textbox from within the usercontrol, but the id
of the textbox has changed, so it's not recognized by the usercontrol.

anyway the code snippets are below. hopefully this is everything!
there's no @Register directive in the aspx page, but that doesn't make
much difference if its there or not.

if i include the @Register directive, and include the usercontrol in the
aspx page, everything works fine.

searchcrtl.ascx:
<asp:textbox id="txtSearch" runat="server" />
<asp:button id="btnGo" text="search" runat="server" />
<asp:label id="outText" runat="server" />

searchctrl.ascx.vb

Public MustInherit Class findtest
Inherits System.Web.UI.UserControl
Protected WithEvents txtSearch As System.Web.UI.WebControls.TextBox
protected WithEvents btnGo as System.Web.UI.WebControls.Button
protected WithEvents outText as System.Web.UI.WebControls.Label

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
if isPostBack then
if len(txtSearch.Text) > 0 then outText.Text = txtSearch.Text else outText.Text = "got nada"
end if
' note: this could be outText.Text = txtSearch.Text and nothing
' appears.
End Sub

searchtest.aspx.vb
Public Class SearchTest
Inherits System.Web.UI.Page
protected withEvents frmSearch as System.Web.UI.HtmlControls.HtmlForm

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dim myCtrl as myNameSpace.myControl = LoadControl("path_to_my_control.ascx")
frmSearch.Controls.Add(myCtrl)
End Sub
End Class

and a view source after running the page gives:

<form name="frmSearch" method="post" action="SearchTest.aspx" id="frmSearch">
<input name="_ctl0:txtSearch" type="text" value="test" id="_ctl0_txtSearch" />
<input type="submit" name="_ctl0:btnText" value="search" id="_ctl0_btnText" />
<span id="_ctl0_outText">got nada</span>
</form>

which screams out that i'm missing something obvious as the label is
updated, but not with the text from the textbox!

thanks for any help,

darren.

Reply to this message...
 
    
Jeff Widmer
Darren,
Get rid of:
frmSearch.Controls.Add(myCtrl)

And replace it with:
page.controls.add(myCtrl)

Also get rid of:
protected withEvents frmSearch as System.Web.UI.HtmlControls.HtmlForm

and just use the Page class that is available from inheriting
System.Web.UI.Page into the aspx code-behind.

-Jeff

-----Original Message-----
From: darren [mailto:Click here to reveal e-mail address]
Sent: Monday, February 18, 2002 8:25 AM
To: aspngcontrolsvb
Subject: [aspngcontrolsvb] RE: dynamically loading usercontrols

On 18 February 2002 at 12:58:48, aspngcontrolsvb <Click here to reveal e-mail address>
wrote:

JW> Darren,
JW> Can you post some of the code to your usercontrol?

ok...i've included a sample below...

JW> But I think your problem is that your trying to access the textbox of
the
JW> usercontrol from the aspx page that contains the user control. Instead,
JW> just access the text box from within the usercontrol (in other words, do
the
JW> work of the usercontrol from within the user control).

i'm trying to get at the textbox from within the usercontrol, but the id
of the textbox has changed, so it's not recognized by the usercontrol.

anyway the code snippets are below. hopefully this is everything!
there's no @Register directive in the aspx page, but that doesn't make
much difference if its there or not.

if i include the @Register directive, and include the usercontrol in the
aspx page, everything works fine.

searchcrtl.ascx:
<asp:textbox id="txtSearch" runat="server" />
<asp:button id="btnGo" text="search" runat="server" />
<asp:label id="outText" runat="server" />

searchctrl.ascx.vb

Public MustInherit Class findtest
Inherits System.Web.UI.UserControl
Protected WithEvents txtSearch As System.Web.UI.WebControls.TextBox
protected WithEvents btnGo as System.Web.UI.WebControls.Button
protected WithEvents outText as System.Web.UI.WebControls.Label

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
if isPostBack then
if len(txtSearch.Text) > 0 then outText.Text = txtSearch.Text else
outText.Text = "got nada"
end if
' note: this could be outText.Text = txtSearch.Text and nothing
' appears.
End Sub

searchtest.aspx.vb
Public Class SearchTest
Inherits System.Web.UI.Page
protected withEvents frmSearch as System.Web.UI.HtmlControls.HtmlForm

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
dim myCtrl as myNameSpace.myControl =
LoadControl("path_to_my_control.ascx")
frmSearch.Controls.Add(myCtrl)
End Sub
End Class

and a view source after running the page gives:

<form name="frmSearch" method="post" action="SearchTest.aspx"
id="frmSearch">
<input name="_ctl0:txtSearch" type="text" value="test"
id="_ctl0_txtSearch" />
<input type="submit" name="_ctl0:btnText" value="search"
id="_ctl0_btnText" />
<span id="_ctl0_outText">got nada</span>
</form>

which screams out that i'm missing something obvious as the label is
updated, but not with the text from the textbox!

thanks for any help,

darren.

| [aspngcontrolsvb] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngcontrolsvb.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

Reply to this message...
 
    
darren
On 18 February 2002 at 14:02:51, aspngcontrolsvb <Click here to reveal e-mail address> wrote:

jeff,

JW> Get rid of:
JW> frmSearch.Controls.Add(myCtrl)

JW> And replace it with:
JW> page.controls.add(myCtrl)

JW> Also get rid of:
JW> protected withEvents frmSearch as System.Web.UI.HtmlControls.HtmlForm

ok...how do you control where the usercontrol is inserted into the page??
just using this command sticks it at the end of the page outside of the
<form>...</form> tags. moving the form tags to the usercontrol, still
doesn't update the label with the text from the textbox.

if i try with a <asp:placeholder .../> to specify where the control should
go, i have the same results as above.

thanks for your help,

darren.

JW> -----Original Message-----
JW> From: darren [mailto:Click here to reveal e-mail address]
JW> Sent: Monday, February 18, 2002 8:25 AM
JW> To: aspngcontrolsvb
JW> Subject: [aspngcontrolsvb] RE: dynamically loading usercontrols

JW> On 18 February 2002 at 12:58:48, aspngcontrolsvb <Click here to reveal e-mail address>
JW> wrote:

JW> searchcrtl.ascx:
JW> <asp:textbox id="txtSearch" runat="server" />
JW> <asp:button id="btnGo" text="search" runat="server" />
JW> <asp:label id="outText" runat="server" />

JW> searchctrl.ascx.vb

JW> Public MustInherit Class findtest
JW> Inherits System.Web.UI.UserControl
JW> Protected WithEvents txtSearch As System.Web.UI.WebControls.TextBox
JW> protected WithEvents btnGo as System.Web.UI.WebControls.Button
JW> protected WithEvents outText as System.Web.UI.WebControls.Label

JW> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
JW> System.EventArgs) Handles MyBase.Load
JW> if isPostBack then
JW> if len(txtSearch.Text) > 0 then outText.Text = txtSearch.Text else
JW> outText.Text = "got nada"
JW> end if
JW> ' note: this could be outText.Text = txtSearch.Text and nothing
JW> ' appears.
JW> End Sub

JW> searchtest.aspx.vb
JW> Public Class SearchTest
JW> Inherits System.Web.UI.Page
JW> protected withEvents frmSearch as System.Web.UI.HtmlControls.HtmlForm

JW> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
JW> System.EventArgs) Handles MyBase.Load
JW> dim myCtrl as myNameSpace.myControl =
JW> LoadControl("path_to_my_control.ascx")
JW> frmSearch.Controls.Add(myCtrl)
JW> End Sub
JW> End Class

JW> and a view source after running the page gives:

JW> <form name="frmSearch" method="post" action="SearchTest.aspx"
JW> id="frmSearch">
JW> <input name="_ctl0:txtSearch" type="text" value="test"
JW> id="_ctl0_txtSearch" />
JW> <input type="submit" name="_ctl0:btnText" value="search"
JW> id="_ctl0_btnText" />
JW> <span id="_ctl0_outText">got nada</span>
JW> </form>

JW> which screams out that i'm missing something obvious as the label is
JW> updated, but not with the text from the textbox!

JW> thanks for any help,

JW> darren.

Reply to this message...
 
    
Jeff Widmer
Darren,
Can you post the code to the aspx page?

It should look something like this:
<form runat=server>
    <asp:placeholder runat=server id=ctlPl />
</form>

then in the aspx code-behind

protected ctlPl as placeholder

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
    ctlPl.Controls.Add(Page.LoadControl("path_to_my_control.ascx"))
End Sub

-Jeff

-----Original Message-----
From: darren [mailto:Click here to reveal e-mail address]
Sent: Monday, February 18, 2002 9:05 AM
To: aspngcontrolsvb
Subject: [aspngcontrolsvb] RE: dynamically loading usercontrols

On 18 February 2002 at 14:02:51, aspngcontrolsvb <Click here to reveal e-mail address>
wrote:

jeff,

JW> Get rid of:
JW> frmSearch.Controls.Add(myCtrl)

JW> And replace it with:
JW> page.controls.add(myCtrl)

JW> Also get rid of:
JW> protected withEvents frmSearch as System.Web.UI.HtmlControls.HtmlForm

ok...how do you control where the usercontrol is inserted into the page??
just using this command sticks it at the end of the page outside of the
<form>...</form> tags. moving the form tags to the usercontrol, still
doesn't update the label with the text from the textbox.

if i try with a <asp:placeholder .../> to specify where the control should
go, i have the same results as above.

thanks for your help,

darren.

JW> -----Original Message-----
JW> From: darren [mailto:Click here to reveal e-mail address]
JW> Sent: Monday, February 18, 2002 8:25 AM
JW> To: aspngcontrolsvb
JW> Subject: [aspngcontrolsvb] RE: dynamically loading usercontrols

JW> On 18 February 2002 at 12:58:48, aspngcontrolsvb
<Click here to reveal e-mail address>
JW> wrote:

JW> searchcrtl.ascx:
JW> <asp:textbox id="txtSearch" runat="server" />
JW> <asp:button id="btnGo" text="search" runat="server" />
JW> <asp:label id="outText" runat="server" />

JW> searchctrl.ascx.vb

JW> Public MustInherit Class findtest
JW> Inherits System.Web.UI.UserControl
JW> Protected WithEvents txtSearch As System.Web.UI.WebControls.TextBox
JW> protected WithEvents btnGo as System.Web.UI.WebControls.Button
JW> protected WithEvents outText as System.Web.UI.WebControls.Label

JW> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
JW> System.EventArgs) Handles MyBase.Load
JW> if isPostBack then
JW> if len(txtSearch.Text) > 0 then outText.Text = txtSearch.Text
else
JW> outText.Text = "got nada"
JW> end if
JW> ' note: this could be outText.Text = txtSearch.Text and nothing
JW> ' appears.
JW> End Sub

JW> searchtest.aspx.vb
JW> Public Class SearchTest
JW> Inherits System.Web.UI.Page
JW> protected withEvents frmSearch as
System.Web.UI.HtmlControls.HtmlForm

JW> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
JW> System.EventArgs) Handles MyBase.Load
JW> dim myCtrl as myNameSpace.myControl =
JW> LoadControl("path_to_my_control.ascx")
JW> frmSearch.Controls.Add(myCtrl)
JW> End Sub
JW> End Class

JW> and a view source after running the page gives:

JW> <form name="frmSearch" method="post" action="SearchTest.aspx"
JW> id="frmSearch">
JW> <input name="_ctl0:txtSearch" type="text" value="test"
JW> id="_ctl0_txtSearch" />
JW> <input type="submit" name="_ctl0:btnText" value="search"
JW> id="_ctl0_btnText" />
JW> <span id="_ctl0_outText">got nada</span>
JW> </form>

JW> which screams out that i'm missing something obvious as the label is
JW> updated, but not with the text from the textbox!

JW> thanks for any help,

JW> darren.

| [aspngcontrolsvb] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngcontrolsvb.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

Reply to this message...
 
    
darren
On 18 February 2002 at 20:19:10, jeff widmer <Click here to reveal e-mail address> wrote:

JW> Can you post the code to the aspx page?

it looks just like what you typed...the control shows up exactly where it
should...it just doesn't read the value of the textbox.

JW> It should look something like this:
JW> <form runat=server>
JW> <asp:placeholder runat=server id=ctlPl />
JW> </form>

JW> then in the aspx code-behind

JW> protected ctlPl as placeholder

JW> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
JW> System.EventArgs) Handles MyBase.Load
JW> ctlPl.Controls.Add(Page.LoadControl("path_to_my_control.ascx"))
JW> End Sub

a bit of experimentation shows that:

strString = myTextBox.text

gives the initial value of the textbox always. while

strString = Request.Form("ctrlID:" & myTextbox)

(where ctrlID is the extra bit added onto the id and name fields) gives
the actual value of the textbox.

both of these are within the ascx file after the form has been submitted.
if the control is added onto the page using the @register method, you get
the actual values of the textbox for both.

do you know if there is any problem accessing server-side properties of
dynamically loaded controls??

many thanks,

darren.

Reply to this message...
 
    
Jeff Widmer
Darren,
OK, you're problem is not where the control is being instantiated but
understanding the control/page lifecycle.

At what point in the page lifecycle are you trying to access the value in
the textbox? Is it before the LoadViewState event has fired? If it is,
then the textbox has only been initialized but it has not yet been populated
with the data from the postback (this happens in LoadViewState). Try adding
your control in the Page_Init event.

-Jeff

-----Original Message-----
From: darren [mailto:Click here to reveal e-mail address]
Sent: Monday, February 18, 2002 6:12 PM
To: aspngcontrolsvb
Subject: [aspngcontrolsvb] RE: dynamically loading usercontrols

On 18 February 2002 at 20:19:10, jeff widmer <Click here to reveal e-mail address>
wrote:

JW> Can you post the code to the aspx page?

it looks just like what you typed...the control shows up exactly where it
should...it just doesn't read the value of the textbox.

JW> It should look something like this:
JW> <form runat=server>
JW> <asp:placeholder runat=server id=ctlPl />
JW> </form>

JW> then in the aspx code-behind

JW> protected ctlPl as placeholder

JW> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
JW> System.EventArgs) Handles MyBase.Load
JW> ctlPl.Controls.Add(Page.LoadControl("path_to_my_control.ascx"))
JW> End Sub

a bit of experimentation shows that:

strString = myTextBox.text

gives the initial value of the textbox always. while

strString = Request.Form("ctrlID:" & myTextbox)

(where ctrlID is the extra bit added onto the id and name fields) gives
the actual value of the textbox.

both of these are within the ascx file after the form has been submitted.
if the control is added onto the page using the @register method, you get
the actual values of the textbox for both.

do you know if there is any problem accessing server-side properties of
dynamically loaded controls??

many thanks,

darren.

| [aspngcontrolsvb] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngcontrolsvb.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

Reply to this message...
 
    
darren
On 19 February 2002 at 05:19:48, jeff widmer <Click here to reveal e-mail address> wrote:

jeff,

JW> OK, you're problem is not where the control is being instantiated but
JW> understanding the control/page lifecycle.

ahhh....

JW> At what point in the page lifecycle are you trying to access the value in
JW> the textbox? Is it before the LoadViewState event has fired? If it is,
JW> then the textbox has only been initialized but it has not yet been populated
JW> with the data from the postback (this happens in LoadViewState). Try adding
JW> your control in the Page_Init event.

at the moment i'm trying to get the value either in the page_onload event
or in the onclick event for the button.

looking at the trace information the onload grab is being done after the
LoadViewState and ProcessPostData, but there is a second ProcessPostData
after the page_onload, which is presumably the one that's updating the
form values. any ideas why there are two ProcessPostData events?

if i try and grab the values from an onclick even on the button, then
things work as desired as the event is trapped further down the lifecycle.
i guess that's the way to go, especially as the onclick even is fired when
the user hits return in the textbox as well.

one last thing, where would you put the control in the Page_Init event??
i have the standard visual studio generated stuff, which is just a call to
InitializeComponent.

many thanks for your help,

darren.

Reply to this message...
 
 
System.EventArgs
System.Object
System.Web.UI.HtmlControls.HtmlForm
System.Web.UI.Page
System.Web.UI.UserControl
System.Web.UI.WebControls.Button
System.Web.UI.WebControls.Label
System.Web.UI.WebControls.TextBox




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com