Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.

Tuesday, December 23, 2008

Keeping a DropDownList's DataSource Updated

The DropDownList does not manage selected state like other controls. Therefore, I use a Session variable to store an index to keep track of the currently selected item:

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Session["listId"] = ddlList.SelectedValue;
GridViewStocksInLists.DataBind();
}
}

Now, I add the SelectedIndexChanged event with the following code:

protected void ddlList_SelectedIndexChanged(object sender, EventArgs e)
{
Session["listId"] = ddlList.SelectedValue;
GridViewStocksInLists.DataBind();
}

Reference: http://www.velocityreviews.com/forums/t123449-problem-with-formview-and-dropdownlists.html

No comments: