1

Below is a repro for a bug that I came across while reviewing some code.

aspx page:

<asp:DropDownList ID="ddlMain" runat="server" Visible="False" />
<asp:Button ID="btnSelect" runat="server" Text="Select" />

Code behind file:

protected void Page_Load(object sender, EventArgs e)
{                
            ddlMain.SelectedIndex = 0;
}

Note that ddlMain has visible=False. On page load if I assign selectedIndex = 0; the selectedIndex value doesn't change and remain -1.

the button is there to enable postback; on postback the above statement fails and the following exception is raised:

'ddlMain' has a SelectedIndex which is invalid because it does not exist in the list of items. Parameter name: value

Why didn't the exception get raised the first time around? And why does it get raised after postback?

I noticed that even though the rendersize for the dropdown is 0; there are still 12 bytes assigned in viewstate - though i couldn't verify what exactly was stored for the control. If i disable viewstate then the exception doesn't get raised after postback.

just.another.programmer
  • 8,579
  • 8
  • 51
  • 90
NoviceProgrammer
  • 3,347
  • 1
  • 22
  • 32
  • Show us the code for Button Click. – Priyank Patel Oct 26 '12 at 09:49
  • nothing happens in button click – NoviceProgrammer Oct 26 '12 at 09:51
  • 1
    I know that assignment of `.SelectedValue` will only cause an exception on the post-back ([details here on MSDN](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.selectedvalue.aspx), which states "The selected value is not in the list of available values and view state or other state has been loaded (a postback has been performed)"), but there is no such statement for the `.SelectedIndex` property. The only other thing I could think of is whether the viewstate for the dropdown is turned off, therefore there are no items to select on the post-back – freefaller Oct 26 '12 at 10:38
  • actually its similar to this question [see this link][1] [1]: http://stackoverflow.com/questions/5656058/asp-net-dropdownlist-resets-selectedindex-after-postback – immayankmodi Oct 26 '12 at 10:42
  • @freefaller thanks for sharing that. Wonder if the same is applicable for selectedIndex, even though its not explicitly mentioned anywhere. – NoviceProgrammer Oct 26 '12 at 20:29
  • @NoviceProgrammer - did you get any further into this? Does it look like .SelectedIndex is doing the same exception pattern, but un-documented? Or is it a case that the data simply isn't there to select? – freefaller Oct 29 '12 at 14:12
  • @freefaller tried finding out more but no useful updates to share. To me it does look like the SelectedIndex is working in the same manner as SelectedValue – NoviceProgrammer Oct 30 '12 at 09:33

1 Answers1

0

Does this give you the same error?

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
        ddlMain.SelectedIndex = 0;
}

On postback it loads the viewstate values and it tells the dropdownlist that it doesn't have any values. Then you give it a selected index of 0, which doesn't exist.

Razvan Trifan
  • 534
  • 2
  • 6
  • this will not get executed on postback so there won't be any error. I am just curious to know why it doesnt fail on the initial page load. – NoviceProgrammer Oct 26 '12 at 09:54
  • on the initial load it doesn't load the viewstate. see here http://i.msdn.microsoft.com/dynimg/IC152667.gif – Razvan Trifan Oct 26 '12 at 09:56
  • yes I know. After rendering I see 12 bytes in viewstate. If i disable viewstate the exception doesnt occur. Why doesn't it occur the first time round as there is no item at index 0. As i mentioned the selected index is shown as -1 even after the assignment. – NoviceProgrammer Oct 26 '12 at 09:59