simonm_jp
asked on
Populate DataGrid with results based on DropDownList selection
Hi Experts,
I have a page which displays a dropdownlist, followed by a DataGrid with results extracted from a database. Once the dropdownlist is changed, the results in the datagrid should change to reflect the choice, however, despite my attempts the DropDownList seems to Postback, but not change the results in the DataGrid.
Code is detailed below ... anyone's help would be greatly appreciated. :)
Sample:
ASCX Control HTML ....
<table><tr><td><asp:placeh older id="ResultsSpan" runat="server"></asp:place holder>
<ASP:DATAGRID id="ResultsDataGrid" runat="server" AutoGenerateColumns="True" Width="100%" CssClass="moduleBody" ShowFooter="false" OnPageIndexChanged="myData Grid_PageC hanger" PagerStyle-Mode="NumericPa ges" PageSize="10" AllowPaging="True" EnableViewState="true" AllowSorting="true" SortCommand="SortGrid">
</ASP:DATAGRID></td></tr>< /table>
C# Code Behind
public class myDataTest : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls. PlaceHolde r ResultsSpan;
protected System.Web.UI.WebControls. DataGrid ResultsDataGrid;
protected System.Web.UI.WebControls. DropDownLi st SelectStatus;
private string status = "Not Selected"
private void Page_Load(object sender, System.EventArgs e)
{
if(Page.IsPostBack == false)
{
PopulateStatus();
}
BindGrid();
}
public void SelectStatus_SelectedIndex Changed(ob ject sender, System.EventArgs e)
{
string status = SelectStatus.SelectedItem. Value;
BindGrid();
}
public void BindGrid(string inStatus)
{
//get a dataset from component
myApplication.ContactDB db = myApplication.ContactDB();
DataSet ds = new DataSet();
ds = db.GetResults();
DataView view = new DataView(ds.Tables[0]);
//Sorting criteria
view.Sort = SortField; if (!SortAscending) { view.Sort += " DESC"; }
ResultsDataGrid.DataSource = view;
ResultsDataGrid.DataBind() ;
}
// using ViewState for SortField property
string SortField
{ get
{
object o = ViewState["SortField"];
if (o == null) { return String.Empty; }
return (string)o;
} set
{
if (value == SortField)
{ //if ascending change to descending or vice versa.
SortAscending = !SortAscending; }
ViewState["SortField"] = value;
}
}
// using ViewState for SortAscending property
bool SortAscending
{
get
{
object o = ViewState["SortAscending"] ;
if (o == null) { return true; } return (bool)o;
}
set { ViewState["SortAscending"] = value; }
}
protected void SortGrid(Object src, DataGridSortCommandEventAr gs e)
{
ResultsDataGrid.CurrentPag eIndex = 0;
SortField = e.SortExpression;
BindGrid();
}
protected void myDataGrid_PageChanger(Obj ect src, DataGridPageChangedEventAr gs e)
{
ResultsDataGrid.CurrentPag eIndex = e.NewPageIndex;
BindGrid();
}
public DataSet PopulateStatus()
{
DataSet ds = new DataSet();
myApplication.ContactDB db = new myApplication.ContactDB();
SelectStatus.DataSource = db.GetStatusList();
SelectStatus.DataTextField = "STATUS";
SelectStatus.DataValueFiel d = "STATUS";
SelectStatus.DataBind();
return ds;
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.SelectStatus.Selected IndexChang ed += new System.EventHandler(this.S electStatu s_Selected IndexChang ed);
this.Load += new System.EventHandler(this.P age_Load);
}
#endregion
}
}
I have a page which displays a dropdownlist, followed by a DataGrid with results extracted from a database. Once the dropdownlist is changed, the results in the datagrid should change to reflect the choice, however, despite my attempts the DropDownList seems to Postback, but not change the results in the DataGrid.
Code is detailed below ... anyone's help would be greatly appreciated. :)
Sample:
ASCX Control HTML ....
<table><tr><td><asp:placeh
<ASP:DATAGRID id="ResultsDataGrid" runat="server" AutoGenerateColumns="True"
</ASP:DATAGRID></td></tr><
C# Code Behind
public class myDataTest : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
private string status = "Not Selected"
private void Page_Load(object sender, System.EventArgs e)
{
if(Page.IsPostBack == false)
{
PopulateStatus();
}
BindGrid();
}
public void SelectStatus_SelectedIndex
{
string status = SelectStatus.SelectedItem.
BindGrid();
}
public void BindGrid(string inStatus)
{
//get a dataset from component
myApplication.ContactDB db = myApplication.ContactDB();
DataSet ds = new DataSet();
ds = db.GetResults();
DataView view = new DataView(ds.Tables[0]);
//Sorting criteria
view.Sort = SortField; if (!SortAscending) { view.Sort += " DESC"; }
ResultsDataGrid.DataSource
ResultsDataGrid.DataBind()
}
// using ViewState for SortField property
string SortField
{ get
{
object o = ViewState["SortField"];
if (o == null) { return String.Empty; }
return (string)o;
} set
{
if (value == SortField)
{ //if ascending change to descending or vice versa.
SortAscending = !SortAscending; }
ViewState["SortField"] = value;
}
}
// using ViewState for SortAscending property
bool SortAscending
{
get
{
object o = ViewState["SortAscending"]
if (o == null) { return true; } return (bool)o;
}
set { ViewState["SortAscending"]
}
protected void SortGrid(Object src, DataGridSortCommandEventAr
{
ResultsDataGrid.CurrentPag
SortField = e.SortExpression;
BindGrid();
}
protected void myDataGrid_PageChanger(Obj
{
ResultsDataGrid.CurrentPag
BindGrid();
}
public DataSet PopulateStatus()
{
DataSet ds = new DataSet();
myApplication.ContactDB db = new myApplication.ContactDB();
SelectStatus.DataSource = db.GetStatusList();
SelectStatus.DataTextField
SelectStatus.DataValueFiel
SelectStatus.DataBind();
return ds;
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.SelectStatus.Selected
this.Load += new System.EventHandler(this.P
}
#endregion
}
}
Move BindGrid in method Page_Load to line below PopulateStatus(); in if not post back.
ASKER
Hi Dungla,
Thanks for the comment ... I moved the BindGrid as per your suggestion, but there is no change. The page initially loads ok, and the Datagrid contains results for the default selection ("Not Selected"). But when the DropDownList is changed to one of the other options in the DropDownList, the page flickers, then returns to the default selection, causing the DataGrid to once again display the same results.
Thanks,
Thanks for the comment ... I moved the BindGrid as per your suggestion, but there is no change. The page initially loads ok, and the Datagrid contains results for the default selection ("Not Selected"). But when the DropDownList is changed to one of the other options in the DropDownList, the page flickers, then returns to the default selection, causing the DataGrid to once again display the same results.
Thanks,
ASKER
Note one other correction to BindGrid() as follows:
public void BindGrid() // removed the parameter
{
//get a dataset from component
myApplication.ContactDB db = myApplication.ContactDB();
DataSet ds = new DataSet();
ds = db.GetResults(status); // status passed to component to define results.
//.....
}
public void BindGrid() // removed the parameter
{
//get a dataset from component
myApplication.ContactDB db = myApplication.ContactDB();
DataSet ds = new DataSet();
ds = db.GetResults(status); // status passed to component to define results.
//.....
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Dungla,
Thanks for your input. :)
After much troubleshooting, I have found the error was caused by another .ascx which was in loaded into the same page. In the other .ascx it contained a Form, which posted to a different URL, and I have found that once I removed the Form element, everything works fine. My DataGrid now sorts, pages, and can be filtered using a DropDownList.
Is there a rule with ASP.NET that all forms postback to the same page, or a restriction on the number of Form elements allowed on a Page?
Thanks,
Thanks for your input. :)
After much troubleshooting, I have found the error was caused by another .ascx which was in loaded into the same page. In the other .ascx it contained a Form, which posted to a different URL, and I have found that once I removed the Form element, everything works fine. My DataGrid now sorts, pages, and can be filtered using a DropDownList.
Is there a rule with ASP.NET that all forms postback to the same page, or a restriction on the number of Form elements allowed on a Page?
Thanks,
As far as i know, it is impossible
ASKER
<asp:dropdownlist id="SelectStatus" runat="server" AutoPostBack="True" OnSelectedIndexChanged="Se