|
-
Oct 16th, 2007, 01:04 PM
#1
Thread Starter
Frenzied Member
[resolved] [2005] Dynamically created controls get a new ID
I'm not sure why this is happening, but for some strange reason, the asigned ID value of a control i created dynamically in a webpage gets changed - and as such, i can't find it again later to read it's value.
Try this example,... create a ASP.NET project (using VS2005). On the form, drop a PlaceHolder control called phrControls, a Button, and a Label called lblCheck. Then in the code behind window, type this:
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim txtBox As New TextBox
txtBox.Text = "sample"
txtBox.ID = "txtBruce"
phrControls.Controls.Add(txtBox)
End Sub
Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
lblCheck.Text = Request.Params("txtBruce")
End Sub
Run it. Type something the text box, and then click the button. Your text is found and display properly.
That works fine for me as well. My problem is when i use a MasterPage. The controls are now inside a ContentPlaceHolder, and when i run the project and click the button, nothing is found. 
The reason is that my controls have been given a new name! This is what i find when i look at the source code of the HTML page:
Code:
<input name="ctl00$cphMainBody$txtBruce" type="text" id="ctl00_cphMainBody_txtBruce" />
Why does VB change the name of my control to have all that extra stuff? And how do i fix it?
.
.
Last edited by MrGTI; Oct 16th, 2007 at 02:54 PM.
Reason: resolved
~Peter

-
Oct 16th, 2007, 02:31 PM
#2
Re: [2005] Dynamically created controls get a new ID
Controls are assigned a name based on the control hierarchy they belong to; ASP.NET uses these values to load view state & fire events (on postbacks). You don't need to fix it.
Your immediate problems are:
1) Dynamic controls really should be created in the CreateChildControls method (overridden); the Load event is raised after the control hierarchy is initialised.
2) You're using Request.Params; one of the key points of ASP.NET is the ability to use objects and move away the classic ASP "style". Declare your textbox as a member variable, initialise it once in CreateChildControls, and refer to it by it's instance, e.g.:
Code:
private TextBox txtFoo;
protected override CreateChildControls() {
base.CreateChildControls();
txtFoo = new TextBox();
txtFoo.ID = "txtFoo";
phrControls.Controls.Add(txtFoo);
}
private void Button1_Click(object sender, EventArgs e) {
this.EnsureChildControls();
lblCheck.Text = txtFoo.Text;
}
-
Oct 16th, 2007, 02:43 PM
#3
Re: [2005] Dynamically created controls get a new ID
You could also use the FindControls method to get a reference to the textbox control. Something like
Code:
Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As ContentPlaceHolder = Me.Master.FindControl("ContentPlaceHolder1")
lblCheck.Text = CType(a.FindControl("txtBruce"), TextBox).Text
End Sub
If you have a Panel in the ContentPlaceholder and the TextBox is in the Panel you would need to do something like
Code:
Dim a As ContentPlaceHolder = Me.Master.FindControl("ContentPlaceHolder1")
Dim b As Panel = a.FindControl("Panel1")
lblCheck.Text = CType(b.FindControl("txtBruce"), TextBox).Text
-
Oct 16th, 2007, 02:54 PM
#4
Thread Starter
Frenzied Member
Re: [2005] Dynamically created controls get a new ID
Thanks Axion_Sa. I appreciate that suggestion. While valid, it wouldn't work for me because the dynamic controls are coming from a database, so pre-declaring them (ie: private TextBox txtFoo) wouldn't work in my situation.
BruceVde's suggestion works really well. It's even smart enough to realize that txtQuestion1 and txtQuestion10 are not the same control! 
Thanks guys.
~Peter

-
Oct 17th, 2007, 08:39 AM
#5
Re: [resolved] [2005] Dynamically created controls get a new ID
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|