我用自带的SqlDataSource绑定DropDownList后,如下:
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="ct_Name" DataValueField="ct_ID"> </asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SqlConnectionString %>" SelectCommand="SELECT * FROM [db]">
为什么DropDownList1.Items.Count.ToString()的值为0??但是结果页面是正常的.
导致ListItem item = DropDownList1.Items.FindByValue(sParentID.ToString());一直都是无法找相应的值.
如果我用手动编辑DropDownList ,如下:
    <asp:DropDownList ID="DropDownList2" runat="server">
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>
        <asp:ListItem>3</asp:ListItem>
    </asp:DropDownList>
为什么这个能得到值??难道非要我手写代码进行绑定DropDownList??新手分不多啊,意思意思就行了,大家见谅!!

解决方案 »

  1.   

    你是在哪里取DropDownList1.Items.Count.ToString()的值为0的?
      

  2.   

    读取的时间不对,要先绑定数据
    DropDownList1.DataBind();
    Label1.Text=  DropDownList1.Items.Count.ToString();
      

  3.   

    我倒,原来用自带的SqlDataSource绑定DropDownList1,取值前也要DataBind(); 一下...
    懂了,谢谢2楼!
    DropDownList1.DataBind(); 
    Label1.Text=  DropDownList1.Items.Count.ToString();