我为DropDownList绑定了数据库,可以正常显示,但是无法返回选中的下拉项的内容
既单击ImageButton1弹出的窗体内容为空代码如下:
private void Page_Load(object sender, System.EventArgs e)
{
         if(!IsPostBack)
{
DropDownList1_connect();
}
}private void DropDownList1_connect()
{
try
{
string strConn="server=127.0.0.1;uid=sa;pwd=;database=sa";
SqlConnection Connection=new SqlConnection();
Connection.ConnectionString=strConn;
string strSQL="select [name] from [phone] where [father]=0";
DataSet ds =new DataSet();
SqlDataAdapter Command=new SqlDataAdapter(strSQL,Connection);
Command.Fill(ds,"database");
DropDownList1.DataSource=ds.Tables["database"].DefaultView;
DropDownList1.DataValueField=ds.Tables["database"].Columns[0].ColumnName;
DropDownList1.DataTextField=ds.Tables["database"].Columns[0].ColumnName;
DropDownList1.DataBind();
Connection.Close();
}
catch
{
Response.Write("<script language='javascript'>alert('服务器忙,请稍后再试!');</script>");
Response.Write("<script language='JavaScript'>window.location='http://127.0.0.1';</script>");
}
}private void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
          Response.Write("<script language='javascript'>alert('"+DropDownList1.SelectedValue+"');</script>");
          Response.Write("<script language='JavaScript'>window.location='default.aspx';</script>");请各位大大帮帮忙,谢谢了
}

解决方案 »

  1.   

    看看aspx里是否有<%@ Page EnableViewState="true"%>
      

  2.   

    看看你的页面和dropdownlist是否禁用了视图状态viewstate?
      

  3.   

    如果在DropDownList1_connect()里加上
    Response.Write("<script language='javascript'>alert('"+DropDownList1.SelectedValue+"');</script>");
    比如
    ……
    DropDownList1.DataBind();
    Response.Write("<script language='javascript'>alert('"+DropDownList1.SelectedValue+"');</script>");
    ……
    则可以显示值,不过是第一个的
      

  4.   

    Response.Write("<script language='javascript'>alert('"+DropDownList1.SelectedValue+"');</script>");
              Response.Write("<script language='JavaScript'>window.location='default.aspx';</script>");会不会是因为你执行完以后又重定向到default.aspx了,所以跟没点是一样的呢?
      

  5.   

    你可以先不用数据库挷定,直接用Item试一下,看看有没有问题,如果这样能正常显示的话就说明是数据挷定的问题,也可能是你的数据库里字段是空的。
      

  6.   

    to  gsxzm(肖明)
    如果在DropDownList1_connect()里加上
    Response.Write("<script language='javascript'>alert('"+DropDownList1.SelectedValue+"');</script>");
    比如
    ……
    DropDownList1.DataBind();
    Response.Write("<script language='javascript'>alert('"+DropDownList1.SelectedValue+"');</script>");
    ……
    能够正确的显示里面的值
    也就是说,在绑定里可以读取数据,虽然读的是第一行
    但是,写在别的控件里面,返回的就是空值或者是NULL,反正什么都没有,就弹出个窗体
    不知道为什么
      

  7.   

    如果DropDownlist的数据绑定没有问题的话try:
     Response.Write("<script>window.alert('"+DropDownList1.SelectedValue.ToString()+"');location='default.aspx';</script>");
    我试过,没问题的
      

  8.   

    to wangdequan1024(紫夜) 
    我也觉的应该没什么问题
    可是我写在ImageButton1_Click里就是没反映,是不是哪里出错了啊
      

  9.   

    是否将AutoPostBack属性设为true!!
      

  10.   

    发现原因的
    因为我把DropDownlist放在Panel里面了,所以没反映
    只要把DropDownlist拉出来,放在外面就行了,晕了
    不知道这是为什么啊?