Repeater Control binding categories to details - c#

I'm new to repeater controls and pretty unfamiliar with them. Can someone help get me on the right track. Im using c# asp.net. I need my repeater to populate a sidebar with category names. On category name click, a main content div will populate category item details.
Thank you
All i have so far is populating the table with a hardcoded pk
<div class="chartdisplay">
<div id="repeater">
<asp:Repeater ID="Repeater" runat="server">
<HeaderTemplate>
<table width="100%" style= "background-color:#cccccc; font-size: 16px">
<tr style="background-color:#00bfbf"> <th> Item #</th> <th> Item Description </th> <th>Pre Price</th> <th>Size</th> </tr>
</HeaderTemplate>
<ItemTemplate>
<tr> <td> <%#Eval("Item #") %></td>
<td> <%#Eval("Item Description") %></td>
<td> <%#Eval("Pre Price") %></td>
<td> <%#Eval("Size") %></td> </tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr style="background-color:#eeeeee">
<td> <%#Eval("Item #") %></td>
<td> <%#Eval("Item Description") %></td>
<td> <%#Eval("Pre Price") %></td>
<td> <%#Eval("Size") %></td
</tr>
</AlternatingItemTemplate>
<FooterTemplate> </Table> </FooterTemplate>
</asp:Repeater>
</div>
</div>
and side bar
<div class="productCategoriesLabel">
<asp:Repeater ID="rptrCategories" OnItemCommand="detailsCat" runat="server">
<ItemTemplate>
<tr><td><%#Eval("PromoGroup") %></td></tr>
</ItemTemplate>
</asp:Repeater>
</div>

This is how u can bind another Repeater on click.
<div class="productCategoriesLabel">
<asp:Repeater ID="rptrCategories" runat="server"
onitemcommand="rptrCategories_ItemCommand">
<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID="lbtnGroup" CommandName="cmd" runat="server" Text='<%#Eval("PromoGroup") %>'></asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</div>
protected void rptrCategories_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "cmd")
{
string Group = ((LinkButton)rptrCategories.Items[e.Item.ItemIndex].FindControl("lbtnGroup")).Text;
// DataTable dt = GroupDeatils(Group);
detRepeter.DataSource = dt;
detRepeter.DataBind();
}
}

Related

Find index value of repeater to open new webform and read data from SQL

How do I pass the ID value linked to a repeater row to open the values in a new webform with textfields which can then be edited and updated in repeater (SQL Stored procedure). I am new to repeaters and I managed to do this using a GridView but I'm not sure how to do it with a repeater.
What I did with the GridView c# which worked perfect in Default.aspx.cs:
if (dgvEmployees.SelectedIndex != -1)
{
indexId = dgvEmployees.SelectedRow.Cells[1].Text;
Response.Redirect("About.aspx?&Id=" + indexId);
}
What is received in About.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if(!String.IsNullOrEmpty(Request.QueryString["Id"]))
{
idNum = Convert.ToInt32(Request.QueryString["Id"]);
if(!IsPostBack)
{
FillEmployeeData();
}
}
}
This is the repeater code:
<asp:Repeater ID="rptList" runat="server" OnItemDataBound="rptList_ItemDataBound" Visible="true">
<HeaderTemplate>
<table class="table table-bordered" id="VersionsTable">
<tr>
<th>Actions
</th>
<th>Employee Number
</th>
<th>ID Number
</th>
<th>Employee Name
</th>
<th>Employee Surname
</th>
<th>Number of Dependants
</th>
<th>Race
</th>
<th>Gender
</th>
<th>Delete
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tbody>
<tr style="cursor:pointer" onmouseover="style.backgroundColor='#E6E6E6'" onmouseout="style.backgroundColor=''" onclick="style.backgroundColor='#727A91'">
<asp:HiddenField ID="hIDfield" runat="server" Value='<%#Eval("Id")%>' />
<td>
<asp:button runat="server" OnClick="btnEdit_Click" CssClass="btn btn-default" Text="Edit"/>
</td>
<td onclick="PopupEdit('<%# Eval("EmployeeNumber") %>')">
<%# Eval("EmployeeNumber") %>
</td>
<td>
<%# Eval("IDNumber") %>
</td>
<td>
<%# Eval("employeeName") %>
</td>
<td>
<%# Eval("employeeSurname") %>
</td>
<td>
<%# Eval("numberOfDependants") %>
</td>
<td>
<%# Eval("RaceId") %>
</td>
<td>
<%# Eval("GenderId") %>
</td>
<td style="text-align:center">
<asp:CheckBox ID="chkDelete" CssClass="form-control" runat="server" />
</td>
</tr>
</tbody>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Thank you for your help!

