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.

Re: FindControl in Repeater by msnews

msnews
Tue May 06 12:17:20 CDT 2008

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.com> wrote in message
news:OGzyCv5rIHA.3616@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.
>



Re: FindControl in Repeater by Lucid

Lucid
Tue May 06 12:57:54 CDT 2008

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.com> wrote in message
news:Osz%23yy5rIHA.1436@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.com> wrote in message
> news:OGzyCv5rIHA.3616@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.
>>
>
>


Re: FindControl in Repeater by Madhur

Madhur
Tue May 06 13:13:59 CDT 2008

FindControl does not search recursively.

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

--
Madhur

<msnews.microsoft.com> wrote in message
news:Osz%23yy5rIHA.1436@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.com> wrote in message
> news:OGzyCv5rIHA.3616@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.
>>
>
>


Re: FindControl in Repeater by msnews

msnews
Tue May 06 14:05:16 CDT 2008

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

"Madhur" <sdf@df.com> wrote in message
news:uMe7nV6rIHA.3780@TK2MSFTNGP03.phx.gbl...
> FindControl does not search recursively.
>
> You need to do RepeaterControl.FindControl("textbox1") instead of
> Page.FindControl ("textbox1").
>
> --
> Madhur
>
> <msnews.microsoft.com> wrote in message
> news:Osz%23yy5rIHA.1436@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.com> wrote in message
>> news:OGzyCv5rIHA.3616@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.
>>>
>>
>>
>



Re: FindControl in Repeater by Ignacio

Ignacio
Tue May 06 14:17:40 CDT 2008

On May 6, 2:13=A0pm, "Madhur" <s...@df.com> wrote:
> FindControl does not search recursively.
>
> You need to do RepeaterControl.FindControl("textbox1") instead of
> Page.FindControl ("textbox1").
>
> --
> Madhur
>
> <msnews.microsoft.com> wrote in message
>
> news:Osz%23yy5rIHA.1436@TK2MSFTNGP05.phx.gbl...
>
>
>
> > Sorry, the controls are dynamically created and placed in a PlaceHolder
> > control. =A0Does there have to be some sort of looipng to find the contr=
ol?
>
> > <msnews.microsoft.com> wrote in message
> >news:OGzyCv5rIHA.3616@TK2MSFTNGP06.phx.gbl...
> >>I dynamically create controls (textboxes) in a repeater control. =A0I kn=
ow
> >>their names (eg. TextBox1). =A0How 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

Re: FindControl in Repeater by Ignacio

Ignacio
Tue May 06 14:18:53 CDT 2008

On May 6, 3:05=A0pm, <msnews.microsoft.com> wrote:
> Problem is, even the placeholder control is dynamically created so I can't=

> reference it in the code. =A0Any thoughts?
>
> "Madhur" <s...@df.com> wrote in message
>
> news:uMe7nV6rIHA.3780@TK2MSFTNGP03.phx.gbl...
>
>
>
> > FindControl does not search recursively.
>
> > You need to do RepeaterControl.FindControl("textbox1") instead of
> > Page.FindControl ("textbox1").
>
> > --
> > Madhur
>
> > <msnews.microsoft.com> wrote in message
> >news:Osz%23yy5rIHA.1436@TK2MSFTNGP05.phx.gbl...
> >> Sorry, the controls are dynamically created and placed in a PlaceHolder=

> >> control. =A0Does there have to be some sort of looipng to find the cont=
rol?
>
> >> <msnews.microsoft.com> wrote in message
> >>news:OGzyCv5rIHA.3616@TK2MSFTNGP06.phx.gbl...
> >>>I dynamically create controls (textboxes) in a repeater control. =A0I k=
now
> >>>their names (eg. TextBox1). =A0How do I find the text of TextBox1 in th=
e
> >>>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?

Re: FindControl in Repeater by Ignacio

Ignacio
Tue May 06 14:21:04 CDT 2008

