Get a control value of a parent Repeatercontrol from inside the ch

R

Raj

// Binded the data for repeater2 control (child) in testRepeater1_OnItemCreated

protected void testRepeater1_OnItemCreated(object sender,
RepeaterItemEventArgs e)

protected void testRepeater2_OnItemCreated(object sender,
RepeaterItemEventArgs e)
{
// trying to access the parent control value using
// e.Item.Parent but it is returning null

}

How to access the parent control value
 
J

Jesse Houwing

Hello Raj,
// Binded the data for repeater2 control (child) in
testRepeater1_OnItemCreated

protected void testRepeater1_OnItemCreated(object sender,
RepeaterItemEventArgs e)

protected void testRepeater2_OnItemCreated(object sender,
RepeaterItemEventArgs e)
{
// trying to access the parent control value using
// e.Item.Parent but it is returning null
}

How to access the parent control value

Looking at Reflector, the Item is first created, then the event is raised
and then it is added to the repeater, so the parent will never be available
in the OnItemCreated call, aside from the object sender, which will contain
a reference to the parent.

From Repeater:


private RepeaterItem CreateItem(int itemIndex, ListItemType itemType, bool
dataBind, object dataItem)
{
RepeaterItem item = this.CreateItem(itemIndex, itemType);
RepeaterItemEventArgs e = new RepeaterItemEventArgs(item);
this.InitializeItem(item);
if (dataBind)
{
item.DataItem = dataItem;
}
this.OnItemCreated(e); // Fires the event
this.Controls.Add(item); // Adds it to the repeater and sets the parent
if (dataBind)
{
item.DataBind();
this.OnItemDataBound(e);
item.DataItem = null;
}
return item;
}

protected virtual void OnItemCreated(RepeaterItemEventArgs e)
{
RepeaterItemEventHandler handler = (RepeaterItemEventHandler) base.Events[EventItemCreated];
if (handler != null)
{
handler(this, e); // This is added from the repeater control, so
it always is a reference to the parent object
}
}


This means you should be able to access the parent object by casting sender
to Repeater like so:

protected void testRepeater2_OnItemCreated(object sender, RepeaterItemEventArgs
e)
{
Repeater parent = sender as Repeater;
if (parent != null)
{
// use it...
}
}
 
R

Raj

Thanks Jesse, this information is really helpful for me

Jesse Houwing said:
Hello Raj,
// Binded the data for repeater2 control (child) in
testRepeater1_OnItemCreated

protected void testRepeater1_OnItemCreated(object sender,
RepeaterItemEventArgs e)

protected void testRepeater2_OnItemCreated(object sender,
RepeaterItemEventArgs e)
{
// trying to access the parent control value using
// e.Item.Parent but it is returning null
}

How to access the parent control value

Looking at Reflector, the Item is first created, then the event is raised
and then it is added to the repeater, so the parent will never be available
in the OnItemCreated call, aside from the object sender, which will contain
a reference to the parent.

From Repeater:


private RepeaterItem CreateItem(int itemIndex, ListItemType itemType, bool
dataBind, object dataItem)
{
RepeaterItem item = this.CreateItem(itemIndex, itemType);
RepeaterItemEventArgs e = new RepeaterItemEventArgs(item);
this.InitializeItem(item);
if (dataBind)
{
item.DataItem = dataItem;
}
this.OnItemCreated(e); // Fires the event
this.Controls.Add(item); // Adds it to the repeater and sets the parent
if (dataBind)
{
item.DataBind();
this.OnItemDataBound(e);
item.DataItem = null;
}
return item;
}

protected virtual void OnItemCreated(RepeaterItemEventArgs e)
{
RepeaterItemEventHandler handler = (RepeaterItemEventHandler) base.Events[EventItemCreated];
if (handler != null)
{
handler(this, e); // This is added from the repeater control, so
it always is a reference to the parent object
}
}


This means you should be able to access the parent object by casting sender
to Repeater like so:

protected void testRepeater2_OnItemCreated(object sender, RepeaterItemEventArgs
e)
{
Repeater parent = sender as Repeater;
if (parent != null)
{
// use it...
}
}
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top