Bind a repeater dynamically inside another repeater? - c#

I have this code:
<asp:Repeater id="repeaterCategories" runat="server">
<ItemTemplate>
<div class="categorie-item">
...
<asp:Repeater id="repeaterSubCategories" runat="server">
<ItemTemplate>
...
</ItemTemplate>
</asp:Repeater>
</div>
</ItemTemplate>
</asp:Repeater>
and repeaterSubCategories must be repeaterCategories.SubCategories, for each repeaterCategories. So I have to bind dynamically (for each first repeater iteration) a list of sub categories.
Can I do it? How?

If you have a nested repeater like this:
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<asp:Repeater ID="Repeater2" runat="server"></asp:Repeater>
</ItemTemplate>
</asp:Repeater>
You can use this to bind to it:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
var data = ((MyClass)e.Item.DataItem).Subcategories;
var repeater2 = (Repeater)e.Item.FindControl("Repeater2");
repeater2.DataSource = data;
repeater2.DataBind();
}

Related

Repeater does not appear

the following code should show the list of websites in a repeater but it does not (though I get no errors):
In the aspx file I have:
<div>
<asp:Repeater ID="Rpt1" Visible="true" runat="server"></asp:Repeater>
<ItemTemplate>
<asp:HyperLink runat="server" Text = '<%# DataBinder.Eval(Container.GetDataItem(), "Key") %>'
NavigateUrl= '<%#DataBinder.Eval(Container.GetDataItem(), "Value") %>'>
</asp:Hyperlink>
</ItemTemplate>
</div>
In the code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack) return;
CaricaSiti();
}
protected void CaricaSiti()
{
ListDictionary L = new ListDictionary();
L.Add("google", #"http://google.com");
L.Add("microsoft", #"http://microsoft.com");
L.Add("yahoo", #"http://yahoo.com");
Rpt1.DataSource = L;
Rpt1.DataBind();
}
The repeater's ItemTemplate must be inside of the repeater :-)
<asp:Repeater ID="Rpt1" Visible="true" runat="server">
<ItemTemplate>
<asp:HyperLink runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Key") %>'
NavigateUrl= '<%#DataBinder.Eval(Container.DataItem, "Value") %>'>
</asp:Hyperlink>
</ItemTemplate>
</asp:Repeater>

C# ASP.NET How to insert Eval value Repeater from Code-Behind

In the repeater Design, I have something like this:
Text='<%# Eval("deposit") %>'
How do I insert the value from code-behind to "deposit" not using dataSource but by manually inserting string value?
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Label ID="lblDeposit" runat="server" Text='<%# Eval("deposit") %>' />
</ItemTemplate>
</asp:Repeater>
You can use ItemDataBound event, to bind field values manually. Like:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
Label lbl = e.Item.FindControl("lblDeposit") as Label;
lbl.Text = "You Manual String Value";
}
}
Attach this event to repeater in html markup:
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">

How to get Full row from repeater control in code behind?

I am using repeater control to create a table with few rows and columns. On Repeater_ItemCommand I want to select clicked row in the code behind and store it in the session. How do I do this ?
When I click the row, my e.Item.DataItem is coming NULL. I am using <%# DataBinder.Eval(Container.DataItem, "FILE_NAME")%> to bind my values in asp.net
I cant use LINQ.
Thanks
Ved
Here is the code for the repeater
<asp:Repeater ID="Repeater1" runat="server"
onitemcommand="Repeater1_ItemCommand">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<asp:LinkButton ID="btnDeleteComment" runat="server" Text="Delete" CommandName="DeleteComment" CommandArgument=<%#Eval("myId") %>></asp:LinkButton>
<asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "FileName")%>'></asp:Label>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
Here is the code for the code behind
public partial class _Default : System.Web.UI.Page
{
public class myObject
{
public string FileName { get; set; }
public int myId { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
List<myObject> myList = new List<myObject>();
myList.Add(new myObject {myId = 1, FileName = "one" });
myList.Add(new myObject { myId = 2, FileName = "two" });
myList.Add(new myObject { myId = 3, FileName = "three" });
Repeater1.DataSource = myList;
Repeater1.DataBind();
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
Label item = (Label)e.Item.FindControl("label1");
}
}
From what i know about Repeater, it is not supposed to be used like that. Why not just use GridView and TemplateField?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="KeyColumnName"
DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField>
...Same template as in repater...
</asp:TemplateField>
</Columns>
</asp:GridView>
You can use Buttons'/LinkButtons'/etc. CommandArgument field.
<table>
<asp:Repeater ID="someRepeater" OnItemCommand="someRepeater_ItemCommand" runat="server">
<HeaderTemplate>
<tr><th>
File Name Header
</th></tr>
</HeaderTemplate>
<ItemTemplate>
<tr><td>
<asp:LinkButton Text="File Name Item" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "FILE_NAME")%>' Style="display: block" runat="server" />
</td></tr>
</ItemTemplate>
</asp:Repeater>
</table>
In code behind:
protected void someRepeater_ItemCommand(object Sender, RepeaterCommandEventArgs e)
{
Session["FILE_NAME"] = e.CommandArgument; //Here you have your FILE_NAME
}
This is, of course, some example code.

bind data to repeater inside a gridview

