471,349 Members | 1,228 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

FindControl in Repeater

I dynamically create controls (textboxes) in a repeater control. I know
their names (eg. TextBox1). How do I find the text of TextBox1 in the Form?
FindControl does not seem to work.
Jun 27 '08 #1
15 3706
Sorry, the controls are dynamically created and placed in a PlaceHolder
control. Does there have to be some sort of looipng to find the control?
<msnews.microsoft.comwrote in message
news:OG**************@TK2MSFTNGP06.phx.gbl...
>I dynamically create controls (textboxes) in a repeater control. I know
their names (eg. TextBox1). How do I find the text of TextBox1 in the
Form? FindControl does not seem to work.

Jun 27 '08 #2
There is a better way of doing this, but here is an example from some old
code of mine that you can play with. Not sure if this is EXACTLY what you
are looking for, but i've found that the FindControl ability in the
framework is spotty at best so I throw whatever dynamic controls I make into
a collection, in this case an ArrayList. Works well for what I was doing,
hopefully it points you in the right direction:
//Make an ArrayList to hold all of the dynamic controls.
ArrayList controls = new ArrayList();

//Lets make some text boxes.
int i = 0;
while (i <= 10)
{
TextBox tb = new TextBox();
tb.ID = i.ToString();

/*Do here whatever you will to render the control to the page,
etc.
*
*
*/
//Add the TextBox to your array List.
controls.Add(tb);
}

//Now just for measure if we are dependent on keeping and saving
that data through PostBacks, etc.
Session["Controls"] = (ArrayList)controls;
//Now at a later time if we want to get the data from those text
boxes
ArrayList controls2 = (ArrayList)Session["Controls"];

//Make a loop to see what we have and make a referrence to them.
foreach (Control cOUT in controls2)
{
TextBox txtOUT = (TextBox)cOUT;

//Get the data however you with such as txtOUT.Text;

}
Lucid
Phreak2000.Com
Administrator

<msnews.microsoft.comwrote in message
news:Os****************@TK2MSFTNGP05.phx.gbl...
Sorry, the controls are dynamically created and placed in a PlaceHolder
control. Does there have to be some sort of looipng to find the control?
<msnews.microsoft.comwrote in message
news:OG**************@TK2MSFTNGP06.phx.gbl...
>>I dynamically create controls (textboxes) in a repeater control. I know
their names (eg. TextBox1). How do I find the text of TextBox1 in the
Form? FindControl does not seem to work.

Jun 27 '08 #3
FindControl does not search recursively.

You need to do RepeaterControl.FindControl("textbox1") instead of
Page.FindControl ("textbox1").

--
Madhur

<msnews.microsoft.comwrote in message
news:Os****************@TK2MSFTNGP05.phx.gbl...
Sorry, the controls are dynamically created and placed in a PlaceHolder
control. Does there have to be some sort of looipng to find the control?
<msnews.microsoft.comwrote in message
news:OG**************@TK2MSFTNGP06.phx.gbl...
>>I dynamically create controls (textboxes) in a repeater control. I know
their names (eg. TextBox1). How do I find the text of TextBox1 in the
Form? FindControl does not seem to work.

Jun 27 '08 #4
Problem is, even the placeholder control is dynamically created so I can't
reference it in the code. Any thoughts?

"Madhur" <sd*@df.comwrote in message
news:uM**************@TK2MSFTNGP03.phx.gbl...
FindControl does not search recursively.

You need to do RepeaterControl.FindControl("textbox1") instead of
Page.FindControl ("textbox1").

--
Madhur

<msnews.microsoft.comwrote in message
news:Os****************@TK2MSFTNGP05.phx.gbl...
>Sorry, the controls are dynamically created and placed in a PlaceHolder
control. Does there have to be some sort of looipng to find the control?
<msnews.microsoft.comwrote in message
news:OG**************@TK2MSFTNGP06.phx.gbl...
>>>I dynamically create controls (textboxes) in a repeater control. I know
their names (eg. TextBox1). How do I find the text of TextBox1 in the
Form? FindControl does not seem to work.


Jun 27 '08 #5
On May 6, 2:13*pm, "Madhur" <s...@df.comwrote:
FindControl does not search recursively.

You need to do RepeaterControl.FindControl("textbox1") instead of
Page.FindControl ("textbox1").

--
Madhur

<msnews.microsoft.comwrote in message

news:Os****************@TK2MSFTNGP05.phx.gbl...
Sorry, the controls are dynamically created and placed in a PlaceHolder
control. *Does there have to be some sort of looipng to find the control?
<msnews.microsoft.comwrote in message
news:OG**************@TK2MSFTNGP06.phx.gbl...
>I dynamically create controls (textboxes) in a repeater control. *I know
their names (eg. TextBox1). *How do I find the text of TextBox1 in the
Form? FindControl does not seem to work.- Hide quoted text -