On May 6, 1:57=A0pm, "Lucid" <lu...@phreak2000.com> wrote:
> There is a better way of doing this, but here is an example from some old
> code of mine that you can play with. =A0Not sure if this is EXACTLY what y=
ou
> 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 in=
to
> a collection, in this case an ArrayList. =A0Works well for what I was doin=
g,
> hopefully it points you in the right direction:
>
> =A0 =A0 =A0 =A0 //Make an ArrayList to hold all of the dynamic controls.
> =A0 =A0 =A0 =A0 ArrayList controls =3D new ArrayList();
>
> =A0 =A0 =A0 =A0 //Lets make some text boxes.
> =A0 =A0 =A0 =A0 int i =3D 0;
> =A0 =A0 =A0 =A0 while (i <=3D 10)
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 TextBox tb =3D new TextBox();
> =A0 =A0 =A0 =A0 =A0 =A0 tb.ID =3D i.ToString();
>
> =A0 =A0 =A0 =A0 =A0 =A0 /*Do here whatever you will to render the control =
to the page,
> etc.
> =A0 =A0 =A0 =A0 =A0 =A0 =A0*
> =A0 =A0 =A0 =A0 =A0 =A0 =A0*
> =A0 =A0 =A0 =A0 =A0 =A0 =A0*/
>
> =A0 =A0 =A0 =A0 =A0 =A0 //Add the TextBox to your array List.
> =A0 =A0 =A0 =A0 =A0 =A0 controls.Add(tb);
> =A0 =A0 =A0 =A0 }
>
> =A0 =A0 =A0 =A0 //Now just for measure if we are dependent on keeping and =
saving
> that data through PostBacks, etc.
> =A0 =A0 =A0 =A0 Session["Controls"] =3D (ArrayList)controls;
>
> =A0 =A0 =A0 =A0 //Now at a later time if we want to get the data from thos=
e text
> boxes
> =A0 =A0 =A0 =A0 ArrayList controls2 =3D (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

Re: FindControl in Repeater by Lucid

Lucid
Tue May 06 14:28:44 CDT 2008

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 )" <ignacio.machin@gmail.com> wrote in
message
news:2b7ef669-fb16-471b-97c5-a33b7f01cc32@z72g2000hsb.googlegroups.com...
On May 6, 1:57 pm, "Lucid" <lu...@phreak2000.com> wrote:
> 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


Re: FindControl in Repeater by Lucid

Lucid
Tue May 06 14:32:37 CDT 2008

Sorry correct, if session = null... hehe.


"Lucid" <lucid@phreak2000.com> wrote in message
news:A8C53961-B30E-4A21-B2B3-FC6A8422901E@microsoft.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 )" <ignacio.machin@gmail.com> wrote in
> message
> news:2b7ef669-fb16-471b-97c5-a33b7f01cc32@z72g2000hsb.googlegroups.com...
> On May 6, 1:57 pm, "Lucid" <lu...@phreak2000.com> wrote:
>> 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


Re: FindControl in Repeater by Ignacio

Ignacio
Tue May 06 15:13:18 CDT 2008

On May 6, 3:28=A0pm, "Lucid" <lu...@phreak2000.com> wrote:
> The code below would actually overwrite the session variable every time it=
s
> ran, but if you dont want it to do that =A0just throw a if session !=3D nu=
ll
> 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.

Re: FindControl in Repeater by Ignacio

Ignacio
Tue May 06 15:24:01 CDT 2008

On May 6, 3:05=A0pm, <msnews.microsoft.com> wrote:
> Problem is, even the placeholder control is dynamically created so I can't=

> reference it in the code. =A0Any thoughts?
>
> "Madhur" <s...@df.com> wrote in message
>
> news:uMe7nV6rIHA.3780@TK2MSFTNGP03.phx.gbl...
>
>
>
> > FindControl does not search recursively.
>
> > You need to do RepeaterControl.FindControl("textbox1") instead of
> > Page.FindControl ("textbox1").
>
> > --
> > Madhur
>
> > <msnews.microsoft.com> wrote in message
> >news:Osz%23yy5rIHA.1436@TK2MSFTNGP05.phx.gbl...
> >> Sorry, the controls are dynamically created and placed in a PlaceHolder=

