1

I have a DropDownList with button:

<form id="form1" runat="server">
      <asp:DropDownList ID="DropDay" runat="server"/>
      <input type="submit" value="Save" runat="server" id="btn"/>
</form>

C# code for the page:

protected void Page_Load(object sender, EventArgs e)
{
    btn.ServerClick+=new EventHandler(btn_ServerClick);
}

void btn_ServerClick(object sender, EventArgs e)
{
    HttpContext value= HttpContext.Current;
    value.Items.Add("Day", DropDay.SelectedValue);
    Server.Transfer("ASP_2.aspx");
}

In ASP_2 I have a label, that I need to set the selected Dropdownlist value. And here is the code for ASP_2.aspx:

protected void Page_Load(object sender, EventArgs e)
{
    HttpContext value= HttpContext.Current;
    lblDay.Text = value.Items["Day"].ToString();
}

The problem is, that the value always is the first item from the dropdown. How do I set the selected value?

EDIT

the way I am filling the dropdown list The code goes in Page_Load:

DropDay.Items.Clear();
for (int i = 1; i <= 10; i++)
{
    DropDay.Items.Add(i.ToString());
}
Danny Varod
  • 17,324
  • 5
  • 69
  • 111
Brezhnews
  • 1,559
  • 2
  • 20
  • 37
  • All looks good in that code, the problem is how you fill the drop down list and why the selectedValue is the first every time ? Maybe you fill it again on post back ? – Aristos Feb 24 '13 at 20:31
  • have you tried DropDay.SelectedText ? are you sure you are assigning the right value? also try [this](http://stackoverflow.com/questions/11149282/post-asp-net-form-data-to-another-page) post – Saeid Farivar Feb 24 '13 at 20:32
  • At which time do you do your DropDownList binding ? If it is done on page_load and does not handle ispostback condition, your selected value will be reset before the button_click is processed. You should probably bind your list during prerender, on !PostBack – jbl Feb 24 '13 at 20:38
  • @jbl Edited the post, now showing how the list is filled – Brezhnews Feb 24 '13 at 20:42

5 Answers5

1

Your code for filling the DDL should be (at least) :

if (!IsPostBack)
{
  DropDay.Items.Clear();
      for (int i = 1; i <= 10; i++)
      {
          DropDay.Items.Add(i.ToString());
      }
}

Hope this will help

jbl
  • 15,179
  • 3
  • 34
  • 101
0

You can use this in first page:

Server.Transfer("ASP_2.aspx?dropdown="+DropDay.SelectedValue);


and in the second page

 protected void Page_Load(object sender, EventArgs e)
{
  string val=Request.QueryString["dropdown"].toString();
}
danarj
  • 1,798
  • 7
  • 26
  • 54
0

Maybe you left the autopostback property of the dropdownlist true.

Omareo
  • 123
  • 1
  • 5
0

Figured out it by myself, just needed to remove "DropDay.Items.Clear();"

    //DropDay.Items.Clear(); removed this line and the thing was fixed
for (int i = 1; i <= 10; i++)
{
    DropDay.Items.Add(i.ToString());
}
Brezhnews
  • 1,559
  • 2
  • 20
  • 37
  • This is not a good practice for standard asp.net webform with postback scenario. You are adding 10 more items to your list, making it a 20 items list. You don't see it because of the Server.Transfer You should work on the postback mechanism and page lifecycle. It will help you a lot when building more complex features – jbl Feb 24 '13 at 21:02
-1

Have you tried DropDay.SelectedItem.Text?

diogod
  • 151
  • 9