javascript referencing items within a Repeater?

H

HockeyFan

Yesterday, I posted a question dealing with an issue of trying to
reference (from javascript on the client side) an item within a
Repeater. My code was hard-coded to use the actual ClientId, but
someone admonished me, stating that I shouldn't do that because the id
could change. However, since I'm trying to reference a control on a
Repeater line, I'm wondering how else to do that. Repeaters aren't
rendered in html are they? What method do other .Net developers use
to reference a control on a RepeaterItem when doing it from javascript?
 
T

Teemu Keiski

Hi,

Repeaters do render but what you have specified them to. Pij t is to
generate the script to access the controls on repeaterItem, at least
generate an js array which contains the ClientIDs of the controls in the
repeater

It can be done for example iterating Repeater's Items collection, calling
FindControl against every RepeaterItem to get reference to the control on
the item. Then you can access ClientID of these controls

I have availbale a sample which deals with gridView but idea is 100% same.
item type is just RepeaterItem and you iterate Repeater's Items collection

protected void GridView1_PreRender(object sender, EventArgs e)
{
StringBuilder sbScript = new StringBuilder();

//I'm doing this in PreRender because RowDataBound happens only
when databinding occurs - not necessarily on every request
//and RowCreated is too early to get the correct client-side ID
foreach(GridViewRow row in GridView1.Rows)
{
CheckBox CheckBox1 = (CheckBox)row.FindControl("CheckBox1");
sbScript.Append("'");
sbScript.Append(CheckBox1.ClientID);
sbScript.Append("'");
if(row.RowIndex != (GridView1.Rows.Count - 1))
sbScript.Append(",");

}
//Register the array
Page.ClientScript.RegisterArrayDeclaration("checkBoxes",
sbScript.ToString());
}.

Previous examplöe creates a javascript array named checkBoxes cointaining
reference of CheckBoxes with CheckBox1 on every GridViewRow. You can then
use these array at client to reference the checkboxes there.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top