Difficulty in displaying data using repeater - asp.net

I am trying to display shopping categories with its sub categories and sub-sub categories using repeater.. Data is binding but it is not being displayed.. Can anyone help why?
here's my code:
.aspx file
<asp:Repeater ID="CategoryRepeater" runat="server" OnItemDataBound="CategoryRepeater_OnItemDataBound">
<ItemTemplate>
<a href='Clothing.aspx?CategoryId=<%#Eval("CategoryId") %>'<%#Eval("CategoryName") %>></a><br />
<asp:Repeater ID="SubCategoryRepeater" runat="server" OnItemDataBound="SubCategoryRepeater_OnItemDataBound">
<ItemTemplate>
<a href='Clothing.aspx?CategoryId=<%#Eval("CategoryId") %>&SubCategoryId=<%#Eval("SubCategoryId") %>'<%#Eval("SubCategoryName") %>></a><br />
<asp:Repeater ID="SubSubCategoryRepeater" runat="server">
<ItemTemplate>
<a href='Clothing.aspx?CategoryId=<%#Eval("CategoryId") %>&SubCategoryId=<%#Eval("SubCategoryId") %>&SubSubCategoryId=<%#Eval("SubSubCategoryId") %>'<%#Eval("SubSubCategoryName") %>></a><br />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
C# Code:
protected void CategoryRepeater_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item)||(e.Item.ItemType == ListItemType.AlternatingItem))
{
DataRowView dataItem = e.Item.DataItem as DataRowView;
int categoryId = Convert.ToInt32(dataItem["CategoryId"]);
Repeater rp = (Repeater)e.Item.FindControl("SubCategoryRepeater");
ds = us.SelectSubCategories(categoryId);
rp.DataSource = ds;
rp.DataBind();
}
}
protected void SubCategoryRepeater_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item)||(e.Item.ItemType == ListItemType.AlternatingItem))
{
DataRowView dataItem = e.Item.DataItem as DataRowView;
int SubCategoryId = Convert.ToInt32(dataItem["SubCategoryId"]);
Repeater rp1 = (Repeater)e.Item.FindControl("SubSubCategoryRepeater");
ds1 = us.SelectSubSubCategories(SubCategoryId);
rp1.DataSource = ds1;
rp1.DataBind();
}
}

Check your source code to make sure nothing is being output to the page. I think you just have the category name being rendered inside the anchor tag.
Here is the same code with the evals simplified to see what is going on
<a href='Clothing.aspx?CategoryId={catid}'{name}></a>
Should be:
<a href='Clothing.aspx?CategoryId={catid}'>{name}</a>
or
<a href='Clothing.aspx?CategoryId=<%#Eval("CategoryId") %>'><%#Eval("CategoryName") %></a>
Same mistake was done in all three locations.

I think everything is correct from the code, but your output is wrong.
You are doing this:
<a href='Clothing.aspx?CategoryId=<%#Eval("CategoryId") %>&SubCategoryId=<%#Eval("SubCategoryId") %>'<%#Eval("SubCategoryName") %>></a><br />
But it needs to be
<a href='Clothing.aspx?CategoryId=<%#Eval("CategoryId") %>&SubCategoryId=<%#Eval("SubCategoryId") %>'><%#Eval("SubCategoryName") %></a><br />
Noticed I moved the '>' back behind <%# Eval("SubCategoryName")%>

Related

How to change the css of a dynamically created link button in repeater

I am loading link button in in a repeater. i want to change the particular link button css class when the user clicks it.
<asp:Repeater ID="moviedaterepeater" runat="server">
<ItemTemplate>
<li>
<asp:LinkButton ID="theatrelinkbutton" runat="server" Text='<%#Eval("datetext") %>' CausesValidation="false" CommandName='<%#Eval("datevalue") %>' CommandArgument='<%#Eval("datetext") %>' OnCommand="moviedate_Command"></asp:LinkButton>
</li>
</ItemTemplate>
</asp:Repeater>
how to do it i tried with the itemdatabound
protected void moviedaterepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
//Session["linkbuttonid"] = "ctl00_ContentPlaceHolder1_moviedaterepeater_ctl01_theatrelinkbutton";
////Label lbl = (Label)e.Item.FindControl("Label1");
//LinkButton link = (LinkButton)e.Item.FindControl(Session["linkbuttonid"].ToString());
//link.CssClass = "active";
}
}
but i want to change the value based on the text of the linkbutton .
First change your repeater to this:
<asp:Repeater ID="moviedaterepeater" runat="server" OnItemCommand="moviedaterepeater_ItemCommand">
Then in code behind:
protected void moviedaterepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
LinkButton lnk = (LinkButton)e.Item.FindControl("theatrelinkbutton");
lnk.CssClass = "YourCSSClass";
}
Try this:
LinkButton theatrelinkbutton = (LinkButton)e.Item.FindControl("theatrelinkbutton");
theatrelinkbutton.CssClass = theatrelinkbutton.Text;
You need to add this in ItemDataBound event of your repeater control. Please note you should have this event binded:-
<asp:Repeater ID="moviedaterepeater" runat="server" OnItemDataBound="moviedaterepeater_ItemDataBound">

