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

  • Thread starter Thread starter Raj
  • Start date Start date
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
 
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...
}
}
 
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...
}
}
 
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...
}
}
 
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...
}
}
 
Back
Top