0

I have 2 pages, both with 3 similar DropDownLists. The first DropDownList needs to be populated before selecting the second, and the second before the third (which then populates a gridview).

Usually, a user will view data on one page, click a button, and then view data on the next page (usually with the same DropDownList selections). Is there any way to have the dropdownlists pre-populated with their old selections?

Marcus Vinicius
  • 1,891
  • 1
  • 19
  • 35

3 Answers3

2

You can pass values from one page to the other using the PreviousPage parameter that asp.net provides you. A small example:

You set the second page on the submit button as:

PostBackUrl="SecondPage.aspx"

On SecondPage.aspx you declare where you can get informations

<%@ PreviousPageType VirtualPath="~/FirstPage.aspx" %>

and you get them by...

if (Page.PreviousPage != null)
{
    if(Page.PreviousPage.IsCrossPagePostBack == true)
    {
        // and get the controls of the previous page as
        var SomeVariable = PreviousPage.DropDownlListId.SelectedValue;
    }
}

Some reference.
Cross-Page Posting in ASP.NET Web Pages
ASP.NET how to access public properties?
Cross-page postbacks and back again retaining data from source page
Cross-page posting. Is it a good pratice to use PreviousPage in Asp.net?

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • Now how do I get my DropDownList to be publicly accessible? The error message is "DDL is inaccessible due to its protection level." – digitalfrenchfry Feb 01 '13 at 15:56
  • 1
    @digitalfrenchfry Make a public variable that access them, eg `Public DropDownList getDDList { get{DropDownListID;}}` – Aristos Feb 01 '13 at 16:10
  • I think we're getting there, but still not quite. Here's what i ended up with... public string getWeekOf { get { return ddl_selectDate.SelectedValue; } } if (Page.PreviousPage != null) { string WeekOf = PreviousPage.getWeekOf; List objList = new List(); objList = new mainSQL().getList(WeekOf); GridView.DataSource = objList; GridView.DataBind(); } – digitalfrenchfry Feb 01 '13 at 17:09
  • @digitalfrenchfry I forget a return... is not that hard, make some tries and you going to find it `public DropDownList getDDList { get{ return DropDownListID;}}` – Aristos Feb 01 '13 at 17:11
  • I've asked a new (relevant) question, because i cant get it right, if you want to give that one a shot :) – digitalfrenchfry Feb 01 '13 at 17:32
0

If you have the form where the button that the users click post to the second page, the second page can look for the values of the DDL and set them on page load.

Shai Cohen
  • 6,074
  • 4
  • 31
  • 54
0

There is a way:

  1. pass selections to the second page either via Session or Query String
  2. populate first dropdown
  3. select appropriate item
  4. repeat 2-3 for the rest of dropdowns

If you're looking for a magic solution that just "does that", I don't think this is possible.

p.s. You might rethink your design (if this is an option) and have the grid on the first page; bind the grid when user selects a value from the last dropdown (make sure you have AutoPostback set to True on your last dropdown in order to trigger the final postback)

roman m
  • 26,012
  • 31
  • 101
  • 133