DropDownList1.Text = "男";
这样可以设置,但是,读取数据库这样
DropDownList1.Text = read["Sex"].ToString();
就不能设置了。这样
string str = read["Sex"].ToString();
DropDownList1.Text = str;
也不可以。为什么啊?我
Response.Write(str);
是有东西的。这个如何解决,先谢过了

解决方案 »

  1.   

    ListItem x = DropDownList1.Items.FindByText(read["Sex"].ToString());
    if(x!=null) x.Selected=true;
      

  2.   

    ddl.Items[i].Selected = true;
      

  3.   

     read["Sex"].ToString()的值在DropDownList1中没有相同value的元素
      

  4.   

    DropDownList1.Items.FindByText(read["Sex"].ToString()).Selected = true;
      

  5.   

    Response.Write(str);
    是有东西的这东西在DropDownList元素里面,有匹配的元素吗,你看了没有,注意是和<asp:ListItem Value="c">啊啊啊</asp:ListItem>元素的Value匹配如果有匹配的,你可以处理一下str的空白字符,如str.Trim();
      

  6.   


    也是不行,加个else后会执行else的代码,但是Response.Write(read["Sex"].ToString())的值是"男"。前台代码是这个,为什么会是null呢?
    <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem Text="保密" Value="保密">保密</asp:ListItem>
        <asp:ListItem Text="男" Value="男">男</asp:ListItem>
        <asp:ListItem Text="女" Value="女">女</asp:ListItem>
    </asp:DropDownList>后台string strSex = read["Sex"].ToString();
    Response.Write(strSex);   // 输出"男"
    ListItem x = DropDownList1.Items.FindByText(strSex);
    if (x != null)
        x.Selected = true;
    else
        Response.Write("sdfsdf"); // 执行这段代码
      

  7.   


    没有执行else里面的代码,你在哪里执行的?