Binding a Dictionary to a repeater asp.net 4 - asp.net

i have a dictionary in this form Dictionary<int, List<CartItem>>, this dictionany contain informationa about my shopping cart item. i am looking for a way to display the result in a repeater or a datalist what ever is better. i have started using a repeater and i got stuck
i want to display the information in the list in the repeater for each element in the repeater
in the page.aspx.cs
void bindinfo()
{
ShoppingCart cart = ShoppingCart.GetShoppingCart();
RepeaterCustomerShoppingCarts.DataSource = cart.dictionaryShoppingCart;
RepeaterCustomerShoppingCarts.DataBind();
}
in page.aspx
<asp:Repeater id="RepeaterCustomerShoppingCarts" onitemdatabound="RepeaterCustomerShoppingCarts_ItemDataBound" runat="server">
<HeaderTemplate>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="shopping-table">
<tr>
<th> </th>
<th>Merchant Name</th>
<th>Quantity</th>
<th>Total Amount</th>
<th> </th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="first"><asp:Image ID="MerchantLogo" runat="server" width="52" height="46"/></td>
<td>
<b><asp:Label ID="lblTitle" runat="server" Text=""></asp:Label></b>
<p> <asp:Label ID="lblDetails" runat="server" Text=""></asp:Label> </p>
</td>
<td>
<div class="dropdown">
<asp:Label ID="lblQuantity" runat="server" Text=""></asp:Label>
</div>
</td>
<td><b><asp:Label ID="lblTotalPrice" runat="server" Text=""></asp:Label> </b></td>
<td><asp:HyperLink ID="hlShopDetails" CssClass="remove" runat="server">More Details</asp:HyperLink></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
any help and advice to try to display the information would be great. the purpose of the page is u can have many shopping cart depending on different merchant and u display them here than there is a link called more details it will take you to a specific cart.
thanks

Try this
protected void RepeaterCustomerShoppingCarts_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
if (e.Item.ItemType == ItemType.AlternatingItem || e.Item.ItemType == ItemType.Item){
// Grab all your controls like this
var lblQuantity = e.Item.FindControl("lblQuantity") as Label;
var NestedRepeater = e.Item.FindControl("NestedRepeater") as Repeater;
// Get the current data
var data = (KeyValuePair<Int32, List<CartItem>>)e.Item.DataItem;
//Bind the values
lblQuantity.Text = data.Value.Count.ToString();
NestedRepeater.DataSource = data.Value;
NestedRepeater.DataBind();
}
}
The markup would look something like this.
<asp:Repeater id="RepeaterCustomerShoppingCarts" onitemdatabound="RepeaterCustomerShoppingCarts_ItemDataBound" runat="server">
<ItemTemplate>
<asp:Label id="lblQuantity" runat="server" />
<asp:Repeater runat="server" id="NestedRepeater">
<ItemTemplate>
<!-- whatever your controls -->
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
The thing here is that you control your layout in the code behind. Grab the necessary controls there and the current data. Then just do your bindings as normally.

Rather than using the normal Eval("ColumnName") on a dictionary you would simply do Eval("value")
You could do something like this.
<asp:Label ID="lblQuantity" runat="server" Text='<%#(Eval("key")=="Quantity" ? Eval("value") : "") %>' />

Related

OnCheckedChanged doesnt pick up function when checked

Using a repeater, which displays a list, which includes a checkbox field.
When a checkbox is clicked I want the function to run and display the label....trying to debug this but cant seem to get it going, using breakpoint wont pick up the function
//code behind
protected void chkMyCheck_CheckedChanged(object sender, EventArgs e)
{
label.Text = "Button Clicked";
}
aspx:
<asp:Repeater id="rptSelectedUtilities" runat="server">
<HeaderTemplate>
<table class="detailstable FadeOutOnEdit">
<tr>
<th style="width:200px;">Utility</th>
<th style="width:200px;">Contacted</th>
<th style="width:200px;">Comment</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<th style="width:200px;"><%# Eval("Name") %></th>
<th style="width:200px;"><asp:CheckBox ID="chkMyCheck" AutoPostBack="true" runat="server" OnCheckedChanged="chkMyCheck_CheckedChanged" Checked='<%# Convert.ToBoolean(Eval("Checked")) %>'/></th>
<th style="width:200px;"><%# Eval("Comment") %></th>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:Label id="labelTableEmpty" runat="server" Text="There are currently no items in this table." />
<asp:Label ID ="label" runat="server" />
Any idea as to why the code doesnt go into the chkMyCheck_CheckedChanged function when a checkbox is clicked?, the page does re-fresh so its picking up auto post back
Are you bind repeater inside of !IsPostBack ????
for
void page_load()
{
if(!IsPostBack)
{
//BindRepeater method???
}
}
Check