Hi have a repeater which is inside a gridview. when i bind the data to gridview the data is binding to the controles inside the gridview but repeater is not binding.
<asp:GridView ID="gvMain" runat="server" AllowPaging="false" AutoGenerateColumns="false"
Width="200px" Height="200px"
onrowdatabound="gvMain_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtnDptName" runat="server" Text='<%# Eval("deptName")%>'></asp:LinkButton>
<asp:Label ID="lblDptDesc" runat="server" Text = "sdfsdfsdfdsf"></asp:Label>
<asp:Repeater ID="rtFunctions" runat="server" OnItemDataBound="rtFunctions_ItemDataBound" >
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID="lbtnFunctions" runat="server" ></asp:LinkButton>
<asp:Label ID="lbltemp" Style="border:1px solid blue;width:20px;height:20px;background:green" runat="server" Text="TempLabel" ></asp:Label>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
in Page load:
gvMain.DataSource = objDeptColl;
gvMain.DataBind();
Codebehind for repeater:
protected void gvMain_RowDataBound(object sender, GridViewRowEventArgs e)
{
FunctionCollection objTempFuncColl = new FunctionCollection();
objTempFuncColl = (FunctionCollection)Cache["objFuncColl"];
Repeater rt = (Repeater)e.Row.FindControl("rtFunctions");
if (e.Row.RowType == DataControlRowType.DataRow && objTempFuncColl.Count !=0 )
{
rt.DataSource = objTempFuncColl;
rt.DataBind();
}
}
protected void rtFunctions_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
FunctionCollection objTempFuncColl = new FunctionCollection();
objTempFuncColl = (FunctionCollection)Cache["objFuncColl"];
Repeater rt = (Repeater)e.Item.FindControl("rtFunctions");
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
foreach (Functions f in objTempFuncColl)
{
LinkButton lnk = (LinkButton)e.Item.FindControl("lbtnFunctions");
lnk.Text = f.funcName;
}
}
}
linkbutton in gridview is binding but the linkbutton in repeater is not binding.
The problem appears to be with your repeater ondatabound function.
FunctionCollection objTempFuncColl = new FunctionCollection();
objTempFuncColl = (FunctionCollection)Cache["objFuncColl"];
The first line is not needed as you then replace it with the contents of the cache, which might be null if it's expired or purged or an instance.
For each row in your repeater, the link will be set to the last value in the objtempfunccoll.
you don't really need any of the function apart from lnk.Text = f.funcName; (you'll need to cast f from dataitem)
When you databind to the gridview, ondatabound is called for each row. you've got that wired-up. For each row you now need to find the repeater, set its datasource (we'll call this inner collection) & call databind on the repeater. this will cause ondatabound on the repeater to be called but the container.dataitem now points to each item in the inner collection. We can use that directly, casting container.dataitem to whatever type the inner collection is a list of.
protected void gvMain_RowDataBound(object sender, GridViewRowEventArgs e)
{
FunctionCollection objTempFuncColl = (FunctionCollection)Cache["objFuncColl"];
Repeater rt = (Repeater)e.Row.FindControl("rtFunctions");
if (e.Row.RowType == DataControlRowType.DataRow && objTempFuncColl.Count !=0 )
{
rt.DataSource = objTempFuncColl;
rt.DataBind();
}
}
protected void rtFunctions_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
lnk.Text = ((Functions)e.Item.DataItem).funcName;
}
Simon
You do not appear to be binding the repeater within any of this code. You may have some code to bind data to the GridView control, but this will not automatically bind anything to the repeater within the ItemTemplate.
Why don't databind in the aspx, leaving empty the code behind:
<asp:GridView ID="gvMain" runat="server" AllowPaging="false" AutoGenerateColumns="false"
Width="200px" Height="200px">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtnDptName" runat="server" Text='<%# Eval("deptName")%>'></asp:LinkButton>
<asp:Label ID="lblDptDesc" runat="server" Text = "sdfsdfsdfdsf"></asp:Label>
<asp:Repeater ID="rtFunctions" runat="server" DataSource='<%# Cache["objFuncColl"] %>' >
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID="lbtnFunctions" runat="server" Text='<%# Eval("funcName") %>' ></asp:LinkButton>
<asp:Label ID="lbltemp" Style="border:1px solid blue;width:20px;height:20px;background:green" runat="server" Text="TempLabel" ></asp:Label>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Access a control inside a the LayoutTemplate of a ListView

How do I access a Control in the LayoutTemplate of a ListView control?
I need to get to litControlTitle and set its Text attribute.
<asp:ListView ID="lv" runat="server">
<LayoutTemplate>
<asp:Literal ID="litControlTitle" runat="server" />
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
Any thoughts? Perhaps via the OnLayoutCreated event?
Try this:
((Literal)lv.FindControl("litControlTitle")).Text = "Your text";
The complete solution:
<asp:ListView ID="lv" OnLayoutCreated="OnLayoutCreated" runat="server">
<LayoutTemplate>
<asp:Literal ID="lt_Title" runat="server" />
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
In codebehind:
protected void OnLayoutCreated(object sender, EventArgs e)
{
(lv.FindControl("lt_Title") as Literal).Text = "Your text";
}
This technique works for template layout; use the init event of the control:
<asp:ListView ID="lv" runat="server" OnDataBound="lv_DataBound">
<LayoutTemplate>
<asp:Literal ID="litControlTitle" runat="server" OnInit="litControlTitle_Init" />
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
And capture a reference to the control for use in the code-behind (e.g) in the ListView's DataBound event:
private Literal litControlTitle;
protected void litControlTitle_Init(object sender, EventArgs e)
{
litControlTitle = (Literal) sender;
}
protected void lv_DataBound(object sender, EventArgs e)
{
litControlTitle.Text = "Title...";
}
For Nested LV Loop:
void lvSecondLevel_LayoutCreated(object sender, EventArgs e)
{
Literal litText = lvFirstLevel.FindControl("lvSecondLevel").FindControl("litText") as Literal;
litMainMenuText.Text = "This is test";
}
In case you need the VB version, here it is
Dim litControl = CType(lv.FindControl("litControlTitle"), Literal)
litControl.Text = "your text"

Categories

Resources