ASP:DropDownList in ItemTemplate: Why is SelectedValue attribute allowed?

拈花ヽ惹草 提交于 2019-11-29 13:17:54

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'

in markup use SelectedValue='<%# "32" %> syntax .(note 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";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!