Find control in Nested listview - c#

I'm trying to find controls in a nested listview in a parent listview on postback.
if (Page.IsPostBack)
{
ListView ChildLV = (ListView)(LVParent.FindControl("ChildLV"));
foreach (ListViewItem item in ChildLV.Items)
{
item.FindControl("NestListViewChildControl");
}
}

You have to loop the items in the parent ListView and use FindControl in each of those to locate the nested ListView.
if (Page.IsPostBack)
{
foreach (ListViewItem item in LVParent.Items)
{
ListView ChildLV = item.FindControl("ChildLV") as ListView;
}
}
aspx
<asp:ListView ID="LVParent" runat="server">
<ItemTemplate>
<asp:ListView ID="ChildLV" runat="server">
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:ListView>

Related

ASP.NET Index of parent repeater item (in nested repeaters)

I have a nested repeaters, i.e. a parent repeater and a child repeater. The child repeater contains only one DropDownList control. I have OnSelectedIndexChanged setup on DropDownList control. I can get the index of the child repeater item when the drop down list's selection is changed.
My question is: How can I get the index of the parent repeater in which the drop down list's selection was changed.
Here is the sample code:
<asp:Repeater runat="server" ID="ParentRepeater">
<ItemTemplate>
<asp:Repeater runat="server" ID="ChildRepeater">
<ItemTemplate>
<asp:DropDownList runat="server" ID="DropDownInChildRepeater" OnSelectedIndexChanged="DropDownInChildRepeater_OnSelectedIndexChanged" />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
protected void DropDownInChildRepeater_OnSelectedIndexChanged(object sender, EventArgs e)
{
var dropDownInChildRepeater = (DropDownList)sender;
var dropDownInChildRepeaterItem = (RepeaterItem)dropDownInChildRepeater.NamingContainer;
var indexOfDropDownInChildRepaterItem = dropDownInChildRepeater.ItemIndex;
//Question I need index of ParentRepeater in which sender resides
}
You were on the right track with the NaminContainer. You just need to go up the control tree. DropDownList > RepeaterItem > Repeater > RepeaterItem.
//dropdown in child repeater
var dropDownInChildRepeater = (DropDownList)sender;
//repeater item in child repeater
var dropDownInChildRepeaterItem = (RepeaterItem)dropDownInChildRepeater.NamingContainer;
//child repeater
var childRepeater = (Repeater)dropDownInChildRepeaterItem.NamingContainer;
//repeateritem of parent repeater
var parentRepeaterItem = (RepeaterItem)childRepeater.NamingContainer;
//the item index of the parent repeateritem
var parentRepeaterItemIndex = parentRepeaterItem.ItemIndex;

How to get duplicate ID ASP elements from codebehind C#

My code:
<asp:DataList ID="datalist" runat="server" >
<ItemTemplate>
<asp:Textbox ID="Values" runat="server" type="text" />
</ItemTemplate>
</asp:DataList>
<asp:Button ID="Button1" runat="server" Text="SEND" OnClick="send" />
How could I get each Values ID elements of the DataList from code behind in C# ?
You loop all the items in the DataList and use FindControl to locate the TextBox.
protected void send(object sender, EventArgs e)
{
//loop all the items in the datalist
foreach (DataListItem item in datalist.Items)
{
//find the textbox with findcontrol
TextBox tb = item.FindControl("Values") as TextBox;
//do something with the textbox content
string value = tb.Text;
}
}

FindControl GridTemplateColumn Outside of radGrid Events

I have my own method and I am trying to findcontrol on a control inside the GridTemplateColumn, so I am doing it outside of the events for the radGrid. Is this possible and if so, how?
Thanks!
Try with below code.
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:Label ID="Label1"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1"></asp:TextBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>
...................
button1_click()
{
// for Normal mode
foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
{
Label Label1 = item.FindControl("Label1") as Label;
}
// for edit mode
foreach (GridDataItem item in RadGrid1.EditItems)
{
TextBox TextBox1 = item.FindControl("TextBox") as TextBox;
}
}
Thanks,
Jayesh Goyani

Bind the datasource of a nested ListView to the parent's ListView datasource

