我为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>");请各位大大帮帮忙,谢谢了
}
既单击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>");请各位大大帮帮忙,谢谢了
}
Response.Write("<script language='javascript'>alert('"+DropDownList1.SelectedValue+"');</script>");
比如
……
DropDownList1.DataBind();
Response.Write("<script language='javascript'>alert('"+DropDownList1.SelectedValue+"');</script>");
……
则可以显示值,不过是第一个的
Response.Write("<script language='JavaScript'>window.location='default.aspx';</script>");会不会是因为你执行完以后又重定向到default.aspx了,所以跟没点是一样的呢?
如果在DropDownList1_connect()里加上
Response.Write("<script language='javascript'>alert('"+DropDownList1.SelectedValue+"');</script>");
比如
……
DropDownList1.DataBind();
Response.Write("<script language='javascript'>alert('"+DropDownList1.SelectedValue+"');</script>");
……
能够正确的显示里面的值
也就是说,在绑定里可以读取数据,虽然读的是第一行
但是,写在别的控件里面,返回的就是空值或者是NULL,反正什么都没有,就弹出个窗体
不知道为什么
Response.Write("<script>window.alert('"+DropDownList1.SelectedValue.ToString()+"');location='default.aspx';</script>");
我试过,没问题的
我也觉的应该没什么问题
可是我写在ImageButton1_Click里就是没反映,是不是哪里出错了啊
因为我把DropDownlist放在Panel里面了,所以没反映
只要把DropDownlist拉出来,放在外面就行了,晕了
不知道这是为什么啊?