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

后端 未结 2 1314
逝去的感伤
逝去的感伤 2020-12-20 13:31

This piece of code


    

        
相关标签:
2条回答
  • 2020-12-20 13:43

    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'

    0 讨论(0)
  • 2020-12-20 13:48

    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";
    
    0 讨论(0)
提交回复
热议问题