List control data filter asp.net
I have a list control. I have data coming from the object. there is field called status id in that object.
I have a drop down box. The drop down contains a status id field.
Initially all the data is being popualted.
When I click on filter by selec开发者_如何学JAVAting an option in the dropdown I want my list to be loaded with the status field equal to the selecetd value value in drop down.
In page load I am using foreach to populate the list control. Where do I put the filter statement in of drop down code. Shall I put it in if(!ispostback) or outside the postback.
Because everytime I select the value from the drop down the list populates with the default values.
Please help me.
From what I have understood from your description, if you will place your filtering code in the page load event than:
if it's inside an if (!Page.IsPostBack) { } block than it will be executed only when the page is rendered for the first time - but you want to have all data initially and filter later when you have selected an option from the drop down;
if it is outside of this block it is not right because it will be executed when any control on the page causes a postback not only when you you want to filter by selecting an option from the drop down.
Try to use something like this instead:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// executed only first time
// load data for both list control and drop down
}
}
protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
// filter the data
// (or clear the items in the list control and fetch new data for the list
// control based on the status id selected in the drop down and rebind it)
}
Don't forget to set AutoPostBack="True" for the drop down control or else SelectedIndexChanged event will not cause a postback and your data will not be filtered when you change the selection (just after some other controls causes a postback).
精彩评论