Hello,
I am using aSP.net 4.5 Web Forms. I have a listview whose SelectMethod is set. In the header of ListView I have a dropdown whose data is populated dynamically & set Datasource.
Now, based on the selection of drop down I want to show items in ListView. I am not able to get this part. Here's my code :
<asp:ListView id="ListView1" runat="server"
DataKeyNames="ChannelId"
ItemType="VincitoreCRMApplication.Models.ChannelViewModel" SelectMethod="GetData" >
<LayoutTemplate>
<table class="table" runat="server">
<tr runat="server">
<td runat="server">
<table id="itemPlaceholderContainer" runat="server" class="table">
<thead>
<th>
<asp:LinkButton Text="Country" CommandName="Sort" CommandArgument="Country" runat="Server" />
<br />
<asp:DropDownList ID="CountryList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="CountryList_SelectedIndexChanged" >
</asp:DropDownList>
</th>
<th> </th>
</tr>
</thead>
In the code behind :
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Populate DropDown Countries
DropDownList CountryList = ListView1.FindControl("CountryList") as DropDownList;
if (CountryList != null)
{
CountryList.DataSource = GetCountries();
CountryList.DataBind();
CountryList.SelectedIndex = -1;
}
}
}
// Model binding method to get List of Channel entries
// USAGE: <asp:ListView SelectMethod="GetData">
public IQueryable<VincitoreCRMApplication.Models.ChannelViewModel> GetData()
{
return _db.Channels
.Select(u => new ChannelViewModel
{
ChannelId = u.ChannelId,
NameSuffix = u.NameSuffix,
FirstName = u.FirstName,
LastName = u.LastName,
City = u.City,
State = u.State,
Country = u.Country
});
//return _db.Channels;
}
public string[] GetCountries()
{
List<String> list = GetData().Select(o => o.Country).Distinct().ToList<String>();
list.Insert(0, "--- Filter ---");
list.RemoveAll(s => s == null);
return list.ToArray();
}
// Selection Event of CountryList DropDown
protected void CountryList_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList CountryList = ListView1.FindControl("CountryList") as DropDownList;
if (CountryList != null)
{
System.Diagnostics.Debug.WriteLine("Selected Index = " + CountryList.SelectedIndex);
if (CountryList.SelectedIndex > -1)
// This also gives proepr result
System.Diagnostics.Debug.WriteLine("Selected text = " + CountryList.Text);
}
}
Now the only part I am stuck is, how do I refresh the GetData() using the selected Country from drop down. If I set a parameter in GetData, then how do I pass parameter in SelectMethod part.
I believe I can set the DataSource and Databind of ListView programmatically & get it work. But I think using Model Binding is more worth than using DataSource/Databind pattern.
Also, I wont be adding/Editing/Deleting items in the Lsitview - it is just for display & viewing with a facility to filter the data based on selection.
Can anyone please suggest me for the above. I am not able to get any expected result from googling the same.
Kindly try to help me at the earliest.
Thanks