FindControl in Repeater

G

Guest

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.
 
G

Guest

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?
 
L

Lucid

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
 
M

Madhur

FindControl does not search recursively.

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

Guest

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

Ignacio Machin ( .NET/ C# MVP )

FindControl does not search recursively.

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

--
Madhur

<msnews.microsoft.com> wrote in message






- 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
 
I

Ignacio Machin ( .NET/ C# MVP )

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







- 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?
 
I

Ignacio Machin ( .NET/ C# MVP )

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
 
L

Lucid

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.


message
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
 
L

Lucid

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


Lucid said:
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.


message
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
 
I

Ignacio Machin ( .NET/ C# MVP )

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.
 
I

Ignacio Machin ( .NET/ C# MVP )

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







- 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 = 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<Control> CreateRepeaterHeaderControls() {
List<Control> controlsInRow = new List<Control>();
controlsInRow.Add(new LiteralControl("<table width=\"100%
\">"));

return controlsInRow;
}
List<Control> CreateRepeaterFooterControls() {
List<Control> controlsInRow = 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 = 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(CreateRepeaterHeaderControls)) ;
roleLST.ItemTemplate = new
RepeaterTemplate( new
RepeaterControlCreator(CreateRepeaterItemsControls));
roleLST.AlternatingItemTemplate = new
RepeaterTemplate( new
RepeaterControlCreator(CreateRepeaterItemsControls));
roleLST.FooterTemplate = new
RepeaterTemplate( new
RepeaterControlCreator(CreateRepeaterFooterControls));

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;
}
 
L

Lucid

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.




message
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.
 
I

Ignacio Machin ( .NET/ C# MVP )

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
 
L

Lucid

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.


message
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
 
V

Vijeya J

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
 

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

Top