how to update shopping cart quantity?

how to update shopping cart quantity , i developed cartview page and when make update to all items,it take old value of quantity (binded) from textbox not new that i written it in textbox .
shopping cart code
public void SetItemQuantity(int productId, int quantity)
{
// If we are setting the quantity to 0, remove the item entirely
if (quantity == 0)
{
RemoveItem(productId);
return;
}
// Find the item and update the quantity
CartItem updatedItem = new CartItem(productId);
foreach (CartItem item in Items)
{
if (item.Equals(updatedItem))
{
item.Quantity = quantity;
return;
}
}
}
full shopping cart
<asp:ListView ID="FullCartListView" runat="server" DataKeyNames="ProductID" OnDataBound="FullCartListView_DataBound" OnItemCommand="FullCartListView_ItemCommand">
<EmptyDataTemplate>
<table class="table-bordered table table-cart">
<thead>
<tr>
<th>
No items in shopping cart .
</th>
</tr>
</thead>
</table>
</EmptyDataTemplate>
<ItemTemplate>
<tbody>
<tr>
<td class="cart-product">
<%--×--%>
<asp:LinkButton ID="RemoveLB" data-toggle="tooltip" CssClass="remove" data-original-title="Remove" runat="server" CommandName="Remove" CommandArgument='<%# Eval("ProductID")%>'>
×
</asp:LinkButton>
<a href="ProductDetails.aspx?ProductID=<%# Eval("ProductID") %>" title="" class="product-cart">
<img src="<%=ConfigurationManager.AppSettings["ProductsHTMLPath"].ToString() %><%# Eval("PictureName") %>" width="72" height="100" alt="">
<h3 class="product-title"><%# Eval("Name") %></h3>
</a>
</td>
<%-- <td>
<a href="#" data-toggle="tooltip" title="" class="edit-link" data-original-title="Edit">
<i class="fa fa-edit"></i>
</a>
</td>--%>
<td><span class="amount"><%# Eval("Price") %> </span></td>
<td><asp:TextBox ID="QuantityTextBox" CssClass="qty" runat="server" Text='<%# Eval("quantity") %>' />
<asp:Label runat="server" ID="ProductIDLabel" Text='<%# Eval("ProductID") %>' Visible="false" />
<%-- <br /><br /><asp:Button ID="UpdateCartButton" runat="server" CssClass="btn btn-dark btn-outline" CommandName="Update" CommandArgument="<%# Eval("ProductID") %>" Text="Update"></asp:Button>--%>
</td>
<td><span class="amount">
<asp:Label runat="server" ID="SubTotalPriceLabel" Text='<%# (decimal)Eval("quantity") * (decimal)Eval("Price") %>' />
</span></td>
</tr>
</tbody>
</ItemTemplate>
<LayoutTemplate>
<table id="itemPlaceholderContainer" class="table-bordered table table-cart">
<thead>
<tr>
<th>Product Name</th>
<%--<th style="width:10%"></th>--%>
<th style="width:10%">Unit</th>
<th style="width:10%">QTY</th>
<th style="width:10%">Subtotal</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</thead>
<tfoot>
<tr>
<td colspan="5">
<div class="text-right">
<%--Empty cart--%>
<asp:Button ID="EmptyCartButton" runat="server" CssClass="btn btn-dark btn-outline" Text="Empty cart" OnClick="EmptyCartButton_Click"></asp:Button>
<asp:Button ID="UpdateCartButton" runat="server" CssClass="btn btn-primary" Text="Update cart" OnClick="UpdateCartButton_Click"></asp:Button>
<%--<button type="button" class="btn btn-primary">Update cart</button>--%>
</div>
</td>
</tr>
</tfoot>
</table>
</LayoutTemplate>
</asp:ListView>
code behind of update quantity
protected void UpdateCartButton_Click(object sender, EventArgs e)
{
for (int i = 0; i < FullCartListView.Items.Count; i++)
{
TextBox quantity = (TextBox)FullCartListView.Items[i].FindControl("QuantityTextBox");
Label productid = (Label)FullCartListView.Items[i].FindControl("ProductIDLabel");
ShoppingCart.ShoppingCart.Instance.SetItemQuantity(Convert.ToInt32(productid.Text), Convert.ToInt32(quantity.Text));
}
BindData();
}
protected void BindData()
{
FullCartListView.DataSource = ShoppingCart.ShoppingCart.Instance.Items;
FullCartListView.DataBind();
}
From what you're saying, I think the text boxes are being bound on postback, click events happen after postback, so if the controls are being databound on postback, the updates in the text box would be lost
Edit: More specifically the page load after a postback happens before event handlers are called
Edit2: looked closer at your code after your comment, you're using Eval on the textboxes which will not store your updates, try bind instead

