Avoid data redundancy in ASP.NET page
I have an asp.net page which uses a listview to show 2 columns of data. The first column has just labels and the second one has dropdowns.
My concern is, the dropdowns in second column has same item开发者_如何学Pythons 100% of the time, they never change and since it is databound, and datasource to these dropdown is also same. As these dropdowns are in a list view this repetition happens on each row added to my list view!
So, I was thinking of removing this data redundancy being transported over the wire. Any ideas?
If you are using an ObjectDataSource, you can reduce the load time enabling the cache:
<asp:objectdatasource ID="ObjectDataSource1" runat="server"
EnableCaching="true" .... >
</asp:objectDataSource>
If your datasource is a database call then you can reduce that by storing the result of the call in a DataView
object, and then binding your dropdowns to that object rather than making the call to the database for each dropdown.
You could use the following approach:
It loads the items in to the very first DropDownList and then uses JQuery to retrieve that DropDownList and and replicate the items into all of the others.
Markup
<div id="listViewContainer">
<asp:ListView ID="listView1" runat="server">
<ItemTemplate>
<div><asp:DropDownList ID="dropDownList1" runat="server"></asp:DropDownList></div>
</ItemTemplate>
</asp:ListView>
</div>
Script
$(function () {
var sourceDropDown = $('#listViewContainer').find('select').first();
$('#listViewContainer').find('select').not(sourceDropDown).each(function () {
var dropdown = $(this);
dropdown.find('option').remove();
sourceDropDown.find('option').each(function () {
var option = $(this);
dropdown.append($('<option />').text(option.text()).val(option.val()));
});
});
});
Code
void listView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.DisplayIndex == 0)
{
DropDownList dropDownList1 = (DropDownList)e.Item.FindControl("dropDownList1");
dropDownList1.DataSource = dataTable;
dropDownList1.DataTextField = "Text";
dropDownList1.DataValueField = "Value";
dropDownList1.DataBind();
}
}
Hope this helps.
精彩评论