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中选定的值,怎么回事?
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中选定的值,怎么回事?
private void DroplistDataBind()
{
//省略....
}
拿出来看看
{
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";
}
{
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();
}
}麻烦各位看看,先谢了!
要么给DataValueField赋值
要么取selecteditem 的text值
Dlist.DataSource = cReader;
Dlist.DataTextField ="xm";
Dlist.DataValueField ="xm";
Dlist.DataBind();还是不行!我又改btnOK_Click
lxm=Dlist.SelectedItem.Text;
也不行!还要麻烦指教一下,谢谢!
你调试一下看看
到你要取lxm的值的时候,看看那个Dlist,一共有几个item,当前item的text和value和index是什么
应该可以找到原因
{
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就行了
{
.....
}
又让Dlist的SelectedValue="hhhhh";,这时候Dlist的item为空
哪位大哥帮帮忙给个类似详细的代码例子,偶自己看看错在哪!
谢谢!
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类似的代码。
//在这里取数据
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();
偶这样进行测试:
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有什么奥妙在这里?
CSDN发完贴不能改顶讨厌的!...