前端代码<td style="width: 253px; height: 32px;" class="Login2">
                        <asp:DropDownList ID="DDLArea" runat="server" AutoPostBack="True" Width="121px">
                            <asp:ListItem Value="100" Selected="True">长春市</asp:ListItem>
                            <asp:ListItem Value="101">吉林市</asp:ListItem>
                            <asp:ListItem Value="102">四平市</asp:ListItem>
                            <asp:ListItem Value="103">辽源市</asp:ListItem>
                            <asp:ListItem Value="104">通化市</asp:ListItem>
                            <asp:ListItem Value="105">白山市</asp:ListItem>
                            <asp:ListItem Value="106">松原市</asp:ListItem>
                            <asp:ListItem Value="107">白城市</asp:ListItem>
                            <asp:ListItem Value="108">延边州</asp:ListItem>
                        </asp:DropDownList>
                        <asp:DropDownList ID="DDLCity" runat="server" DataSourceID="SqlDataSource2" DataTextField="Name"
                            DataValueField="CityId" Width="116px">
                        </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:AgriMachineConnectionString %>"
                            SelectCommand="SELECT [CityId], [Name] FROM [tb_City] WHERE ([AreaID] = @AreaID)">
                            <SelectParameters>
                                <asp:ControlParameter ControlID="DDLArea" DefaultValue="100" Name="AreaID" PropertyName="SelectedValue"
                                    Type="Byte" />
                            </SelectParameters>
                        </asp:SqlDataSource>
                    </td>
后台Page_load中cs代码DDLArea.SelectedIndex = -1;
DDLArea.Items.FindByText(Dr["area"].ToString()).Selected = true;
DDLCity.SelectedIndex = -1;
DDLCity.Items.FindByText(Dr["city"].ToString()).Selected = true;问题描述:
DDLArea正常,但DDLCity出问题
调试后发现DDLCity的Items.count值为0如果去掉
DDLCity.SelectedIndex = -1;
DDLCity.Items.FindByText(Dr["city"].ToString()).Selected = true;
页面显示DDLCity是有数据项的请大家解释一下是什么原因

解决方案 »

  1.   

    因为count为0
    所以这句就会出错
    DDLCity.Items.FindByText(Dr["city"].ToString()).Selected = true;我想知道初始化DDLCity为什么是0
      

  2.   

    问题描述: 
    DDLArea正常,但DDLCity出问题 
    调试后发现DDLCity的Items.count值为0 // 说明调用的时候,数据根本没绑定到,(你是第一次开始就调试吧)如果去掉 
    DDLCity.SelectedIndex = -1; 
    DDLCity.Items.FindByText(Dr["city"].ToString()).Selected = true; //先不用去掉,搞个按钮会发一次,看有数据不
    页面显示DDLCity是有数据项的 请大家解释一下是什么原因
      

  3.   

    要保证ddlcity的绑定在你的代码:
    DDLCity.SelectedIndex = -1; 
    DDLCity.Items.FindByText(Dr["city"].ToString()).Selected = true; 
    的前面。
    建议楼主不用sqldatasource,用sql语句查询出来给dataset ,然后让ddlcity的数据源为dataset并绑定,然后再执行你写的那些代码!
    你的错误出现原因很可能是:在执行这段代码前,ddlcity还没有绑定。 
      

  4.   


    我也认为是这种原因另外一个问题是,在页面初始化时
    DDLCity是什么时候才会绑定?有可靠的依据吗?
      

  5.   

    DataSet ds=new DataSet();
    string sSQL="你的sql语句";
    SqlDataAdapter adr=new SqlDataAdapter(sSQL,你的数据库连接);
                            adr.Fill(ds);
    DDLCity.DataSource=ds;
    DDLCity.DataTextField="Name";
    DDLCity.DataValueField="CityId";
    DDLCity.DataBind();
    这段代码后面再:
    DDLCity.SelectedIndex = -1; 
    DDLCity.Items.FindByText(Dr["city"].ToString()).Selected = true; 
    同时要将你aspx页面上的sqldatasource去掉。
      

  6.   

    看了楼上说的很多,
    事实上很简单,楼主的代码打在Page_Load当然出错,
    这时DDLCity还没绑定,就去执行DDLCity.Items.FindByText当然出错拉!!
    如果一定要写在Page_Load中,要这样写:DDLCity.SelectedIndex = -1; 
    DDLCity.DataBind();
    DDLCity.Items.FindByText(Dr["city"].ToString()).Selected = true; 正常的应写在DDLCity的PreRender事件中
    DDLCity.Items.FindByText(Dr["city"].ToString()).Selected = true; 
    则DDLCity.DataBind();可不写!!
      

  7.   

    设置了 DataSourceID 属性的每个数据绑定控件会在ASP.NET页的PreRender事件中调用 DataBind 方法,而
    PreRender事件是发生在Page_Load之后的,详细的ASP.NET页的生命周期请参考我的博客:
    http://blog.csdn.net/ojlovecd/archive/2008/07/22/2691116.aspx
      

  8.   

    终于懂了原来是在PreRender里面谢谢 koukoujiayi,ojlovecd还有zhuanshen712