0

I have an asp dropbox which I populate from database:

this is the aspx

<asp:DropDownList runat="server" ID="ddl_last" AppendDataBoundItems="true">

and this is the cs:

  ddl_last.DataSource = eggsContext.Customers;
        ddl_last.DataValueField = "last_name";
        ddl_last.DataTextField = "last_name";
        ddl_last.DataBind();

When I try to get ddl_last.SelectedValue from the c# code I get nothing. infact ddl_last.Items count is 0! When I don't populate it from code, but write in the aspx

<asp:ListItem Text="example" />

everything works.

I can get the value from javascript by writing document.getElementById('ddl_last').value;

But I was wondering what did microsoft screw up this time that this is not working from the code behind?

Nick Rolando
  • 25,879
  • 13
  • 79
  • 119
BornToCode
  • 9,495
  • 9
  • 66
  • 83
  • Can you confirm that your drop down list is in fact populated when you try to populate it in your code behind? – Eric Jun 19 '12 at 16:20
  • Please tell how you define `eggsContext.Customers` – Nick Rolando Jun 19 '12 at 16:21
  • @autumyst - all values appear on the page, from javascript I CAN access the new populated values, what other confirmation do you need? :) – BornToCode Jun 19 '12 at 16:21
  • 1
    The values from `eggsContext.Customers` appear in the dropdown, but are not retrievable on postback? – Nick Rolando Jun 19 '12 at 16:23
  • @Shredder - using entity framework - myEntities eggsContext; EggsContext = new myEntities(); – BornToCode Jun 19 '12 at 16:24
  • @Shredder - even before postback they are not retrievable. I set a breakpoint on a button press and checked the values of the dropdown – BornToCode Jun 19 '12 at 16:25
  • 2
    by the time the button press event triggers, postback already occurs. – Eric Jun 19 '12 at 16:27
  • @autumyst - Ok, you led me to the answer.. when I changed in the the property EnableViewState to true it worked. But if I'd like to stick with viewstate disabled on page, and just add to the asp:DropDownList the following - EnableViewState="true" ViewStateMode="Enabled" it still doesn't work, can I enable viewstate only for specific controls? – BornToCode Jun 19 '12 at 16:40
  • you should be able to, however, I'd suggest trying to find a more graceful solution. I had an issue which I tried to resolve simply by changing the viewstate, however, it may cause you problems in the future when you expect a behaviour, but you aren't getting it. – Eric Jun 19 '12 at 16:49

2 Answers2

1

After seeing your issue in comment, I think this may help you resolve it:

To disable view state for a page and to enable it for a specific control on the page, set the EnableViewState property of the page and the control to true, set the ViewStateMode property of the page to Disabled, and set the ViewStateMode property of the control to Enabled.

http://msdn.microsoft.com/en-us/library/system.web.ui.control.viewstatemode.aspx

Note also, with ViewState being disabled, you can still retrieve the posted value by

if (Request["ddl_last"] != null)
    val = Request["ddl_last"];
Nick Rolando
  • 25,879
  • 13
  • 79
  • 119
0

Looking at the issue it seems that when you try to access ddl_last.SelectedValue and ddl_last.Items count your ddl_last list has not been data bound. Make sure you are accessing those values after you have bound data

DSharper
  • 3,177
  • 9
  • 29
  • 47