- Show quoted text -
Hi,
The above will not work neither, each row has its own set of controls,
you can do the above at a row level though
Jun 27 '08 #6
On May 6, 3:05*pm, <msnews.microsoft.comwrote:
Problem is, even the placeholder control is dynamically created so I can't
reference it in the code. *Any thoughts?

"Madhur" <s...@df.comwrote in message

news:uM**************@TK2MSFTNGP03.phx.gbl...
FindControl does not search recursively.
You need to do RepeaterControl.FindControl("textbox1") instead of
Page.FindControl ("textbox1").
--
Madhur
<msnews.microsoft.comwrote in message
news:Os****************@TK2MSFTNGP05.phx.gbl...
Sorry, the controls are dynamically created and placed in a PlaceHolder
control. *Does there have to be some sort of looipng to find the control?
<msnews.microsoft.comwrote in message
news:OG**************@TK2MSFTNGP06.phx.gbl...
I dynamically create controls (textboxes) in a repeater control. *I know
their names (eg. TextBox1). *How do I find the text of TextBox1 in the
Form? FindControl does not seem to work.- Hide quoted text -

- Show quoted text -
Yes,

Describe better your problem, what are you trying to do?
Why you need to have access to the other controls?
What control is raising the event that you are receiving in the code
behind?
Jun 27 '08 #7
On May 6, 1:57*pm, "Lucid" <lu...@phreak2000.comwrote:
There is a better way of doing this, but here is an example from some old
code of mine that you can play with. *Not sure if this is EXACTLY what you
are looking for, but i've found that the FindControl ability in the
framework is spotty at best so I throw whatever dynamic controls I make into
a collection, in this case an ArrayList. *Works well for what I was doing,
hopefully it points you in the right direction:

* * * * //Make an ArrayList to hold all of the dynamic controls.
* * * * ArrayList controls = new ArrayList();

* * * * //Lets make some text boxes.
* * * * int i = 0;
* * * * while (i <= 10)
* * * * {
* * * * * * TextBox tb = new TextBox();
* * * * * * tb.ID = i.ToString();

* * * * * * /*Do here whatever you will to render the control to the page,
etc.
* * * * * * **
* * * * * * **
* * * * * * **/

* * * * * * //Add the TextBox to your array List.
* * * * * * controls.Add(tb);
* * * * }

* * * * //Now just for measure if we are dependent on keeping and saving
that data through PostBacks, etc.
* * * * Session["Controls"] = (ArrayList)controls;

* * * * //Now at a later time if we want to get the data from those text
boxes
* * * * ArrayList controls2 = (ArrayList)Session["Controls"];
Hi,

And what happen if the user open two windows from the same session
(open one window from the other) ?
You have ONLY ONE list in Session, therefore both instances willl be
mixed together
Jun 27 '08 #8
The code below would actually overwrite the session variable every time its
ran, but if you dont want it to do that just throw a if session != null
fill session with arraylist else just render the controls to the page.
"Ignacio Machin ( .NET/ C# MVP )" <ig************@gmail.comwrote in
message
news:2b**********************************@z72g2000 hsb.googlegroups.com...
On May 6, 1:57 pm, "Lucid" <lu...@phreak2000.comwrote:
There is a better way of doing this, but here is an example from some old
code of mine that you can play with. Not sure if this is EXACTLY what you
are looking for, but i've found that the FindControl ability in the
framework is spotty at best so I throw whatever dynamic controls I make
into
a collection, in this case an ArrayList. Works well for what I was doing,
hopefully it points you in the right direction:

//Make an ArrayList to hold all of the dynamic controls.
ArrayList controls = new ArrayList();

//Lets make some text boxes.
int i = 0;
while (i <= 10)
{
TextBox tb = new TextBox();
tb.ID = i.ToString();

/*Do here whatever you will to render the control to the page,
etc.
*
*
*/

//Add the TextBox to your array List.
controls.Add(tb);
}

//Now just for measure if we are dependent on keeping and saving
that data through PostBacks, etc.
Session["Controls"] = (ArrayList)controls;

//Now at a later time if we want to get the data from those text
boxes
ArrayList controls2 = (ArrayList)Session["Controls"];
Hi,

And what happen if the user open two windows from the same session
(open one window from the other) ?
You have ONLY ONE list in Session, therefore both instances willl be
mixed together

