473,383 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Accessing renamed controls in code behind

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?)
Nov 18 '05 #1
4 2036
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

"John Holmes" <jo****@co.skagit.wa.us> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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?)

Nov 18 '05 #2
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...WebUIControlCl
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...001.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.)

Nov 18 '05 #3
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




"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:Yf**************@cpmsftngxa07.phx.gbl...
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...WebUIControlCl 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...001.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.)

Nov 18 '05 #4
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
Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jonathan Williams | last post by:
Hi, I have an object which inherits from WebControl (CUSTOM : WebControl) In this object I have code in which I add child contols: protected override void CreateChildControls() {...
1
by: Chris Kettenbach | last post by:
Hi I am designing an ASP.net app. Just wanted some opinions on code behind options. My idea is to have a code behind file that the controls will reference and have ascx files for the controls. ...
2
by: Ric | last post by:
im new to asp.net. from what i understand, you have the aspx file (presentation), user-control(ascx file), code-behind(vb file) and components(compiled vb and dll files). the aspx file contains a...
3
by: Thubaiti | last post by:
Hi, I have this code in my ASP.NET and I want to convert it to C# (code behind) <asp:Repeater id="subCategoryRepeater" runat="server"> <ItemTemplate> <ul> <li> <asp:HyperLink...
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
9
by: Alan Silver | last post by:
Hello, I have a user control which I am trying to load dynamically, but am running into problems. I think the problem is because I have two .ascx files that refer to the same .ascx.cs file. A...
4
by: John Kotuby | last post by:
Hi all, I am using a Repeater in conjunction with a SQLDatasource and SQL Server. One of the controls in the repeater is a HyperlLink as follows: <asp:HyperLink...
5
by: michael234 | last post by:
Hi All, Before i get into things I'm writing in C# using VS2003 with framework 1.1. I am having a problem accessing a user control in the code behind. I'm using the user control for some...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.