Results 1 to 5 of 5

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

Threaded View

  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


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