Jun 27 '08 #9
Sorry correct, if session = null... hehe.
"Lucid" <lu***@phreak2000.comwrote in message
news:A8**********************************@microsof t.com...
The code below would actually overwrite the session variable every time
its ran, but if you dont want it to do that just throw a if session !=
null fill session with arraylist else just render the controls to the
page.
"Ignacio Machin ( .NET/ C# MVP )" <ig************@gmail.comwrote in
message
news:2b**********************************@z72g2000 hsb.googlegroups.com...
On May 6, 1:57 pm, "Lucid" <lu...@phreak2000.comwrote:
>There is a better way of doing this, but here is an example from some old
code of mine that you can play with. Not sure if this is EXACTLY what you
are looking for, but i've found that the FindControl ability in the
framework is spotty at best so I throw whatever dynamic controls I make
into
a collection, in this case an ArrayList. Works well for what I was doing,
hopefully it points you in the right direction:

//Make an ArrayList to hold all of the dynamic controls.
ArrayList controls = new ArrayList();

//Lets make some text boxes.
int i = 0;
while (i <= 10)
{
TextBox tb = new TextBox();
tb.ID = i.ToString();

/*Do here whatever you will to render the control to the page,
etc.
*
*
*/

//Add the TextBox to your array List.
controls.Add(tb);
}

//Now just for measure if we are dependent on keeping and saving
that data through PostBacks, etc.
Session["Controls"] = (ArrayList)controls;

//Now at a later time if we want to get the data from those text
boxes
ArrayList controls2 = (ArrayList)Session["Controls"];

Hi,

And what happen if the user open two windows from the same session
(open one window from the other) ?
You have ONLY ONE list in Session, therefore both instances willl be
mixed together
Jun 27 '08 #10
On May 6, 3:28*pm, "Lucid" <lu...@phreak2000.comwrote:
The code below would actually overwrite the session variable every time its
ran, but if you dont want it to do that *just throw a if session != null
fill session with arraylist else just render the controls to the page.
That was not the point I was trying to make, what if you want to do
the same in two pages at the same time?
Like editing a record in each page.

In that escenario (and using Session the way you are doing) one
instance will override the otehr isntance data.
Jun 27 '08 #11
On May 6, 3:05*pm, <msnews.microsoft.comwrote:
Problem is, even the placeholder control is dynamically created so I can't
reference it in the code. *Any thoughts?

"Madhur" <s...@df.comwrote in message

news:uM**************@TK2MSFTNGP03.phx.gbl...
FindControl does not search recursively.
You need to do RepeaterControl.FindControl("textbox1") instead of
Page.FindControl ("textbox1").
--
Madhur
<msnews.microsoft.comwrote in message
news:Os****************@TK2MSFTNGP05.phx.gbl...
Sorry, the controls are dynamically created and placed in a PlaceHolder
control. *Does there have to be some sort of looipng to find the control?
<msnews.microsoft.comwrote in message
news:OG**************@TK2MSFTNGP06.phx.gbl...
I dynamically create controls (textboxes) in a repeater control. *I know
their names (eg. TextBox1). *How do I find the text of TextBox1 in the
Form? FindControl does not seem to work.- Hide quoted text -

- Show quoted text -
Hi,

In this case first of all you need to recreate ALL the controls in
postback.
Here is a piece of code I use to create and fill a repeater in code.
There are some classes of mine that I use (cause I bind to a
collection of business objetcs) that yo will not have, but you will
get the idea of how it is done.