Number of items in result in ASP repeater with Sitecore

I'm working with Sitecore ascx file, and got something like this:
<asp:Repeater ID="NavRepeater" runat="server">
<ItemTemplate>
<div class="item">
<img src="<%# ((Sitecore.Data.Items.Item)Container.DataItem).Fields["Image Url"] %>" width="360" height="420" alt="">
</div>
</ItemTemplate>
<SeparatorTemplate></SeparatorTemplate>
</asp:Repeater>
Can't figure out, how to get total number of elements from Sitecore.Data.Items.Item.
I was also wondering how to retrieve given item in collection - like item no 2 ?
It's on the data source of your repeater in C#:
So if your C# is:
NavRepeater.DataSource = someData;
Then the length is someData.Length or someData.Count() based on its type.
For getting a specific item at a location:
Item second = someData[1]; // per 0-indexing
Or based on type, can also be:
Item second = someData.ElementAt(1) // per 0-indexing
You can have something like :
<asp:Repeater ID="NavRepeater" runat="server" OnItemDataBound="rptGallery_ItemDataBound">
<ItemTemplate>
<div class="item">
<sc:fieldrenderer id="fldGalleryItemImageThumbnail" runat="server" fieldname="ImageUrl" disablewebediting="true" />
</div>
</ItemTemplate>
<SeparatorTemplate></SeparatorTemplate>
</asp:Repeater>
On Page_Load you will have:
protected void Page_Load(object sender, EventArgs e)
{
rptGallery.DataSource = listofitemyyouwanttoshow;
rptGallery.DataBind();
}
On rptGallery_ItemDataBound event you will have:
protected void rptGallery_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Item item = (Item)e.Item.DataItem;
//you can check index here of item
if (item != null)
{
var fldGalleryItemImageThumbnail = e.Item.FindControl("fldGalleryItemImageThumbnail") as FieldRenderer;
if (fldGalleryItemImageThumbnail != null)
{
fldGalleryItemImageThumbnail.DataSource = item.ID.ToString();
fldGalleryItemImageThumbnail.Parameters = String.Format("width={0}", 360);
}
}
}
}
}
I hope it helps you .
You would get these from the enumerable that you are binding you repeater to.

how to find repeater inside another repeater

I want to find repeater inside the another repeater. But i m not able to find. My code is
<asp:Repeater ID="rep_test" runat="server">
<ItemTemplate>
<div id='h<%# DataBinder.Eval(Container, "ItemIndex") %>' class="header" onclick='ToggleDisplay(<%# DataBinder.Eval(Container, "ItemIndex") %>);'>
<%#DataBinder.Eval(Container.DataItem, "ID")%>
</div>
<div id='d<%# DataBinder.Eval(Container, "ItemIndex") %>' class="details">
<asp:Repeater ID="rep_hello" runat="server">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "batchid")%><br />
<%#DataBinder.Eval(Container.DataItem, "ts")%><br />
</ItemTemplate>
</asp:Repeater>
<%-- <%#DataBinder.Eval(Container.DataItem, "batchid")%><br />
<%#DataBinder.Eval(Container.DataItem, "ts")%><br />--%>
</div>
</ItemTemplate>
</asp:Repeater>
If you put a repeater inside an item template of another repeater that means that every item of the main repeater (rep_test) will have a repeater inside it (rep_hello). So you actually need to find the repeater inside a repeaterItem. You can iterate trough all the nested repeaters like this:
foreach (RepeaterItem item in rep_test)
Repeater rptr = (Repeater)item.FindControl("rep_hello");
Example:
In ItemDataBound event handler:
protected void rep_test_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
(e.Item.FindControl("rep_hello") as Repeater).DataSource = YourOtherDataSource;
}
}
You can try using .FindControl(). In VB, it would be something like
Dim rpt as Repeater = rep_test.FindControl("rep_hello")
Usually when I see this kind of thing, you want to perform some event on all of the inner repeaters. What I usually do is handle this kind of thing inside the ItemDataBound event.
Add an OnItemDataBound attribute to your Repeater.
<asp:Repeater ID="rep_test" runat="server"
OnItemDataBound="rep_test_ItemDataBound">
Then in the back end, add a handler, with a FindControl call.
protected void rptBasket_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater innerRepeater = (Repeater)e.Item.FindControl("rep_hello");
// Now your have your repeater...do what you want with it.
}
}

