绑定DropDownList的SelectedIndex

DropDownList是放在FormView上的,后者绑定到一个数据源.

DropDownList的Items是代码设置的,我想把数据源中对应的值绑定到它的SelectedIndex属性,

代码如下:

<asp:FormView 
        ID="FromView1"
        DataKeyNames="ID"
        DataSourceID="myDataSource"
        AllowPaging="true"
        runat="server">
       <ItemTemplate>

      <h3><%# Eval("Date") %></h3>

      <asp:TextBox
        ID="txtDate"
        Text='<%# Bind("Date") %>'
        runat="server" />

      <asp:DropDownList ID="DropDownList1" runat="server">(如何绑定?)
                            <asp:ListItem>IC</asp:ListItem>
                            <asp:ListItem>1</asp:ListItem>
                            <asp:ListItem>2</asp:ListItem>
                            <asp:ListItem>3</asp:ListItem>
                            <asp:ListItem>4</asp:ListItem>
                            <asp:ListItem>5</asp:ListItem>
                            </asp:DropDownList>

      </ItemTemplate>

</formview>

<asp:ObjectDataSource
            ID="myDataSource"
            TypeName="ShanShan.myClass"
            SelectMethod="SelectAll"
            UpdateMethod="Update"
            InsertMethod="Insert"
            DeleteMethod="Delete"
            runat="server" >
       </asp:ObjectDataSource>

DropDownList 能像TextBox那样直接Bind绑定么?

望不吝赐教!

慕的地8271018
浏览 447回答 3
3回答

繁星点点滴滴

不可以实现,因为TextBox的Text属性有Bindable(true, BindingDirection.TwoWay) Attribute;而DropdownList的SelectedIndex没有指定此Attribute;所以不可做数据绑定。 你可以用反射工具看看源代码实现。

慕森王

这样行不行

手掌心

不行,只能通过后台代码实现

富国沪深

打开App,查看更多内容
随时随地看视频慕课网APP