ListView Web Control question:
I developed a report service web application using Asp.Net 3.5 and ReportingService2010.asmx. I retrieved the ItemHistorySnapshots with the following code:
ItemHistorySnapshot[] itemSnapshots = null;
itemSnapshots = rs.ListItemHistory(strChildNode);
foreach(ItemHistorySnapshot snapshot in itemSnapshots) {
listview.add (snapshot.HistoryID.Tostring());
listview.add (snapshot.Size.Tostring());
listview.add(snapshot.DateTime.Tostring());
}
I want to create a ListView with 3 columns HistoryID, Size, DateTime and want to assign the string values in foreach loop.
Please let me know how to assign the string values to ListView. I want to know the source control code for listview also. Thank you very much.
You can use an repeater and bind the data to it, like this:
Code behind:
ItemHistorySnapshot[] itemSnapshots = null;
itemSnapshots = rs.ListItemHistory(strChildNode);
rpt.DataSource = itemSnapshots.Select(s => new
{
HistoryID = s.HistoryID.ToString(),
Size = s.Size.ToString(),
DateTime = s.DateTime.ToString(),
});
rpt.DataBind();
Aspx Page:
<asp:Repeater id="rpt" runat="server">
<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>HistoryID</th>
<th>Size</th>
<th>DateTime</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("HistoryID") %></td>
<td><%# Eval("Size") %></td>
<td><%# Eval("DateTime") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Related
I am binding a dataset to asp repeater.
<asp:Repeater id="rptDataset" runat="server">
<table>
<HeaderTemplate>
<tr>
<th>Col1</th>
<th>Col2</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("c1") %></td>
<td><%# Eval("c2") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
Now I have varying number of columns, that won't be known until the page is loaded. So I'm modifying the header like this
<HeaderTemplate>
<tr>
<th>Col1</th>
<th>Col2</th>
<% for (int i=0; i<NoOfColumn; i++){
Response.Write("<th>ColN</th>")
} % >
</tr>
</HeaderTemplate>
Which correctly showed all column headers.
What should I modify <ItemTemplate> so it could show all the column data?
I've tried something like
<% Response.Write("<td><%# Eval("c1") %></td>") %>
which would result an error due to the nested <% %> sign.
Also tried
<% Response.Write(DataBinder.Eval(Container.DataItem, "c1")) %>
but it said Container cannot be resolved.
So you have two options, quick hack and proper pretty solution.
Quick hack
Create all necessary html in a string and output it in Literal control. Make sure to use PassThrough mode not to have html encoded:
<ItemTemplate>
<%# GetDataRow(Container.DataItem) %>
</ItemTemplate>
protected string GetDataRow(object dataItem)
{
StringBuilder output = new StringBuilder();
output.Append("<tr>");
for (int i=0; i<NoOfColumn; i++)
{
output.Append("<td>");
output.Append(DataBinder.Eval(dataItem, "c"+i));
output.Append("</td>");
}
output.Append("</tr>");
return output.ToString();
}
Pretty solution
Define your own custom template implementing ITemplate. A complete example of how this is done can be found here. It will take more work, but, as it's usually the case with pretty solutions, result will be much more readable, maintainable and reusable.
Finally I have it solved by applying nested Repeater.
<asp:Repeater runat="server" ID="rptRow">
<ItemTemplate>
<tr>
<td><%# Eval("c1") %></td>
<td><%# Eval("c2") %></td>
<asp:Repeater runat="server" ID="rptCol">
<ItemTemplate>
<td><%# Eval("cn") %></td>
</ItemTemplate>
</asp:Repeater>
</tr>
</ItemTemplate>
</asp:Repeater>
I have a control (ascx) that has an itemtemplate for inserted service locations. What I am trying to do is OnDataBound event have an item already added upon the listview showing. The listview is for city and state. I have been trying to use the ListViewItemInsertArgs, but the signature is wrong. Been trying to figure this out for days..
protected void lvServiceLocations_DataBound(object sender, EventArgs e)
{
ListView lv = sender as ListView;
var lvi = new ListViewDataItem(0, 0);
lvi.DataItem = da_User_Data.Select_Applicant_Specific_Results(userProfileId, dlControlId); //this returns a string
lv.Items.Insert(0, lvi);
}
<asp:ListView ID="lvServiceLocations" runat="server" DataSourceID="sdsServiceLocations" DataKeyNames="user_service_location_id" InsertItemPosition="FirstItem"
OnItemInserting="lvServiceLocations_ItemInserting"
OnDataBound="lvServiceLocations_DataBound">
<LayoutTemplate>
<table class="location-selection-table ui-widget-content">
<tr><th colspan="4" class="ui-widget-header">Locations</th></tr>
<tr id="itemPlaceHolder" runat="server" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr class="location-selection-item-row">
<td class="location-selection-actions"><asp:LinkButton runat="server" Text="Remove" CommandName="Delete" /></td>
<td><%# Eval("selected_state") %></td>
<td><%# Eval("selected_county") %></td>
<td class="location-selection-actions"></td>
</tr>
</ItemTemplate>
DataBound event will only occur if you call the DataBind method, and it will only occur once either Page.DataBind or Ctrl.DataBind is called but in any case it is not in this method where you have to insert the new item but instead you have to add it to the Source of the data set in the datasource and then you reset the control with a DataBind method call.
So you have to insert the new item into the source associated with sdsServiceLocations and not directly into the control
I've used this tutorial to use a repeater to display a list of names on a page on my project.
So I'm using dynamic data and in my aspx.cs page I have:
List<string> subContractors = new List<string>();
Context db = new Context();
subContractors = (from SUBContractors in db.BOQ_SubContractors
where SUBContractors.Bill_Of_Quantity_id == this.boqId
select SUBContractors.Sub_Contractor.Company_Name).ToList();
repeaterShowSubContractorName.DataSource = subContractors;
repeaterShowSubContractorName.DataBind();
In my aspx:
<asp:Repeater ID="repeaterShowSubContractorName" runat="server" OnItemDataBound="subContractors_ItemDataBound">
<HeaderTemplate>
<table>
<tr>
<th>
<asp:Label ID="SubConName" Text="SubContractor Name" runat="server"></asp:Label>
</th>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="SubCon" Text='<%# Eval("subContractors") %>' runat="server"></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
The error is coming from OnItemDataBound="subContractors_ItemDataBound".
What or where do I link this to? I have no subContractors_ItemDataBound at the moment.
just remove OnItemDataBound="subContractors_ItemDataBound" from your aspx page
Edit this error occurred because you don't have subContractors_ItemDataBound method in .cs file to handle OnItemDataBound event , so you have to handle OnItemDataBound event or just remove OnItemDataBound="subContractors_ItemDataBound"
Edit to bind list of strings use :
<asp:Label ID="SubCon" Text='<%# Container.DataItem %>' runat="server"></asp:Label>
Do this on your page load (or wherever you want to laod data) to link your data with repeater
repeaterShowSubContractorName.DataSource = subContractors;
repeaterShowSubContractorName.DataBind();
and remove
OnItemDataBound="subContractors_ItemDataBound"
I have a repeater that is bound to a List(of T) object collection. It's a list of Inventory objects. Each Inventory object also contains a List(of T) which is a list of Date / Inventory Count pairs. When the repeater is creating a table, I need to create a TD for each of the Date / Inventory Counts. Since the number of Date / Inventory Counts is not set until runtime (using NumWeeks variable), I need to vary the number of TD's in my repeater. This is essentially what I want:
<asp:Repeater ID="rptReport" runat="server">
<ItemTemplate>
<tr>
<td><%#DataBinder.Eval(Container.DataItem, "Department")%></td>
<td><%#DataBinder.Eval(Container.DataItem, "Description")%></td>
<% For x = 0 To NumWeeks%>
<td><%#DataBinder.Eval(Container.DataItem, "Values")(x).Value()%></td>
<% Next%>
</tr>
</ItemTemplate>
</asp:Repeater>
You need to place another repeater inside this repeater and assign the datasource to that inner repeater in the "ItemDataBound" event of the parent repeater. This should solve your issue.
Hope this is helpful!!
Another option would be that you create a UserControl containing a Repeater for the inner loop. You can assign the "Values" as a property of the UserControl. Something like this:
<asp:Repeater ID="rptReport" runat="server">
<ItemTemplate>
<tr>
<td><%# Eval("Department") %></td>
<td><%# Eval("Description") %></td>
<uc:WeekControl NumWeeks="<%#NumWeeks %>" Values='<%# EVal(Values)%> />
</tr>
</ItemTemplate>
</asp:Repeater>
I needed that for loop I tried another approach and it worked.
in my .cs file I created a public string and used it inside the repeater. It works fine.
public string somethingloop
for (int i = 0; i < dolar; i++)
{
somethingloop += "<i class='fa fa-dollar icon highlighted'></i>";
}
and in repeater inside anywhere in the itemtemplate
<asp:Repeater ID="rptReport" runat="server">
<ItemTemplate>
<%= somethingloop %>
<tr>
<td><%# Eval("Department") %></td>
<td><%# Eval("Description") %></td>
<uc:WeekControl NumWeeks="<%#NumWeeks %>" Values='<%# EVal(Values)%> />
</tr>
</ItemTemplate>
</asp:Repeater>
Greetings!
I'm looking for some advice regarding an approach to displaying data in a FormView based on a selection of a DropDownList within that FormView control. For example, I have a UserControl with the following:
<asp:XmlDataSource ID="xdsMyXmlData" runat="server" EnableCaching="false" XPath="Root/Membership" />
<asp:FormView ID="fvwMyFormView" runat="server" DataSourceID="xdsMyXmlData">
<ItemTemplate>
<div>
<h2><%# XPath("Title") %></h2>
<fieldset>
<asp:DropDownList ID="ddlMemberTypes" runat="server" DataSource='<%# XPathSelect("MenuItems/*") %>'></asp:DropDownList>
</fieldset>
<table>
<thead>
<tr>
<th><%# XPath("Columns/Name") %></th>
<th><%# XPath("Columns/Age") %></th>
<th><%# XPath("Columns/DateJoined")%></th>
</tr>
</thead>
<tbody>
<asp:Repeater ID="rptMembershipInfo" runat="server" DataSource='<%# XPathSelect("Members/*") %>'>
<ItemTemplate>
<tr>
<th><%# XPath("Data/Name") %></th>
<td><%# XPath("Data/Age") %></td>
<td><%# XPath("Data/DateJoined") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
</div>
</ItemTemplate>
</asp:FormView>
The UserControl's OnLoad() looks like this so far:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
string l_XmlData = MyControllerClass.GetMembershipTableXml(0);
xdsMyXmlData.Data = l_XmlData;
}
I would like to be able to pass the value of the DropDownList's selected item into GetMembershipTableXml() in order to retrieve the corresponding XML and then use it to populate the values of the FormView. What would the best way be to do this? Doing a Response.Redirect back to the current page using the selected DropDownList value as a query string variable? I'm hoping there's a better approach. What do you think?
You can create an event for OnSelectedItemChanged on your DropDownList; when this occurs, you can grab the selected item, and call your GetMembershipTableXml function.
Finally, dont forget to call DataBind on your FormView control to update the values :)
I think that's what you're after, hopefully it helps!