Results 1 to 5 of 5

Thread: [resolved] [2005] Dynamically created controls get a new ID

  1. #1

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Question [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


  2. #2
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    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;
    }

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    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

  4. #4

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up 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


  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width