I have a case of nested repeaters where a child repeater is nested in the ItemTemplate of a parent repeater. The DataSource of the parent is a Dictionary<String, List<XYZ>>.
In the ItemDataBound of the parent Repeater I am using the full code:
protected void rptParent_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (e.Item.DataItem is KeyValuePair<String, List<Object>>)
{
pair = (KeyValuePair<String, List<XYZ>>)e.Item.DataItem;
}
Repeater childRepeater = e.Item.FindControl("rptChild") as Repeater;
//bind the child repeater.
childRepeater.ItemDataBound += new RepeaterItemEventHandler(childRepeater_ItemDataBound);
childRepeater.DataSource = pair.Value;
childRepeater.DataBind();
}
}
protected void childRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
//Access the Parent row's Key value
}
}
There are 2 questions:
Can I use a hidden field while binding the parent and set it to the Key value and then retrieve the hidden field value in the child?
Will the order of the events firing be as follows:
a. Parent_ItemDataBound for Row 1 of Dictionary<Key, List<XYZ>>
i. Child_ItemDataBound for each child row of Row 1 of parent repeater
ii. Child_ItemDataBound for Footer of child repeater
b. Parent_ItemDataBound for Row 2 of Dictionary<Key, List<XYZ>>
i. Child_ItemDataBound for each child row of Row 2 of parent repeater
ii. Child_ItemDataBound for Footer of child repeater
and so on. In other words, will the Parent_ItemDataBound be followed by Child_ItemDataBound events for each child row - with the cycle repeating?
Here are the answers to your question:
Yes, you can access parent repeater item control
var hfID = e.Item.NamingContainer.NamingContainer.FindControl("hfID") as HiddenField;
the order is correct.
Hope that helps
Related
I want to find the Parent Repeater, which contains Child Repeater and Child Repeater contains dropdownlist. On SelectedIndexChange of the Drowndownlist I want to find out the Parent Repeater. After finding the parent repeater, I want to find the hiddenfield value inside Parent Repeater. i.e.
Parent Repeater Contains HiddenField and Child Repeater
Child Repeater contains Dropdownlist on this dropdown selected index change event I want to find HiddenField value which is in Parent Repeater.
My Code:
DropDownList myGeneralButton = (DropDownList)sender;
Repeater item = (Repeater)myGeneralButton.Parent.Parent;
for (int i = 0; i < item.Items.Count; ++i)
{
HiddenField hdn= item.Items[i].FindControl("Hdhotelname") as HiddenField;
string h = hdn.Value;
}
In this hidden field I am getting all the values, but I want a value of that particular index where I am selecting selecting the dropdown.
Thanks
You have to search through the DropDownList's NamingContainer. The flow should be like this:
(DropDownList)sender
--> NamingContainer(Child RepeaterItem)
--> NamingContainer(Child Repeater)
--> NamingContainer(Parent RepeaterItem)
--> FindControl"Hdhotelname" (Hdhotelname)
and your code should be like this:
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
var ddl = (DropDownList)sender;
var rptChild = ddl.NamingContainer.NamingContainer;//Child Repeater
if (rptChild != null)
{
var rptParentItem = rptChild.NamingContainer;//Parent RepeaterItem
var hdnfld = rptParentItem.FindControl("Hdhotelname") as HiddenField;
if (hdnfld != null)
{
//Do your tasks
}
}
}
Hope it helps!
<%= (Repeater)ChildRepeater.NamingContainer =>
or use it directly in your code-behind without the <%= and =>
I have one dropdownlist and one button and listview. Button and dropdownlist is outside listview.
On 1st page load, all the item is displayed. Now user can see based on selection of dropdownlist.
I want to show selected text of dropdownlist in lable of layout control of listview.
I wrote below code but doesn't seem to work correctly.
protected void lvProduct_LayoutCreated(object sender, EventArgs e)
{
if (dlCategory.SelectedIndex != -1)
{
string Category= dlCategory.SelectedItem.Text.ToString();
(lvProduct.FindControl("lblCategory") as Label).Text = "Category: " + dlCategory.SelectedItem.Text.ToString();
}
else
{
(lvProduct.FindControl("lblCategory") as Label).Text = "All category";
}
}
How can I create divs dynamically using placeholder in c#? I have declared placeholder inside of a repeater.
Is it possible to create controls dynamically in placeholder?
here you go:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
PlaceHolder pl = e.Item.FindControl("PlaceHolder1") as PlaceHolder;
if (pl != null)
{
HtmlGenericControl div1 = new HtmlGenericControl("div");
div1.InnerHtml = "Hello";
pl.Controls.Add(div1);
}
}
}
To dynamically add a div to a PlaceHolder control within a Repeater control, you need to use the ItemDataBound event of the Repeater.
Within the method for ItemDataBound you will need to find the PlaceHolder control. Checking first if the ItemType is ListItemType.Item or ListItemType.AlternatingItem first.
PlaceHolder ph = (PlaceHolder) e.Item.FindControl("PlaceHolderControl1");
Then you can add the div as MUG4N has suggested.
HtmlGenericControl div1 = new HtmlGenericControl("div");
div1.InnerHtml = "Hello";
div1.Attributes.Add("class", "classname");
div1.Attributes.Add("style", "top: 10px; left: 20px;");
ph.Controls.Add(div1);
To add another div, just repeat from div1 = new HtmlGenericControl("div"); down to ph.Controls.Add(div1);
Alternatively, you can add a <asp:Panel> control as this renders as a <div> on the page but is more ".net"
Panel panel1 = new Panel();
Then add a <asp:Literal> control to this.
Literal literal1 = new Literal() { Text = "Hello" };
panel1.Controls.Add(literal1);
ph.Controls.Add(panel1);
I am adding rows dynamically on code behind depending on the Row currently bounded on RowDataBound event. I want that added row to be the same state (Alternate or Normal) of the currently bounded row is this possible?
I'm doing something like this but it doesn't follow what I want. I'm expecting the added row dynamically would be the same as the current row. But it is not.
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
GVData data = e.Row.DataItem as GVData; // not original object just for brevity
if(data.AddHiddenRow)
{
// the row state here should be the same as the current
GridViewRow tr = new GridViewRow(e.Row.RowIndex +1, e.Row.RowIndex + 1, DataControlRowType.DataRow, e.Row.RowState);
TableCell newTableCell = new TableCell();
newTableCell.Style.Add("display", "none");
tr.Cells.Add(newTableCell);
((Table)e.Row.Parent).Rows.Add(tr);
}
}
}
Ok the issue is that the GridView doesnt recognise display:none as meaning that this row will be hidden and so counts it as part of its alternating style. In this instance what you need to do is implement the styling explicitly yourself and remove it from the gridview definiton.
I will add a semi-example shortly have a meeting.
I need to aske I have gridview with combobox template column and its fill with items .
when select index changed i need to set description value in onther cell but I couldn't get the index of the gridview row?
any help
It's a little unclear which SelectedIndexChanges you are talking about, but I assume you think about the combobox SelectedIndexChanged.
Assuming you have hooked up the eventhandler on the combobox, you can use the following code to get the RowIndex inside the handler
protected void cmb_SelectedIndexChanged(object sender, EventArgs e)
{
int idx = (((sender as System.Web.UI.WebControls.DropDownList).Parent.Parent as GridViewRow)).RowIndex;
}
Parent of the dropdownlist will be a DataControlField and the parent if it will be the GridRow.
This also assumes you have no other container controls inside your templatefield, as the parent.parent structure then could be different.