Multiple Calls to DropDownList SelectedIndexChange event

P

Paul C

Can anyone tell me why I am getting 2 calls to my SelectedIndexChange
event.



public class Browse : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.DropDownList DropDownConf;
protected DataSet BrowseOrders=null;
protected int ConfID;
private void Page_Load(object sender, System.EventArgs e)
{

if ( Session["ConfID"]==null)
Session["ConfID"] = 3;


if (!IsPostBack)
{
BindData(null);
}
//Conference Dropdown Menu
DataSet conferences = RegCache.ConferenceList();
DropDownConf.DataTextField = "ConfName";
DropDownConf.DataValueField = "ConfID";
DropDownConf.DataSource = conferences;
DropDownConf.DataBind();

ListItem item = new ListItem("- Choose Conference -", "");
DropDownConf.Items.Insert(0,item);


}
private void BindData (string orderBy)
{
ConfID = Convert.ToInt32(Session["ConfID"]);
orderBy =(orderBy==null)?"OrderID":blush:rderBy;
DataSet ds = null;
if (ViewState["BrowseDataSet"]==null)
{
ds = Data.DataAccess.BrowseOrders(ConfID); //create data set and add
to view state
ViewState.Add("BrowseDataSet",ds);
ds.Tables[0].DefaultView.Sort=orderBy;
}
else
{
ds=(DataSet)ViewState["BrowseDataSet"]; //get data set from view
state

ds.Tables[0].DefaultView.Sort=orderBy;
}
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();
}
public void SortDataGrid(Object sender, DataGridSortCommandEventArgs
e)
{
BindData(e.SortExpression);

}
#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);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DropDownConf.SelectedIndexChanged += new
System.EventHandler(this.DropDownConf_SelectedIndexChanged);

this.Load += new System.EventHandler(this.Page_Load);

}
#endregion




public void DropDownConf_SelectedIndexChanged(object sender,
System.EventArgs e)
{
Session["ConfID"] = DropDownConf.SelectedItem.Value;
ViewState.Remove("BrowseDataSet");
BindData(null);


}
}

Thanks,
Paul
 
A

Angrez Singh

Hi,

Look at the following code snippet:

private void InitializeComponent()
{
this.DropDownConf.SelectedInde­xChanged += new

System.EventHandler(this.DropD­ownConf_SelectedIndexChanged);


this.Load += new
System.EventHandler(this.Page_­Load);


}

Here you are binding your dropdown selected Index Changed event with
the eventhandler. I am afraid that you might have done the same thing
in the corresponding aspx file also. So there are two calls one from
..aspx and other from .aspx.cs file.

Don't know why this happens but it happens only when you write the code
behind using C#. If you use VB.NET then it is intelligent enough to
make this call only once.

HTH,
Regards,
Angrez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top