ASP.net DataGrid adding extra information to table I do not want

Here is what DataGrid does to my table:
<table id="ContentPlaceHolder1_articleList" cellspacing="0" style="border-width:0px;border-collapse:collapse;" headertext="File Name" rules="all">
How can I remove the unnecessary info from the table?
I do not want cellSpacing, anything in the style. I take care of all of thise with CSS and because these are inline it is overriding my CSS declarations.
I don't care if the ID, or rules="all" (not even sure what that does) or the HeaderText is present, but I would love to get rid of the rest.
As Curt suggested, you can use a Repeater control for more control over the output. Another option would be the ListView control, which gives you more options than the the Repeater does.
<asp:ListView ID="ListView1" runat="server">
<LayoutTemplate>
<table class="myclass">
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("ColumnName", "{0:#,###} bytes") %></td>
</tr>
</ItemTemplate>
</asp:ListView>
If you really want full control over your table, I would recommend using a Repeater instead. Unlike the DataGrid control, the Repeater control doesn't render any HTML which isn't inside the ItemTemplate giving you full control over your rendered code.
<table>
<tr>
<td>Column Header</td>
</tr>
<asp:repeater id="rep" runat="server">
<itemtemplate>
<tr>
<td>
<%#eval("ColumnName") %>
</td>
</tr>
</asp:repeater>
</table>
As the the Eval/DataItem will give object/String return value. In
order to convert into 3,546 bytes output, it is required to convert it into
integer. For that onitemdatabound event registration will be required.
Mark Up
<asp:ListView ID="ListView1" runat="server" onitemdatabound="rpt_ItemDataBound">
<LayoutTemplate>
<table class="myclass">
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><asp:Label ID="lbl" runat="server" ></asp:Label></td>
</tr>
</ItemTemplate>
</asp:ListView>
Code Behind
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
Label lbl= (Label)e.Item.FindControl("lbl");
lbl.Text = String.Format("{0:#,### bytes}",
Convert.ToInt32(((YourClassName)e.Item.DataItem).YourProperty);
}
}

SortExpression in ASP.NET GridView not working with <HeaderTemplate>

