protected System.Web.UI.WebControls.TextBox txtId;
protected System.Web.UI.WebControls.TextBox txtName;
protected System.Web.UI.WebControls.DropDownList Dlist;
protected System.Web.UI.WebControls.Button btnOK;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
DroplistDataBind();
}
}
private void DroplistDataBind()
{
//省略....
}
private void btnOK_Click(object sender, System.EventArgs e)
{
string Idno = "";
string Iname = "";
string Ixm = "";

Iname = txtName.Text.Trim();

Idno = txtId.Text.Trim();

lxm=Dlist.SelectedValue.Trim(); Response.Redirect("abcde.aspx?Cxm="+Ixm+"&Cno="+Idno+"&Cname="+Iname);
} private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}运行时Dlist绑定正常, 当上面这个button点击后Idno和Iname读取都正常.但lxm老是空的,取不到Dlist中选定的值,怎么回事?

解决方案 »

  1.   

    你把这个
    private void DroplistDataBind()
    {
    //省略....
    }
    拿出来看看
      

  2.   

    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!IsPostBack)
    {
    DroplistDataBind();
    }
    }
    private void DroplistDataBind()
    {
    SqlCommand CMD = new SqlCommand();
    CMD.CommandText = "select distinct xm from xusers order by xm";
    abc.DataControl.BindDropWithSQLCommand(CMD,ref Dlist);
    Dlist.SelectedValue="hhhhh";
    }
      

  3.   

    abc.DataControl.BindDropWithSQLCommand的代码:public static void BindDropWithSQLCommand(SqlCommand CMD,ref DropDownList Dlist)
    {
    SqlConnection Con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings.Get("Con11"));
    CMD.Connection = Con;
    try
    {
    if (Con.State.ToString() == "Closed") 
    {
    Con.Open();
    }
    SqlDataReader cReader = CMD.ExecuteReader();
    Dlist.DataSource = cReader;
    Dlist.DataTextField ="xm";
    Dlist.DataBind();
    }
    catch(SqlException sqlex)
    {
    HttpContext.Current.Response.Write(sqlex.Message);
    }
    finally
    {
    Con.Close();
    Con.Dispose();
    CMD.Dispose();
    }
    }麻烦各位看看,先谢了!
      

  4.   

    你只设了Dlist.DataTextField ,没有设置DataValueField而你却用lxm=Dlist.SelectedValue.Trim();去取值,当然什么也取不到了解决:
    要么给DataValueField赋值
    要么取selecteditem 的text值
      

  5.   

    dropdownlist的DataValueField没有绑定!当然取不到SelectedValue值啦。你只是绑定了Dlist.DataTextField ="xm";
      

  6.   

    我在BindDropWithSQLCommand改
    Dlist.DataSource = cReader;
    Dlist.DataTextField ="xm";
    Dlist.DataValueField ="xm";
    Dlist.DataBind();还是不行!我又改btnOK_Click
    lxm=Dlist.SelectedItem.Text;
    也不行!还要麻烦指教一下,谢谢!
      

  7.   

    奇怪了
    你调试一下看看
    到你要取lxm的值的时候,看看那个Dlist,一共有几个item,当前item的text和value和index是什么
    应该可以找到原因
      

  8.   

    private void DroplistDataBind()
    {
    SqlCommand CMD = new SqlCommand();
    CMD.CommandText = "select distinct xm from xusers order by xm";
    abc.DataControl.BindDropWithSQLCommand(CMD,ref Dlist);
    Dlist.SelectedValue="hhhhh";
    }
    不对,要把值和ID都绑上,
    在取值时要
    Request.Form就行了
      

  9.   

    to abandonship(eagles of wind)不是很明白,能不能具体点.
      

  10.   

    可能是因为你的Dlist的绑定的cReader 没有hhhhh这个植,在你的private void DroplistDataBind()
    {
    .....
    }
    又让Dlist的SelectedValue="hhhhh";,这时候Dlist的item为空
      

  11.   

    不对的,"hhhhh"有的.
    哪位大哥帮帮忙给个类似详细的代码例子,偶自己看看错在哪!
    谢谢!
      

  12.   

    偶改代码如下:
    protected System.Web.UI.WebControls.TextBox txtId;
    protected System.Web.UI.WebControls.TextBox txtName;
    protected System.Web.UI.WebControls.DropDownList Dlist;
    protected System.Web.UI.WebControls.Button btnOK;
    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!IsPostBack)
    {
    DroplistDataBind();
    }
    }
    private void DroplistDataBind()
    {
    SqlCommand CMD = new SqlCommand();
    CMD.CommandText = "select distinct xm from xusers order by xm";
    abc.DataControl.BindDropWithSQLCommand(CMD,ref Dlist);
    Dlist.SelectedValue="hhhhh";
    }
    private void btnOK_Click(object sender, System.EventArgs e)
    {
    string Idno = "";
    string Iname = "";
    string Ixm = "";Iname = txtName.Text.Trim();Idno = txtId.Text.Trim();lxm=Dlist.SelectedItem.Text.Trim();Response.Redirect("abcde.aspx?Cxm="+Ixm+"&Cno="+Idno+"&Cname="+Iname);
    }private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    }错误变成:
    未将对象引用设置到对象的实例。
    指这一行:string lxnxq=DList.SelectedItem.Text.Trim();
    该如何处理?
    偶下了十几个.NET源码,包括MS的DUWAMISH、PETSHOP,奇怪的是都没有关于DROPDOWNLIST类似的代码。
      

  13.   

    你试试这样:
    //在这里取数据
    DataTable datatable1 = ....;
    if( datatable1.Rows.Count == 0 )
    return;DropDownList1.Items.Clear();
    DropDownList1.DataSource = datatable1;
    DropDownList1.DataTextField = "city_name";
    DropDownList1.DataValueField = "igen_city_code";
    DropDownList1.DataBind();
      

  14.   

    还是不行,不过问题大概找到了,只是不知道如何解决:
    偶这样进行测试:
    protected System.Web.UI.WebControls.TextBox txtId;
    protected System.Web.UI.WebControls.TextBox txtName;
    protected System.Web.UI.WebControls.DropDownList DropDown1;
    protected System.Web.UI.WebControls.Button btnOK;
    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!IsPostBack)
    {
    DropDown1.Items.Add( new ListItem("厦门"));
    DropDown1.Items.Add( new ListItem("深圳"));
    DropDown1.Items.Add( new ListItem("珠海"));
    DropDown1.Items.Add( new ListItem("大连"));
    }
    }
    private void btnOK_Click(object sender, System.EventArgs e)
    {
    string Iname = txtName.Text.Trim();
    string Idno = txtId.Text.Trim();
    string lxm=Dlist.SelectedItem.Text.Trim();
    Response.Redirect("abcde.aspx?Cxm="+Ixm+"&Cno="+Idno+"&Cname="+Iname);
    }
    private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    }
    发现点击button后dropdownlist列表的东西都清空了,根本没有进行Redirect跳转.
    我把一模一样的程序放在同一计算机的另一sln里(比如Duwnmish)运行就正常了.
    不知道.NET framework有什么奥妙在这里?
      

  15.   

    说明一下:上贴dropdownlist名称是偶发贴是临时改的,所以有出入.
    CSDN发完贴不能改顶讨厌的!...
      

  16.   

    问题已经找到是viewstates问题.结贴!