Query regarding control names on an asp.net page

Discussion in 'ASP .Net' started by jaffar.kazi@gmail.com, Aug 25, 2006.

  1. Guest

    Hi,
    What is the exact name to use to get a control reference on an asp.net
    page using FindControl?
    My experience is the following:
    1. Page.FindControl does not work; it always returns null, even if I
    give the exact control name I'm using, eg. txtFirstName.
    However, this.FindControl works.

    2. There is a page that includes 2 user controls. I'm using
    AddressControl.ascx and have included it twice on the page as
    AddressControl1.ascx and AddressControl2.ascx.
    Now, there will be 2 instances of txtFirstName, but they will be unique
    somehow. If we look into the code of the page, this is what we see:
    <td>
    <input name="AddressControl1$txtFirstName" type="text"
    maxlength="20" id="AddressControl1_txtFirstName" />
    <span id="AddressControl1_lblFirstName" style="color:Red;"></span>
    </td>

    and

    <td>
    <input name="AddressControl2$txtFirstName" type="text"
    maxlength="20" id="AddressControl2_txtFirstName" />
    <span id="AddressControl2_lblFirstName" style="color:Red;"></span>
    </td>

    So we see that the UserControl name is prefixed to the 2 controls to
    make them unique. The interesting thing is that there are 2 things, a
    name, which is AddressControl2$txtFirstName and and ID, which is
    AddressControl2_txtFirstName

    Which do we search for, the name or the ID?
    I've given the name to search for, i.e., AddressControl2$txtFirstName,
    and it found my control, but it couldn't find it by the ID.

    The final question here is the syntax, i.e., (UserControlVarName $
    UserControlFieldName). How can we ensure that this is not going to
    change in the future with a newer version of ASP.NET? Or is there any
    other way to do this?

    Environment: I'm using .NET 2.0 on Windows XP.

    Thanks in Advance,
    --Jaffar
     
    , Aug 25, 2006
    #1
    1. Advertising

  2. Tim_Mac Guest

    hi jaffar,
    i believe the Id is the correct one to use, rather than the name, are you
    sure you didn't forget the underscore character?

    if you are calling FindControl from within the user control, you can just
    use this.FindControl("txtName") and you don't need to worry about the
    renamed client ID of the text box.

    did you try using an approach like this from your page code-behind:

    this.UserControl1.FindControl("txtName");

    this is probably safer than relying on the naming convention you have
    correctly identified of UserControlId_ControlId

    tim



    --------------------------
    blog: http://tim.mackey.ie
    <> wrote in message
    news:...
    > Hi,
    > What is the exact name to use to get a control reference on an asp.net
    > page using FindControl?
    > My experience is the following:
    > 1. Page.FindControl does not work; it always returns null, even if I
    > give the exact control name I'm using, eg. txtFirstName.
    > However, this.FindControl works.
    >
    > 2. There is a page that includes 2 user controls. I'm using
    > AddressControl.ascx and have included it twice on the page as
    > AddressControl1.ascx and AddressControl2.ascx.
    > Now, there will be 2 instances of txtFirstName, but they will be unique
    > somehow. If we look into the code of the page, this is what we see:
    > <td>
    > <input name="AddressControl1$txtFirstName" type="text"
    > maxlength="20" id="AddressControl1_txtFirstName" />
    > <span id="AddressControl1_lblFirstName" style="color:Red;"></span>
    > </td>
    >
    > and
    >
    > <td>
    > <input name="AddressControl2$txtFirstName" type="text"
    > maxlength="20" id="AddressControl2_txtFirstName" />
    > <span id="AddressControl2_lblFirstName" style="color:Red;"></span>
    > </td>
    >
    > So we see that the UserControl name is prefixed to the 2 controls to
    > make them unique. The interesting thing is that there are 2 things, a
    > name, which is AddressControl2$txtFirstName and and ID, which is
    > AddressControl2_txtFirstName
    >
    > Which do we search for, the name or the ID?
    > I've given the name to search for, i.e., AddressControl2$txtFirstName,
    > and it found my control, but it couldn't find it by the ID.
    >
    > The final question here is the syntax, i.e., (UserControlVarName $
    > UserControlFieldName). How can we ensure that this is not going to
    > change in the future with a newer version of ASP.NET? Or is there any
    > other way to do this?
    >
    > Environment: I'm using .NET 2.0 on Windows XP.
    >
    > Thanks in Advance,
    > --Jaffar
    >
     
    Tim_Mac, Aug 25, 2006
    #2
    1. Advertising

  3. Guest

    Hi Tim.
    Yes,
    I came to the same conclusion of using:
    this.UserControl1.FindControl("txtName");

    instead of going into the area of "guessing" the final object name/ID.

    The problem was that I was calling it from the main page, not from the
    user-control, and I was trying to fix this. Did this by first finding
    the control, then finding the child controls inside the user control,
    exactly how you're suggesting.

    Thanks for this.
    Regards,
    --Jaffar

    Tim_Mac wrote:
    > hi jaffar,
    > i believe the Id is the correct one to use, rather than the name, are you
    > sure you didn't forget the underscore character?
    >
    > if you are calling FindControl from within the user control, you can just
    > use this.FindControl("txtName") and you don't need to worry about the
    > renamed client ID of the text box.
    >
    > did you try using an approach like this from your page code-behind:
    >
    > this.UserControl1.FindControl("txtName");
    >
    > this is probably safer than relying on the naming convention you have
    > correctly identified of UserControlId_ControlId
    >
    > tim
    >
    >
    >
    > --------------------------
    > blog: http://tim.mackey.ie
    > <> wrote in message
    > news:...
    > > Hi,
    > > What is the exact name to use to get a control reference on an asp.net
    > > page using FindControl?
    > > My experience is the following:
    > > 1. Page.FindControl does not work; it always returns null, even if I
    > > give the exact control name I'm using, eg. txtFirstName.
    > > However, this.FindControl works.
    > >
    > > 2. There is a page that includes 2 user controls. I'm using
    > > AddressControl.ascx and have included it twice on the page as
    > > AddressControl1.ascx and AddressControl2.ascx.
    > > Now, there will be 2 instances of txtFirstName, but they will be unique
    > > somehow. If we look into the code of the page, this is what we see:
    > > <td>
    > > <input name="AddressControl1$txtFirstName" type="text"
    > > maxlength="20" id="AddressControl1_txtFirstName" />
    > > <span id="AddressControl1_lblFirstName" style="color:Red;"></span>
    > > </td>
    > >
    > > and
    > >
    > > <td>
    > > <input name="AddressControl2$txtFirstName" type="text"
    > > maxlength="20" id="AddressControl2_txtFirstName" />
    > > <span id="AddressControl2_lblFirstName" style="color:Red;"></span>
    > > </td>
    > >
    > > So we see that the UserControl name is prefixed to the 2 controls to
    > > make them unique. The interesting thing is that there are 2 things, a
    > > name, which is AddressControl2$txtFirstName and and ID, which is
    > > AddressControl2_txtFirstName
    > >
    > > Which do we search for, the name or the ID?
    > > I've given the name to search for, i.e., AddressControl2$txtFirstName,
    > > and it found my control, but it couldn't find it by the ID.
    > >
    > > The final question here is the syntax, i.e., (UserControlVarName $
    > > UserControlFieldName). How can we ensure that this is not going to
    > > change in the future with a newer version of ASP.NET? Or is there any
    > > other way to do this?
    > >
    > > Environment: I'm using .NET 2.0 on Windows XP.
    > >
    > > Thanks in Advance,
    > > --Jaffar
    > >
     
    , Aug 28, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan
    Replies:
    1
    Views:
    5,439
    Christophe Gijbels
    Jan 4, 2004
  2. Grant Schenck
    Replies:
    2
    Views:
    397
    =?Utf-8?B?RGF2aWQgQ29sbGl2ZXI=?=
    Nov 14, 2006
  3. Bhavesh
    Replies:
    1
    Views:
    391
    Peter Bromberg [C# MVP]
    Feb 13, 2008
  4. Dan
    Replies:
    5
    Views:
    371
  5. Dan
    Replies:
    3
    Views:
    832
    Alessandro Zifiglio
    Jan 21, 2004
Loading...

Share This Page