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
public IQueryable 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 list = GetData().Select(o => o.Country).Distinct().ToList();
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
TerryPosted May 6, 2015, 4:09 PM
Sharing the solution to my question.
With the reference to link shared by Vincent, I found that in all the examples shown, the drop down was out on ListView/GridView. The site says that parameter to SelectMethod can be pass of type Control, Query string Value, etc. With string as value of dropdown, none of them worked for me. So I came up to :
ItemType="VincitoreCRMApplication.Models.ChannelViewModel"
DataValueField="ID" DataTextField="Country"
SelectMethod="GetCountryList" >
Code-BEHIND
// GET COUNTRY LIST FOR DROP DOWN
public IEnumerable
{
var countryList = _db.Channels.Where(o => !o.Country.Equals(string.Empty))
.Select(o => o.Country).Distinct().ToList();
int id = 1;
List
foreach (var country in countryList)
{
if (country != null)
{
ChannelViewModel c = new ChannelViewModel();
c.ID = id++;
c.Country = country;
list.Add(c);
}
}
return list;
}
Check the ID (DataValueField) and the created ID property in the Model. This was the key to work on this.
- Then in my GetData() i.e. the SelectMethod of ListView I added [Control]int? parameter :
// Model binding method to get Grid of Channel entries// USAGE:
public IQueryable
{
IQueryable
.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
});
if (countryList > 0)
{
System.Diagnostics.Debug.WriteLine("Value = " + CountryList.SelectedValue);
cvm = cvm.Where(c => c.Country == CountryList.SelectedItem.Text);
}
return cvm;
}
And I could filter the list of data based on the selection of dropdown. It works like a charm.
NOTE: The parameter passed to GetData() can't be of type string? because initially string value will be null and that isn't accepted in any ways. But it works well with int? or
But the KEY is to create a seperate Model or add another Property in the Model and make that as the DataValueField for the DropDownList and we are done with the ModelBinding.
Hope this could help someone.
Thanks to everyone for supporting me and suggesting me the needful. Vincent, special thanks to you.
Vincent Maverick DuranoPosted May 6, 2015, 12:54 PM
Regarding your last question. I'm not sure if that would be possible at the moment to mark your own reply as answer. Having said that just please do post your full solution for future reference.
Thanks.
Vincent Maverick DuranoPosted May 6, 2015, 12:50 PM
TerryPosted May 6, 2015, 12:25 PM
Is their a way, that I can mark my reply as "Answer", so can close this thread and others can also refer the same and find the solution.
TerryPosted May 6, 2015, 12:14 PM
Your code works, but it doesn't eliminate the value's with "null". So an item with "null" value is also added in the countrylist. I added if (country != null) and things works fine.
Thanks for this Linq statement.
Thanks once again.
Vincent Maverick DuranoPosted May 6, 2015, 11:31 AM
TerryPosted May 6, 2015, 10:48 AM
Looking at that I tried my way implementing with int? by creating an object and adding an ID in it & setting that ID as DataValueField for the drop down.
Then also tried with Enum, that also worked. But string? didn't work in any ways. Though the site mentions "Query string Value", but I couldn't make it happen.
Anyways, Thanks a lot for your response.
In this, can you suggest a better option :
I want list of unique Country's from the collection of data. In my above code, you can see how much I am doing to achieve the goal. I hope their should be some easier & straight forward method that I am missing out.
Can you suggest for the above.
Thanks.
Vincent Maverick DuranoPosted May 6, 2015, 10:35 AM
The link that i've posted is just a reference on how to pass parameters using model bindings. I can't provide you actual code as I didn't use ModelBindings yet, but I was hoping that could give you a good start on resolving your issue.
TerryPosted May 6, 2015, 10:27 AM
I had a look at that site. That says, we can send parameters any of such examples :
This site didn't solved my issue what I was looking for. But I learned something my way by asking the same question in other sites & more research.
In my case Control wasn't working because it was passing string value & nor string? was working as string can be null and the parameter cannot accept null.
From this site I couldn't get why my string? wasn't working. But finally got that it needs either int, Enum - basically whatever Value passed is int. Don't know if any other object also works !!.
And this Filtering dropdown what I had in
Thanks.
Vincent Maverick DuranoPosted Apr 30, 2015, 4:25 PM
https://msdn.microsoft.com/en-us/vs11trainingcourse_aspnetwebforms45_topic2.aspx