Steve Lydford on .Net

Sticking all my useful bits in one place in the hope that I may find them again one day!
posts - 4, comments - 0, trackbacks - 0

My Links

News

Hi. This just a collection of useful bits and pieces that I am posting here so that I might be able to find them again in the future. Feel free to browse through and use anything that may be of some help to you.

Archives

Post Categories

Persist AutoPostback DropDownLists on Page Refresh


Here we go for starters. ;)

The following C# code will allow you to persist AutoPostback DropDownList values on an ASP.Net page, without the use of an AJAX partial page update or a database. It simply uses session variables to persist the values which would otherwise be lost upon navigating away from and back to a page or by redirecting the browser to itself to refresh a databound control, such as a GridView.

This was a problem for me on a page that contained three DropDownLists, each of which was populated as a result of the SelectedValue on AutoPostback of the DropDownList above. Once the third DropDownList was selected a GridView was populated, then a details panel containing various user input controls and a 'Save' button was displayed on selection of a row in the GridView. When the 'Save' button was pressed the GridView was not updating and would only update if the page was refreshed. However, a simple redirect to the page as part of the Save button click event meant that the users selections in the three drop downs were lost. The following code was used to solve this problem and assumes three DropDownLists (DropDownList1, 2 & 3) and a Button (Button1).

 

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

Print | posted on Monday, May 12, 2008 1:56 PM | Filed Under [ C# ASP.Net ]


Feedback

No comments posted yet.


Post Comment

Title  
Name  
Email
Url
Comment   
Please add 7 and 7 and type the answer here:

Powered by: