11

This piece of code

<asp:DropDownList runat="server" ID="testdropdown" SelectedValue="2">
    <asp:ListItem Text="1" Value="1"></asp:ListItem>
    <asp:ListItem Text="2" Value="2"></asp:ListItem>
    <asp:ListItem Text="3" Value="3"></asp:ListItem>
</asp:DropDownList>

yields this error:

The 'SelectedValue' property cannot be set declaratively.

Yet, this is a legal and commonly used edit template for databound GridViews. The SelectedValue attribute certainly appears to be declaratively set here.

<EditItemTemplate>
    <asp:DropDownList runat="server" 
        ID="GenreDropDownList"
        DataSourceID="GenreDataSource" 
        DataValueField="GenreId"
        DataTextField="Name"
        SelectedValue='<%# Bind("Genre.GenreId") %>'>
    </asp:DropDownList>
</EditItemTemplate>

The question is: what is the difference between the cases when you are allowed to set it declaratively and those in which you are not? The error message implies that it's never allowed.

recursive
  • 77,417
  • 29
  • 137
  • 228

2 Answers2

9

in markup use SelectedValue='<%# "32" %>' syntax .(note the order of single and then the double quotes in the following example ):

 <asp:DropDownList  ID="ddlField" SelectedValue='<%# "32" %>' 
   runat="server"   DataTextField="Name" DataValueField="ID"  >
  </asp:DropDownList>

or in code-behind just after DataBinding .(example):

  ddlField.DataSource = Fields.SelectAll();
  ddlField.DataBind();           
  ddlField.SelectedValue = "32";
Iman
  • 15,560
  • 6
  • 70
  • 83
  • 1
    The "markup" code doesn't work if the items are set at design time vs doing a databind call to populate – NotMe Aug 22 '13 at 15:46
  • 1
    Thanks, I had forgotten the # sign. – Ben Jan 14 '15 at 18:34
  • 1
    Thanks, Iman. It works for me :) I looked at many threads and yours is the first and only post to spell out exactly how to use an integer to the set the DropDownList default SelectedValue entirely in mark-up. I thought this should be possible but I must have previously gotten the format slightly wrong - perhaps not quoting the integer. – Zeek2 Dec 20 '19 at 08:12
7

It means you cannot set it through the designer.

The correct way is:

<asp:DropDownList runat="server" ID="testdropdown">
    <asp:ListItem Text="1" Value="1"></asp:ListItem>
    <asp:ListItem Text="2" Value="2" Selected></asp:ListItem>
    <asp:ListItem Text="3" Value="3"></asp:ListItem>
</asp:DropDownList>

The reason the bound method works is because the value isn't selected in design mode but at runtime after the control is bound to a datasource

The DropDownList.SelectedValue method is meant to be applied at runtime hence the error about not being able to set it 'decoratively'

Gautam
  • 2,035
  • 1
  • 24
  • 24
  • That would answer my question, if it weren't for the second part of my question. Clearly, in certain cases, you are allowed to declaratively set SelectedValue, as I illustrated. I am trying to understand the difference between the cases when you are allowed and not. – recursive Feb 24 '09 at 04:42
  • 1
    Because in the gridview template you haven't actually set a value yet using the bind method. The bind actually happens at runtime. It's like saying "At runtime, when I bind data to it, select a value based on this field" – Gautam Feb 24 '09 at 12:32