Accessing renamed controls in code behind

J

John Holmes

I'm using data to rename some web controls on a form that uses a repeater
contol and so it can have mulitple instances of the same control set. The
controls get renamed (thanks to Steven Cheng's help) in a click event of the
button that is pushed to enter data. Each time the button is clicked it
sends the ID entered by the user to query the database and return
information. The renamed controls then include the ID in the name of the
control.

At the bottom of my html form, Form1, is a button used to update the dataset
with any information entered in by the user. I need to access the renamed
controls at this point and check their values, and I can build a string that
is the same as the ID of the control. I'm not sure how to use this string to
access say the Checked value of a CheckBox, though. I've tried various
things, but I don't think I have the scope correct and could use some tips.

private void btnSubmitForm_ServerClick(object sender, System.EventArgs e)

{

foreach(DataRow row in dsAssessorData.Tables["tabParcels"].Rows)

{

string strParcelID = row["ParcelID"].ToString();

CheckBox chkHist = (CheckBox)Form1.FindControl("chk" + strParcelID +
"Historic");

row["Historic"] = chkHist.Checked;

}

}

Gives the following error:

C:\Inetpub\wwwroot\Affidavits\WebForm1.aspx.cs(407): The type or namespace
name 'Form1' could not be found (are you missing a using directive or an
assembly reference?)
 
R

Rimu Atkinson

try using row.FindControl or row.Items.FindControl

turn on trace for the current page, and it will list out the heirachy of
objects in your page, so you know exactly which controls collection to look
through.

R
 
S

Steven Cheng[MSFT]

Hi John,


Thanks for posting here and your confirmation on my former suggestions.
From your description, currently you're wondering how to correctly retrieve
those controls(which has been renamed) from the repeater control,yes?
If there is anything I misunderstood, please feel free to let me know.

As for this question, I think Rimu Atkinson's suggestion is quite correct.
Every asp.net page or server control has a control hierarchy all its direct
sub controls are set in its controls collection. And the
Control.FindControl method is used to find control in a page or server
control's direct(one level below) child controls. If you want to find
controls all over a whole page, you need to loop through all the control
and its child control's controls collection, do you think so?
For detailed description on the Control.FindControl method, please view the
following reference in MSDN:

#Control.FindControl Method
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWebUIControlCl
assFindControlTopic.asp?frame=true

So as for you situation, if you'd like to find and retrieve the controls in
the repeater's template, you need to FindControl at the direct parent
control of those one in the template(such as Label or TextBox). For example,
-----------------
private void lnkFirst_Click(object sender, System.EventArgs e)
{
foreach(RepeaterItem rptItem in rptCheckBox.Items)
{
Label lblName = (Label)rptItem.FindControl("lblName");
// .... find other control as above
}
}

We can find a control in its parent's controls collection as long as we
have its id. Since you've change the controls id to other custom value
from the database, you do need to retrieve them via the proper id
value(generated from the data's value).

Also, you can use the "Trace" function in ASP.NET to trace the whole
Control hierarchy in a certain asp.net page, just set the below directive
in the page:
<%@ Page Language="VB" Trace="True" TraceMode="SortByCategory" %>
For more detailed info on ASP.NET tracing, you may view the following
reference:
#Tracing
http://msdn.microsoft.com/library/en-us/dnaspnet/html/asp01252001.asp?frame=
true

In addition ,here is another kb article on find child control in template
control, I believe it also helpful to you.
#How to find child controls that are located in the template of a parent
control
http://support.microsoft.com/?id=323261

Please check out the suggestions. If you feel anything unclear, please feel
free to let me know.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
J

John Holmes

I'm still not having success. I've tried the "foreach(RepeaterItem rptItem
in rptCheckBox.Items)" approach and it works if I put the original ID value
in, chkHistoric, but if I put the changed ID value in, chkP34031Historic, it
doesn't find it. I did turn on tracing and the control is referred to as:

Repeater1:_ctl1:chkP34031Historic (first instance)
Repeater1:_ctl2:chkP70450Historic (second instance)

I think maybe I need to do a nested loop on Repeater1, but I'm not sure how
to get to these controls or reference them with the trace info.

More trace info that includes the whole repeater section:

txtParcelID
System.Web.UI.WebControls.TextBox
260
0

_ctl13
System.Web.UI.ResourceBasedLiteralControl
474
0

Repeater1
System.Web.UI.WebControls.Repeater
16501
36

Repeater1:_ctl0
System.Web.UI.WebControls.RepeaterItem
85
0

Repeater1:_ctl0:_ctl0
System.Web.UI.LiteralControl
85
0

Repeater1:_ctl1
System.Web.UI.WebControls.RepeaterItem
8132
0

Repeater1:_ctl1:_ctl0
System.Web.UI.DataBoundLiteralControl
1381
236

Repeater1:_ctl1:chkP34031Forest
System.Web.UI.WebControls.CheckBox
154
36

Repeater1:_ctl1:_ctl1
System.Web.UI.ResourceBasedLiteralControl
310
0

Repeater1:_ctl1:chkP34031OpenSpace
System.Web.UI.WebControls.CheckBox
178
36

Repeater1:_ctl1:_ctl2
System.Web.UI.ResourceBasedLiteralControl
285
0

Repeater1:_ctl1:chkP34031NonProfit
System.Web.UI.WebControls.CheckBox
160
36

Repeater1:_ctl1:_ctl3
System.Web.UI.ResourceBasedLiteralControl
284
0

Repeater1:_ctl1:chkP34031Historic
System.Web.UI.WebControls.CheckBox
139
0

Repeater1:_ctl1:_ctl4
System.Web.UI.ResourceBasedLiteralControl
583
0

Repeater1:_ctl1:radP34031PropType1
System.Web.UI.WebControls.RadioButton
186
0

Repeater1:_ctl1:_ctl5
System.Web.UI.LiteralControl
146
0

Repeater1:_ctl1:radP34031PropType2
System.Web.UI.WebControls.RadioButton
229
0

Repeater1:_ctl1:_ctl6
System.Web.UI.LiteralControl
146
0

Repeater1:_ctl1:radP34031PropType3
System.Web.UI.WebControls.RadioButton
188
0

Repeater1:_ctl1:_ctl7
System.Web.UI.LiteralControl
146
0

Repeater1:_ctl1:radP34031PropType4
System.Web.UI.WebControls.RadioButton
199
0

Repeater1:_ctl1:_ctl8
System.Web.UI.LiteralControl
146
0

Repeater1:_ctl1:radP34031PropType5
System.Web.UI.WebControls.RadioButton
198
0

Repeater1:_ctl1:_ctl9
System.Web.UI.LiteralControl
146
0

Repeater1:_ctl1:radP34031PropType6
System.Web.UI.WebControls.RadioButton
190
0

Repeater1:_ctl1:_ctl10
System.Web.UI.ResourceBasedLiteralControl
338
0

Repeater1:_ctl1:radP34031PrincipalUse1
System.Web.UI.WebControls.RadioButton
212
0

Repeater1:_ctl1:_ctl11
System.Web.UI.LiteralControl
146
0

Repeater1:_ctl1:radP34031PrincipalUse2
System.Web.UI.WebControls.RadioButton
222
0

Repeater1:_ctl1:_ctl12
System.Web.UI.LiteralControl
146
0

Repeater1:_ctl1:radP34031PrincipalUse3
System.Web.UI.WebControls.RadioButton
199
0

Repeater1:_ctl1:_ctl13
System.Web.UI.LiteralControl
146
0

Repeater1:_ctl1:radP34031PrincipalUse4
System.Web.UI.WebControls.RadioButton
205
0

Repeater1:_ctl1:_ctl14
System.Web.UI.LiteralControl
146
0

Repeater1:_ctl1:radP34031PrincipalUse5
System.Web.UI.WebControls.RadioButton
214
0

Repeater1:_ctl1:_ctl15
System.Web.UI.LiteralControl
146
0

Repeater1:_ctl1:radP34031PrincipalUse6
System.Web.UI.WebControls.RadioButton
198
0

Repeater1:_ctl1:_ctl16
System.Web.UI.LiteralControl
28
0

Repeater1:_ctl1:txtPrincipalUseOther
System.Web.UI.WebControls.TextBox
128
0

Repeater1:_ctl1:_ctl17
System.Web.UI.ResourceBasedLiteralControl
264
0

Repeater1:_ctl2
System.Web.UI.WebControls.RepeaterItem
8254
0

Repeater1:_ctl2:_ctl0
System.Web.UI.DataBoundLiteralControl
1521
424

Repeater1:_ctl2:chkP70450Forest
System.Web.UI.WebControls.CheckBox
154
36

Repeater1:_ctl2:_ctl1
System.Web.UI.ResourceBasedLiteralControl
310
0

Repeater1:_ctl2:chkP70450OpenSpace
System.Web.UI.WebControls.CheckBox
160
36

Repeater1:_ctl2:_ctl2
System.Web.UI.ResourceBasedLiteralControl
285
0

Repeater1:_ctl2:chkP70450NonProfit
System.Web.UI.WebControls.CheckBox
160
36

Repeater1:_ctl2:_ctl3
System.Web.UI.ResourceBasedLiteralControl
284
0

Repeater1:_ctl2:chkP70450Historic
System.Web.UI.WebControls.CheckBox
139
0

Repeater1:_ctl2:_ctl4
System.Web.UI.ResourceBasedLiteralControl
583
0

Repeater1:_ctl2:radP70450PropType1
System.Web.UI.WebControls.RadioButton
186
0

Repeater1:_ctl2:_ctl5
System.Web.UI.LiteralControl
146
0

Repeater1:_ctl2:radP70450PropType2
System.Web.UI.WebControls.RadioButton
229
0

Repeater1:_ctl2:_ctl6
System.Web.UI.LiteralControl
146
0

Repeater1:_ctl2:radP70450PropType3
System.Web.UI.WebControls.RadioButton
188
0

Repeater1:_ctl2:_ctl7
System.Web.UI.LiteralControl
146
0

Repeater1:_ctl2:radP70450PropType4
System.Web.UI.WebControls.RadioButton
199
0

Repeater1:_ctl2:_ctl8
System.Web.UI.LiteralControl
146
0

Repeater1:_ctl2:radP70450PropType5
System.Web.UI.WebControls.RadioButton
198
0

Repeater1:_ctl2:_ctl9
System.Web.UI.LiteralControl
146
0

Repeater1:_ctl2:radP70450PropType6
System.Web.UI.WebControls.RadioButton
190
0

Repeater1:_ctl2:_ctl10
System.Web.UI.ResourceBasedLiteralControl
338
0

Repeater1:_ctl2:radP70450PrincipalUse1
System.Web.UI.WebControls.RadioButton
212
0

Repeater1:_ctl2:_ctl11
System.Web.UI.LiteralControl
146
0

Repeater1:_ctl2:radP70450PrincipalUse2
System.Web.UI.WebControls.RadioButton
222
0

Repeater1:_ctl2:_ctl12
System.Web.UI.LiteralControl
146
0

Repeater1:_ctl2:radP70450PrincipalUse3
System.Web.UI.WebControls.RadioButton
199
0

Repeater1:_ctl2:_ctl13
System.Web.UI.LiteralControl
146
0

Repeater1:_ctl2:radP70450PrincipalUse4
System.Web.UI.WebControls.RadioButton
205
0

Repeater1:_ctl2:_ctl14
System.Web.UI.LiteralControl
146
0

Repeater1:_ctl2:radP70450PrincipalUse5
System.Web.UI.WebControls.RadioButton
214
0

Repeater1:_ctl2:_ctl15
System.Web.UI.LiteralControl
146
0

Repeater1:_ctl2:radP70450PrincipalUse6
System.Web.UI.WebControls.RadioButton
198
0

Repeater1:_ctl2:_ctl16
System.Web.UI.LiteralControl
28
0

Repeater1:_ctl2:txtPrincipalUseOther
System.Web.UI.WebControls.TextBox
128
0

Repeater1:_ctl2:_ctl17
System.Web.UI.ResourceBasedLiteralControl
264
0

Repeater1:_ctl3
System.Web.UI.WebControls.RepeaterItem
30
0

Repeater1:_ctl3:_ctl0
System.Web.UI.LiteralControl
30
0

_ctl14
System.Web.UI.ResourceBasedLiteralControl
657
0

txtPersPropDesc
 
S

Steven Cheng[MSFT]

Hi John,


Thanks for your followup. I've reviewed the whole post and found that the
problem may be due to approaches we used to associate the control with
data records in the DataTable, here is my suggestions:
In the former post, you asked how to manually change the sub controls' id
in the repeater's repeateritem. And from the code logic in this post, I
found that your approach to associate the data binding controls( in the
repeater) with the related datarow in the DataTable is to loopthrough the
DataRows in the DataTable and then retrieve the certain databinding
controls via the "id"(get from datarow), and that's why you need to change
those control's id when they're binded with data, yes?

However, the problem is that the "Control.FindControl" method has
limitation that it can only retrieve the sub controls which is directly
under the "Control" which is being called the "FindControl" method as I
mentioned in the last reply. As for your situation, you want to directly
retrieve one of those databinded controls by "id" via calling the
repeater's FindControl method, yes? Then you'll found it unable to find the
certain control because the databined controls are not the direct child
controls of the Repeater, but is the direct child control of a
RepeaterItem. One way to accomplish this is to loop through all the
RepeaterItems of the repeater and use findControl to search them in every
RepeaterItem, but this will lead to poor performance, do you think so?

So I suggest that you try the following approach:
1. When the update button is clicked, in the Button's click event handler
function, you use foreach to loop through the
repeater control repeateritem, and then retrieve those certain child
controls from the repeateritem(that's ok , use the means I mentioned in the
last reply). Then find the correct record(DataRow) in the DataTable and
update the record.
Thus, I think you needn't even change the control's id when binded just let
them remain their default id( generated by index). How do you think of this
approach?

Please check out my suggestions. If you have any questions on it , please
feel free to let me know.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top