|
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
///<summary>
/// Steve Lydford - 12th May 2008.
///
/// This class demonstrates how to persist AutoPostback DropDownLists
/// without the use of an AJAX partial page update or database. This
/// means that you are able to redirect to a page to refresh a GridView
/// or navigate away and back to a page and persist DropDownList values.
///</summary>
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if ((Session["fillForm"] != null) && (Session["fillForm"].ToString() == "true"))
{
// fill dropdowns first then set the selectedValue.
// DropDownList1 items collection filled at design time.
fillDropDown(DropDownList2);
fillDropDown(DropDownList3);
DropDownList1.SelectedValue = Session["ddl1"].ToString();
DropDownList2.SelectedValue = Session["ddl2"].ToString();
DropDownList3.SelectedValue = Session["ddl3"].ToString();
Session["fillForm"] = "false";
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Session["ddl1"] = DropDownList1.SelectedValue;
fillDropDown(DropDownList2);
if ((Session["ddl2"] != null) && (Session["ddl2"].ToString() != ""))
DropDownList2.SelectedValue = Session["ddl2"].ToString();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
Session["ddl2"] = DropDownList2.SelectedValue;
fillDropDown(DropDownList3);
if ((Session["ddl3"] != null) && (Session["ddl3"].ToString() != ""))
DropDownList3.SelectedValue = Session["ddl3"].ToString();
}
///<summary>
/// Fills a DropDownList with the numbers 1 to 10.
///</summary>
protected void fillDropDown(DropDownList ddl)
{
ddl.Items.Clear();
for (int i = 1; i <= 10; i++)
ddl.Items.Add(i.ToString());
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["fillForm"] = "true";
Session["ddl3"] = DropDownList3.SelectedValue;
Response.Redirect("default.aspx");
}
}
|