> >> control. =A0Does there have to be some sort of looipng to find the cont=
rol?
>
> >> <msnews.microsoft.com> wrote in message
> >>news:OGzyCv5rIHA.3616@TK2MSFTNGP06.phx.gbl...
> >>>I dynamically create controls (textboxes) in a repeater control. =A0I k=
now
> >>>their names (eg. TextBox1). =A0How do I find the text of TextBox1 in th=
e
> >>>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<Control> CreateRepeaterItemsControls() {
List<Control> controlsInRow =3D new List<Control>();
controlsInRow.Add(new LiteralControl("<tr class=3D
\"RoleControlRepeaterRowCssClass\"><td align=3D\"right\" style=3D\"width:
50%\">"));
Control c =3D new Label();
c.ID =3D "RoleNameLBL";
controlsInRow.Add(c);
controlsInRow.Add(new LiteralControl("</td><td align=3D\"left
\" style=3D\"width: 50%\">"));
c =3D new CheckBox();
c.ID =3D "RoleStatusCHK";
controlsInRow.Add(c);
spanDiv =3D new HtmlGenericControl();
spanDiv.ID =3D "SpanDiv";
spanDiv.Style.Add("visibility", "hidden");
messageLBL =3D new Label();
messageLBL.ID =3D "MessageLBL";
spanDiv.Controls.Add(messageLBL);
controlsInRow.Add(spanDiv);
controlsInRow.Add(new LiteralControl("</td><td></td></
tr>"));

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

return controlsInRow;
}
List<Control> CreateRepeaterFooterControls() {
List<Control> controlsInRow =3D new List<Control>();
controlsInRow.Add(new LiteralControl("</table>"));

return controlsInRow;
}
//The delegate declaration for the above methods
delegate List<Control> RepeaterControlCreator();
//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 =3D 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 =3D new Repeater();
roleLST.ItemDataBound +=3D new
RepeaterItemEventHandler(RoleLST_ItemDataBound);
roleLST.HeaderTemplate =3D new
RepeaterTemplate( new
RepeaterControlCreator(CreateRepeaterHeaderControls)) ;
roleLST.ItemTemplate =3D new
RepeaterTemplate( new
RepeaterControlCreator(CreateRepeaterItemsControls));
roleLST.AlternatingItemTemplate =3D new
RepeaterTemplate( new
RepeaterControlCreator(CreateRepeaterItemsControls));
roleLST.FooterTemplate =3D new
RepeaterTemplate( new
RepeaterControlCreator(CreateRepeaterFooterControls));

Controsl.Add( roleLST);
}


protected void OkBTN_Click(object sender, EventArgs e) {
//update the status
int index =3D 0;
foreach (RepeaterItem rItem in roleLST.Items) {
CheckBox cb =3D rItem.FindControl("RoleStatusCHK") as
CheckBox;
}

Re: FindControl in Repeater by Lucid

Lucid
Tue May 06 16:05:40 CDT 2008

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 )" <ignacio.machin@gmail.com> wrote in
message
news:a5a684ba-055e-4b29-8ad4-2a192b7b5b9a@24g2000hsh.googlegroups.com...
On May 6, 3:28 pm, "Lucid" <lu...@phreak2000.com> wrote:
> 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.


Re: FindControl in Repeater by Ignacio

Ignacio
Wed May 07 08:46:03 CDT 2008

On May 6, 5:05=A0pm, "Lucid" <lu...@phreak2000.com> wrote:
> Well you could always create session variables based off the pages name th=
at
> 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

Re: FindControl in Repeater by Lucid

Lucid
Wed May 07 10:44:20 CDT 2008

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 )" <ignacio.machin@gmail.com> wrote in
message
news:5388def9-e23a-4e1a-ae2f-80f1d11621cd@z72g2000hsb.googlegroups.com...
On May 6, 5:05 pm, "Lucid" <lu...@phreak2000.com> wrote:
> 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