Asp.net syntax error for Command Argument in linkbutton

I am trying to use linkbutton inside of foreach in asp.net
I have below html.
Asp.net html:
<table border="1" style="grid-cell: inherit; border-spacing: inherit;">
<thead>
<tr>
<th>İlan ID
</th>
<th>İlan Yolu
</th>
<th>Eklenme Tarihi
</th>
<th>İlk Güncelleme Tarihi
</th>
<th>Güncelleme Aralığı
</th>
<th>Son Güncelleme Tarihi
</th>
<th>Aktifmi
</th>
<th>Detay Göster
</th>
</tr>
</thead>
<%foreach (var item in list)
{%>
<tr>
<td style="text-align: center">
<span><%= item.Id%> </span>
</td>
<td>
<span><%=item.DosyaAdi %></span>
</td>
<td style="text-align: center">
<span><%=item.EklemeTarihi %></span>
</td>
<td>
<span><%=item.IlkGuncellemeTarihi %></span>
</td>
<td style="text-align: center">
<span><%=item.GuncellemeAraligi %></span>
</td>
<td style="text-align: center">
<span><%=item.SonGuncelleme %></span>
</td>
<td style="text-align: center">
<input type="checkbox" class="chk" id="<%=item.Id %>" <%= item.Aktif ==true ? "checked='checked'" : "" %> />
</td>
<td style="text-align: center">
<asp:LinkButton ID="lbdetay" runat="server" OnClick="lbdetay_Click" CommandArgument="<%=item.Id%>" CommandName="Detay">Detay</asp:LinkButton>
</td>
</tr>
<% } %>
</table>
Question:
In part of linkbutton as below
<asp:LinkButton ID="lbdetay" runat="server" OnClick="lbdetay_Click" CommandArgument="<%=item.Id%>" CommandName="Detay">Detay</asp:LinkButton>
If i use CommandArgument="<%=item.Id%>" it is not working (it displays syntax error here)
Where i miss inside of code for command argument in linkbutton side ?
Any help will be appreciated.
Thanks
<%= %> is equivalent to Response.Write, it outputs directly to the html markup. So it cannot write to server-side controls at all, since it does not know about them. In other words, what you are trying to do is impossible.
You should probably consider refactoring this foreach to Repeater, which gives control over controls as well as html:
<asp:Repeater runat="server" ID="Repeater1">
<ItemTemplate>
<td style="text-align: center">
<span><%# Eval("Id") %></span>
</td>
... same for other tds ...
<td style="text-align: center">
<asp:LinkButton ID="lbdetay" runat="server" OnClick="lbdetay_Click" CommandArgument='<%# Eval("Id") %>' CommandName="Detay">Detay</asp:LinkButton>
</td>
</ItemTemplate>
</asp:Repeater>
And don't forget to data bind it:
Repeater1.DataSource = list;
Repeater1.DataBind();

How to get count of nested repeater items

