0

OK so i am a bit lost and could do with some help.

I am creating a program that a user inputs data into a form on the default page (I have that working).

Then i use session variables to get the data input from a text box on the default page and place that data into a drop down menu on Page2 (I have that working).

What im trying to do now is use the data selected from the drop down on the page2 and output it on to a label. any help would be appreciated.

Page2 code bellow session that populates drop down

public partial class About : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


            MyFruit = Session["Fruitname"] as List<string>;
            //Create new, if null
            if (MyFruit == null)
                MyFruit = new List<string>();
            DropDownList1.DataSource = MyFruit;
            DropDownList1.DataBind();


        }
Beep
  • 2,737
  • 7
  • 36
  • 85

2 Answers2

2

You can use SelectedIndexChanged event of DropDownList to handle this. your AutoPostBack property of DropDownBox should be set to True

sample code as below:

Design code: page.aspx

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        <asp:ListItem>name1</asp:ListItem>
        <asp:ListItem>name2</asp:ListItem>
    </asp:DropDownList>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

CodeBehind File: page.aspx.cs

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Label1.Text = DropDownList1.SelectedValue.ToString();
        }
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
  • Thank you so much, so simple when you see it. i was up till late last night working on that problem. – Beep Nov 10 '13 at 11:20
1

Not sure if this is what you are looking for but what I am guessing is you want an event for your drop down list to get the info and place it into a session to pass onto the next page

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    string item=DropDownList.SelectedItem;
    Session["selectedItem"]=item;
    Response.Redirect("TheNextPageURL")
}

public partial class TheNextPage : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Session["selectedItem"]!=null)
        {
            Label1.Text=Session["selectedItem"].toString();
        }
    }
}

Hope that helps

Zane Chung
  • 154
  • 6
  • Thank you for your answer, is very helpful. @Sudhakar has already provided the solution i required but i will try yours on a back up see if i can use it. – Beep Nov 10 '13 at 11:33