J
jaffar.kazi
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
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