I've a repeater on my page. And it's containing a nested repeater on it's item.
It's structure is as below
<asp:Repeater ID="rptOuterRepeater" runat="server" >
<ItemTemplate>
<tr >
<td>
// Need some code logic here for counting
</td>
</tr>
<tr>
<td>
<asp:Repeater ID="rptInnerRepeater" unat="server">
<ItemTemplate>
<tr >
<td>
</td>
</tr>
</ItemTemplate>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
I need to count rptInnerRepeater 's item count in place of comments. Is this possible using Inline Code.
Since, you are binding your Repeater to a Datasource, Why cannot you manipulate your datasource to contain the Count of the SubItems Collection to which your inner repeater is bound?
You can do it this way:
<asp:Repeater ID="rptOuterRepeater" runat="server">
<ItemTemplate>
<table>
<tr>
<td>
<%# Eval("Count") %>
</td>
</tr>
<tr>
<td>
<asp:Repeater ID="rptInnerRepeater" runat="server"
DataSource='<%# Eval("Items") %>'>
<ItemTemplate>
<tr>
<td>
<%# Eval("Id") %>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
and your DataSource is :
var datasource = testList.Select(s =>
new
{
Count = s.Items.Count,
Items = s.Items,
Id = s.Id
}).ToList();
rptOuterRepeater.DataSource = datasource;
rptOuterRepeater.DataBind();
so why not modify your datasource collection to contain an extra field called Count?
on the client side, you will have to use very ugly javascript/jQuery code, which will be superbly specific to your need.
Best I could come with in terms of short coding, using a literal and a ItemDataBound event handler. Not really what I would call "inline", but I hope this will help anyway.
<script runat="server">
void innerRpItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.DataItem != null)
{
var lit = (Literal) ((Control) sender).Parent.FindControl("count");
lit.Text = string.IsNullOrWhiteSpace(lit.Text) ? "1" : (int.Parse(lit.Text) + 1).ToString();
}
}
</script>
<asp:Repeater ID="rptOuterRepeater" runat="server" >
<ItemTemplate>
<tr >
<td>
// Need some code logic here for counting
<asp:Literal runat="server" ID="count"/>
</td>
</tr>
<tr>
<td>
<asp:Repeater ID="rptInnerRepeater" runat="server" OnItemDataBound="innerRpItemDataBound" >
<ItemTemplate>
<tr >
<td>
</td>
</tr>
</ItemTemplate>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
---------------- previous non working solution
You may try to replace this line
// Need some code logic here for counting
with this one
<%#((Repeater)Container.FindControl("rptInnerRepeater")).Items.Count%>
Not tested, and not really sure it would work, but worth a try I would say :-)
Hope this will help

Getting control ids in a repeater

I have the following snippet getting control ids in a listview at runtime. However ctrl is null when debugged in spite of the correct id being provided.
foreach (ListViewItem item in lvData.Items)
{
string uid = item.ClientID;
Control ctrl = lvData.FindControl(uid+"_lbUnattend");
ctrl.Visible = false;
}
my template:
<asp:ListView runat="server" ID="lvData" DataSourceID="odsEvents"
OnDataBound="lvData_DataBound"
onselectedindexchanged="lvData_SelectedIndexChanged">
<LayoutTemplate>
<table class="diary" cellspacing="0" width="600px">
<tr>
<th align="center">
Date:
</th>
<th align="center">
Time:
</th>
<th align="center">
Event:
</th>
<th align="center">
Location:
</th>
<th align="center">
</th>
</tr>
<tr runat="server" id="itemPlaceHolder">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td width="100px" align="center">
<%#FRT.Utils.DateTimeHelper.FormatToShortDate(Eval("StartDate"))%>
<%#FRT.Utils.DateTimeHelper.FormatToShortDate(Eval("VisitDateStart"))%>
</td>
<td width="100px" align="center">
<%#FRT.Utils.DateTimeHelper.FormatToShortTime(Eval("StartDate"))%>
<%#FRT.Utils.DateTimeHelper.FormatToShortTime(Eval("VisitDateStart"))%>
</td>
<td width="100px" align="center">
<%#Eval("Title")%>
</td>
<td width="200px" align="center">
<%# Eval("Address") %>
</td>
<td width="100px" align="center">
<asp:LinkButton ID="lbLogin" runat="server" Text="I want to attend this event" CssClass="button" />
<asp:LinkButton ID="lbAttend" runat="server" Text="I want to attend this event" CssClass="button" />
<asp:LinkButton ID="lbUnattend" runat="server" CommandName="unattend" Text="I want to unregister from this event" CssClass="button" />
See details&nbsp
</td>
</tr>
</ItemTemplate>
</asp:ListView>
where linkbuttons whith ids generated from lbUnattend are the ones I want.
Anyone pointout where the problem is?
I think what you're looking for is
foreach (ListViewItem item in lvData.Items)
{
Control ctrl = item.FindControl("lbUnattend");
ctrl.Visible = false;
}

Categories

Resources