I have an ASP.NET GridView. Now I am adding the SortExpression property tothe <TemplateField> tags, to make specific columns sortable.
Now, one of the columns has some markup content to be added in the header. The problem is, SortExpression does not work if there is a <HeaderTemplate> tag in a <TemplateField>, you have to put it inside the HeaderText property of <TemplateField>. But, all the HTML content does not work properly if I dump it inside the HeaderText property.
<asp:TemplateField SortExpression="FK_TesterID" ItemStyle-Width="300px" FooterStyle-Width="300px" ItemStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
<HeaderTemplate>
<table width="100%">
<tr>
<td align="center">
Tester
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="cmbTestersHeader" ClientIDMode="Static" runat="server" Width="300px" DataSource='<%# PopulateTesterNames() %>' DataTextField="FullName" DataValueField = "PK_ID" Visible="false" AutoPostBack="true" OnSelectedIndexChanged="cmbTestersHeader_SelectedIndexChanged" ToolTip="Bulk Assign Testers !" ></asp:DropDownList>
</td>
</tr>
</table>
</HeaderTemplate>
So you can see, if I put the entire <HeaderTemplate> property inside a headertext, it doesn't work.
But I want to have both functionalities. Can anyone help?
Then you need to provide a control in your HeaderTemplate with CommandName="Sort", for example a LinkButton.
<HeaderTemplate>
<table width="100%">
<tr>
<td align="center">
<asp:LinkButton ID="LbSort" runat="server" CommandName="Sort" Text="Sort" />
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="cmbTestersHeader" ClientIDMode="Static" runat="server" Width="300px"
DataSource='<%# PopulateTesterNames() %>' DataTextField="FullName" DataValueField="PK_ID"
Visible="false" AutoPostBack="true" OnSelectedIndexChanged="cmbTestersHeader_SelectedIndexChanged"
ToolTip="Bulk Assign Testers !">
</asp:DropDownList>
</td>
</tr>
</table>
</HeaderTemplate>
This is a quite old thread I have stumbled over while trying to solve exactly that described problem but the solution provided here didn't worked for me. If you have a Sorting method defined for the GridView then
<asp:LinkButton ID="LbSort" runat="server" CommandName="Sort" Text="Sort" />
will call that method
protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
{
dt.DefaultView.Sort = e.SortExpression;
but e.SortExpression will be null and no sorting is happening. You have to first pass the Column's name through the CommandArgument of the LinkButton. Only then it worked in my case!
<asp:LinkButton ID="LbSort" runat="server" CommandName="Sort" CommandArgument="ColumnName" Text="Sort" />

Can't get edit to work on a ListView

I have the following code which I thought would give me the ability to edit a record in my list view but when i click edit i get a postback but am not able to edit anything. Am i doing something wrong?
<asp:ListView ID="lv_Personnel" runat="server" OnItemEditing="lv_Personnel_ItemEditing">
<LayoutTemplate>
<table cellpadding="2" border="1" runat="server" id="tbl_Personnel">
<tr id="headerRow" runat="server">
<th>
</th>
<th>
Level of Staff
</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
<tr runat="server" id="insertPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:LinkButton ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
</td>
<td>
<%# Eval("LineDescription")%>
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr runat="server" style="background-color: #ADD8E6">
<td>
</td>
<td>
Level of Staff:
<asp:TextBox ID="tb_LevelOfStaff" runat="server" Text='<%# Eval("LineDescription") %>' />
</td>
</tr>
</EditItemTemplate>
</asp:ListView>
Are you databinding your ListView to anything? If it's not databound, then you're going to have to manually specify the record that you want to edit by handling the ItemEditing event.
protected void MyListView_ItemEditing(object sender, ListViewEditEventArgs e)
{
ListView1.EditIndex = e.NewEditIndex;
// Re-databind here
}
Based on comments, don't databind on every postback unless you have ViewState turned off.
private void Page_Load()
{
if (!IsPostBack)
{
//databind
}
}
It looks like i just needed to add an OnItemEditing event to my ListView declaration and the function to back it up. I've updated my code snippit above to reflect the changes made in the aspx file.

Master/Detail help needed for ASP.NET newbie. (screenshot too)

I'm just beinning basic data driven ASP.NET webforms design. I have the first part of a form working, but I don't know what to do next.
Please see this screenshot of what I have created so far: http://www.twitpic.com/2gnmr
I need help knowing what kind of HTML element to use in the master list, and what event/trigger on that element to use to fire off the "get child records for this selected item" sequence?
Also, is a ListView even the correct way to present the master list? At this time, I'm not trying to provide any editing features; I'll get to that later, I guess.
Should I use some other ASP.NET data control rather than hand-coding a listview like I am doing?
I don't want to see an actual "Select" link item beside each Customer Name (that looks goofy). I want the Customer Name to be the link to click on.
So, you can see in my code below that I have a ListView to present a list of CustomersWithOpenOrders. But, it's just a static list, so how do I make the Company Name label clickable, and what else will I need to make it fire back to some code-behind to fetch the child records. I already have a code-behind method to get child records for a passed-in CustomerNumber into a DataTable, and I think I would know how to bind that to a grid or listview for child records, but I do not know how to pass the CustomerNumber from the master ListView to the method from the UI form.
<asp:ListView ID="ListView1" runat="server">
<LayoutTemplate>
<table cellpadding="2" border="0" ID="tbl1" runat="server">
<tr id="Tr1" runat="server" class="lvHeader">
<th id="Th1" runat="server">Customer</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr id="Tr2" runat="server">
<td>
<asp:Label ID="label2" class="FirstLine" runat="server" Text='<%# Eval("company") %>' />
<br />
<div class="SecondLine">
<asp:Label ID="labelCustNo" runat="server" Text='<%# Eval("custno") %>'/>
<asp:Label runat="server" Text='Ph: '></asp:Label>
<asp:Label ID="label3" runat="server" Text='<% # Eval("phone") %>' />
</div>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
I personally haven't found a case where the ListView can't solve my needs. If you want to create a customer selection style list you can use a link button for binding.
<asp:ListView runat="server" id="CustomersList" ItemCommand="CustomersList_ItemCommand">
<LayoutTemplate>
<table cellpadding="2" border="0" ID="tbl1" runat="server">
<tr id="Tr1" runat="server" class="lvHeader">
<th id="Th1" runat="server">Customer</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr id="Tr2" runat="server">
<td>
<asp:LinkButton ID="link1" class="FirstLine" runat="server" Text='<%# Eval("company") %>' CommandName="Select" />
<br />
<div class="SecondLine">
<asp:Label ID="labelCustNo" runat="server" Text='<%# Eval("custno") %>'/>
<asp:Label runat="server" Text='Ph: '></asp:Label>
<asp:Label ID="label3" runat="server" Text='<% # Eval("phone") %>' />
</div>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:ListView runat="server" ID="OrderList">
<!-- Child Rows implementation -->
</asp:ListView>
Then you would need to bind the event to the ListView.ItemCommand.
protected void CustomersList_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName = "Select")
{
if (e.Item.ItemType != ListViewItemType.DataItem) return;
var dataItem = e.Item as ListViewDataItem;
if (dataItem == null) return;
var customer = dataItem.DataItem as Customer;
if (customer == null) return;
this.OrdersList.DataSource = GetChildRecords(customer.ID);
this.OrdersList.DataBind();
}
}

Resources