List<ControlCreateRepeaterItemsControls() {
List<ControlcontrolsInRow = new List<Control>();
controlsInRow.Add(new LiteralControl("<tr class=
\"RoleControlRepeaterRowCssClass\"><td align=\"right\" style=\"width:
50%\">"));
Control c = new Label();
c.ID = "RoleNameLBL";
controlsInRow.Add(c);
controlsInRow.Add(new LiteralControl("</td><td align=\"left
\" style=\"width: 50%\">"));
c = new CheckBox();
c.ID = "RoleStatusCHK";
controlsInRow.Add(c);
spanDiv = new HtmlGenericControl();
spanDiv.ID = "SpanDiv";
spanDiv.Style.Add("visibility", "hidden");
messageLBL = new Label();
messageLBL.ID = "MessageLBL";
spanDiv.Controls.Add(messageLBL);
controlsInRow.Add(spanDiv);
controlsInRow.Add(new LiteralControl("</td><td></td></
tr>"));

return controlsInRow;
}
List<ControlCreateRepeaterHeaderControls() {
List<ControlcontrolsInRow = new List<Control>();
controlsInRow.Add(new LiteralControl("<table width=\"100%
\">"));

return controlsInRow;
}
List<ControlCreateRepeaterFooterControls() {
List<ControlcontrolsInRow = new List<Control>();
controlsInRow.Add(new LiteralControl("</table>"));

return controlsInRow;
}
//The delegate declaration for the above methods
delegate List<ControlRepeaterControlCreator();
//This is the class that implement the template for each row
of the repeater
class RepeaterTemplate: ITemplate {
RepeaterControlCreator controlCreator;
public RepeaterTemplate(RepeaterControlCreator
controlCreator) {
this.controlCreator = controlCreator;
}
public void InstantiateIn(System.Web.UI.Control container)
{
foreach (Control c in controlCreator())
container.Controls.Add(c);
}
}

protected override void CreateChildControls() {
base.CreateChildControls();

roleLST = new Repeater();
roleLST.ItemDataBound += new
RepeaterItemEventHandler(RoleLST_ItemDataBound);
roleLST.HeaderTemplate = new
RepeaterTemplate( new
RepeaterControlCreator(CreateRepeaterHeaderControl s)) ;
roleLST.ItemTemplate = new
RepeaterTemplate( new
RepeaterControlCreator(CreateRepeaterItemsControls ));
roleLST.AlternatingItemTemplate = new
RepeaterTemplate( new
RepeaterControlCreator(CreateRepeaterItemsControls ));
roleLST.FooterTemplate = new
RepeaterTemplate( new
RepeaterControlCreator(CreateRepeaterFooterControl s));

Controsl.Add( roleLST);
}
protected void OkBTN_Click(object sender, EventArgs e) {
//update the status
int index = 0;
foreach (RepeaterItem rItem in roleLST.Items) {
CheckBox cb = rItem.FindControl("RoleStatusCHK") as
CheckBox;
}
Jun 27 '08 #12
Well you could always create session variables based off the pages name that
they are currently on ( basically by the file name they are currently on )
and declare your session as Session[filename+"Controls"], then do the same
in reverse when wanting to access the controls in said session. If you are
using the same page with different pass-in vairables to decide what is
dynamically created delcare the session based off of said pass in (
Session[Request.QueryString["YourPassIn"]+"Controls], and again, do the same
in reverse when wanting to access the controls in the array list.


"Ignacio Machin ( .NET/ C# MVP )" <ig************@gmail.comwrote in
message
news:a5**********************************@24g2000h sh.googlegroups.com...
On May 6, 3:28 pm, "Lucid" <lu...@phreak2000.comwrote:
The code below would actually overwrite the session variable every time
its
ran, but if you dont want it to do that just throw a if session != null
fill session with arraylist else just render the controls to the page.
That was not the point I was trying to make, what if you want to do
the same in two pages at the same time?
Like editing a record in each page.

In that escenario (and using Session the way you are doing) one
instance will override the otehr isntance data.

Jun 27 '08 #13
On May 6, 5:05*pm, "Lucid" <lu...@phreak2000.comwrote:
Well you could always create session variables based off the pages name that
they are currently on ( basically by the file name they are currently on )
and declare your session as Session[filename+"Controls"], then do the same
in reverse when wanting to access the controls in said session.
What if it's the same page? :)

Don't get me wrong, the reason I'm being so persistent is that I found
my self in that case, and then I realized that I had no clear way of
how to store two concurrent status in session.

I haven;t solved the problem yet, in my case I could simply use the
ViewState of the page (the data amount wat not big and it was
serializable) but otherwise I would still have the issues
Jun 27 '08 #14
Like I said before if its the same page you could always use query strings
to decide what section to store the array in, thats actually in fact what I
had to do for a dynamic survey engine I wrote. One web user control that
pulled questions from the database and created dynamic textboxes for each
question. Basically I used the query string "?step=[1-10]" and for each
step I held a reference to those controls in a seperate session variable.
"Ignacio Machin ( .NET/ C# MVP )" <ig************@gmail.comwrote in
message
news:53**********************************@z72g2000 hsb.googlegroups.com...
On May 6, 5:05 pm, "Lucid" <lu...@phreak2000.comwrote:
Well you could always create session variables based off the pages name
that
they are currently on ( basically by the file name they are currently on )
and declare your session as Session[filename+"Controls"], then do the same
in reverse when wanting to access the controls in said session.
What if it's the same page? :)

Don't get me wrong, the reason I'm being so persistent is that I found
my self in that case, and then I realized that I had no clear way of
how to store two concurrent status in session.

I haven;t solved the problem yet, in my case I could simply use the
ViewState of the page (the data amount wat not big and it was
serializable) but otherwise I would still have the issues

Jun 27 '08 #15
Hello
You mentioned about using session variable to store Repeater item values. My tasks involve exactly the same but I am not sure how to do this. Can you post some sample code?(When I click a button, its command argument will be used to render a value on the same page. this is what i'm doin right now and i need to store the button's command argument value as session).
Thank you
Any suggestions are appreciated.
VJ
Jul 31 '08 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by MattB | last post: by
1 post views Thread by HockeyFan | last post: by
5 posts views Thread by John Kotuby | last post: by
1 post views Thread by MDS1 | last post: by
2 posts views Thread by Jeff | last post: by
9 posts views Thread by AAaron123 | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.