I have triple-nested ListView controls on my asp.net page, each nested within another. I use the OnItemDataBound event in the 1st ListView to set the DataSource of the 2nd level ListView. The 3rd ListView is contained in the of the 2nd ListView. I want to assign the same DataSource to both the 2nd and 3rd level ListView datasource controls, but I cannot figure out how to access the 3rd level ListView in order to do that.
Here is some sample code to help visualize:
<asp:ListView id="level1" runat="server" OnItemDataBound="level1_ItemDataBound">
<layouttemplate>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder"></asp:PlaceHolder>
</layouttemplate>
<itemtemplate>
<asp:ListView id="level2" runat="server">
<layouttemplate>
<asp:ListView id="level3" runat="server">
<layouttemplate>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder"></asp:PlaceHolder>
</layouttemplate>
<itemtemplate>OUTPUT DATA FOR LEVEL 3</itemtemplate>
</asp:ListView>
</layouttemplate>
<itemtemplate>OUTPUT DATA FOR LEVEL 2</itemtemplate>
</asp:ListView>
OUTPUT DATA FOR LEVEL 1
</itemtemplate>
</asp:ListView>
The level1_ItemDataBound method finds the level2 control, casts it as a ListView, sets its DataSource and executes the DataBind. At this point I'm stuck trying to get Level3.DataSource to be set to the same as Level2.DataSource. Any help?
Before you call DataBind on the on the level2 listview, you should register an event handler on the level2's ItemDataBound event.
Some psuedo code:
protected void level1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
var listView2 = (ListView) e.Item.FindControl("level2");
listView2.ItemDataBound += level2_ItemDataBound;
listView2.DataSource = myDataSource;
listView2.DataBind();
}
protected void level2_ItemDataBound(object sender, ListViewItemEventArgs e)
{
var listView3 = (ListView) e.Item.FindControl("level3");
listView3.DataSource = myDataSource;
listView3.DataBind();
}

Limit the number of results in a Nested ASP.NET ListView

Similar to my other question:
I have a ListView bound to a Dictionary. Then I have a nested ListView for the dictionary's value's integers.
I need to limit the number of items bound to the nested list to something like 5, and show a more button in the template.
I can't find a way to get the more button to work, and to correctly limit the number at the same time. I have it working as one or the other right now.
Any ideas? Thanks!
UPDATE:
The markup looks something like this:
<asp:ListView runat="server" ID="MainListView" ItemPlaceholderID="PlaceHolder2">
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="PlaceHolder2" />
</LayoutTemplate>
<ItemTemplate>
<h1>My Main ListView - <%# Eval("Key") %></h1>
<asp:ListView runat="server" ID="NestedListView" ItemPlaceholderID="PlaceHolder3"
DataSource='<%# Eval("Value") %>' >
<LayoutTemplate>
<h2>One of many Nested ListViews</h2>
<asp:PlaceHolder runat="server" ID="PlaceHolder3" />
</LayoutTemplate>
<ItemTemplate>
<asp:LinkButton runat="server" ID="AnInteger" Text='<%# Eval("value") %>'></asp:LinkButton>
<br />
</ItemTemplate>
</asp:ListView>
<asp:LinkButton runat="server" ID="uxMoreIntegers" Text="More..." Visible="false" OnClick="uxMoreIntegers_Click"></asp:LinkButton>
</ItemTemplate>
</asp:ListView>
DataBind the main ListView anyway you want.
DataBind the nested ListView programmatically in the ItemDataBound event for the main ListView
Code:
protected void uxListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem item = (ListViewDataItem)e.Item;
// Get the bound object (KeyValuePair from the dictionary)
KeyValuePair<string, List<int>> nestedIntegerList = (KeyValuePair<string, List<int>>)item.DataItem;
// Get our nested ListView for this Item
ListView nestedListView = (ListView)e.Item.FindControl("uxNestedListView");
// Check the number of items
if (nestedIntegerList.Value.Count > 5)
{
// There are more items than we want to show, so show the "More..." button
LinkButton button = (LinkButton)item.FindControl("uxMore");
button.Visible = true;
}
// Bind the nestedListView to wahtever you want
nestedListView.DataSource = nestedIntegerList.Value.Take(5);
nestedListView.DataBind();
}
}
The Take method will return the first 5 items in your list, but won't modify the list itself. You can then simply check the number of items in the list to determine if the more button needs to be enabled.
someList.Take(5); //use these items in your ListView
moreButton.Enabled = (someList.Count > 5);

Categories