Loading repeater when binding gridview in asp.net

I have a gridview like this:
<asp:GridView ID="gv1" AutoGenerateColumns="false" BorderWidth="0" runat="server" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<span style="font-family:Tahoma; font-size:14px;">
<u> <a href="<%#DataBinder.Eval(Container.DataItem,"ShUrl")%>">
<%#DataBinder.Eval(Container.DataItem,"PostTitle")%>
</a>
</u>
<br />
</span>
<asp:Repeater ID="rp1" runat="server">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li >
<%# Eval("TagName")%>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Now I am able to load successfull ShUrl and PostTitle. I am bringing titleId from the database also.
Now as usual, a post might have several tag. So I want to load the repeater for the particular titleId.
On server side, I am simply binding the gv1 from the datable.
Now how to load tags for the titleid:
I have already written the function on the server side, might help you all to guide me:
private void LoadtagList(int titleId)
{
// calling DAL
rp1.DataSource = db.GetAllTagsForPost(titleId);
rp1.DataBind();
}
you have to use GridView RowDataBound Event for that
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
System.Data.DataRowView dr = (System.Data.DataRowView)e.Row.DataItem;
if (Convert.ToString(dr["titleId"]) != "")
{
Repeater rp1 = (Repeater)e.Row.Findcontrol("rp1");
rp1.DataSource = db.GetAllTagsForPost(titleId);
rp1.DataBind();
}
}
}

Change an ASP.NET control's properties in a Repeater control

My question is fairly simple. This is what I have for the aspx page:
<ul>
<asp:Repeater runat="server" ID="linksList" OnItemDataBound="linksList_OnItemDataBound" >
<ItemTemplate>
<li><asp:HyperLink runat="server" ID="link" /></li>
</ItemTemplate>
</asp:Repeater>
</ul>
I'm trying to get a list of hyperlinks from a SQL server into a list. This is what I have in the codebehind:
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = Utilities.RunSelectQuery("SELECT *");
DataTable dt = ds.Tables[0];
linksList.DataSource = dt;
linksList.DataBind();
}
How do I change the NavigateUrl and Text properties in the asp:HyperLink after data's been bound to the Repeater? I want to do this in the codebehind, I can get it to work if I do it using <%# Eval("URL") %> in the aspx page but that's sort of against what ASP.NET is all about.
Edit: this is the solution that worked for me thanks to womp:
protected void linksList_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView row = (DataRowView)e.Item.DataItem;
HyperLink link = (HyperLink)e.Item.FindControl("link");
link.Text = row["description"].ToString();
link.NavigateUrl = row["URL"].ToString();
}
}
Actually, using the Databinder syntax in your templates is a great way to do it, I'm not sure what you mean that it's "against what ASP.Net is all about".
However, if you really want to do it in code, you can do it right in your OnItemDataBound handler (which it looks like you've created). Something like this (read: untested) should do the trick:
void linksList_OnItemDataBound(object Sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item
|| e.Item.ItemType == ListItemType.AlternatingItem) {
DataRow row = e.Item.DataItem as DataRow;
Hyperlink link = e.Item.FindControl("link"));
link.Text = row["URL"];
link.NavigateUrl = row["URL"];
}
}
}
The way to do it is just as you said. Doing it in the code behind adds unnecessary work if you only wish to display the URL. You also would want to put the tags in the HeaderTemplate and FooterTemplate.
<asp:Repeater runat="server" ID="linksList" OnItemDataBound="linksList_OnItemDataBound" >
<HeaderTemplate><ul></HeaderTemplate>
<ItemTemplate>
<li><asp:HyperLink runat="server" ID="link" NavigateUrl='<%# Eval("url") %>' /></li>
</ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>

Resources