populate dropdownlist

2019-06-12 08:51发布

Hi i need to populate a dropdownlist. I designed a datasource and assigned it to dropdownlist. The dropdown populated correctly. But the problem is that i need to add a default value say "default" at the starting of the dropdownlist( and this value default is not in the database.

I did this :

 <asp:DropDownList ID="classInstructor" runat="server" DataSourceID="SqlDataSource3" 
                DataTextField="InstrName" DataValueField="InstrName">

        <asp:ListItem Value="Default" Text="Default" Selected="True"></asp:ListItem>


        </asp:DropDownList>

But default doesn't show up on dropdown. Probably, the way i did was wrong. Can u let me know the best way to handle this.

2条回答
倾城 Initia
2楼-- · 2019-06-12 09:36

you cannot use this approach if the DropDownList is bound to a datasource at runtime this Default item you have in the markup at design time will be washed away in the binding.

what you need to do is an Insert after the call to the DataBind() method.

see here for the examples and more comments on this: Asp.net - Add blank item at top of dropdownlist

查看更多
冷血范
3楼-- · 2019-06-12 09:46

Set the AppendDataBoundItems property to true on the dropdown list and the items from the data source will appear after any ListItems you add in the markup e.g.

<asp:DropDownList ID="classInstructor" runat="server" DataSourceID="SqlDataSource3" 
    DataTextField="InstrName" DataValueField="InstrName" AppendDataBoundItems="true">
    <asp:ListItem Value="Default" Text="Default" Selected="True"/>
</asp:DropDownList>
查看更多
登录 后发表回答