将ListBox.SelectedItem.Value和Text赋值给某个变量总是提示
Object reference not set to an instance of an object.
错误!很郁闷, 加 ToString()也不行.
ListBox.SelectedValue也一样问题.
全部代码如下.
谢谢解答!//------------------------------beginning of code-----------------------------------------using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{
DataTable dt = new DataTable();
DataRow myDataRow;
protected void Page_Load(object sender, EventArgs e)
{
BindDataToListBox();
} private void BindDataToListBox()
{
string conStr = ConfigurationManager.ConnectionStrings["ASPDOTNET"].ConnectionString;
string cmdText = "SELECT * FROM Users";
SqlConnection myConnection = new SqlConnection(conStr);
SqlDataAdapter da = new SqlDataAdapter(cmdText, myConnection);
da.Fill(dt);
ListBoxUser.DataSource = dt;
ListBoxUser.DataTextField = dt.Columns["Uname"].ToString();
ListBoxUser.DataValueField = dt.Columns["UID"].ToString();
ListBoxUser.DataBind();
}
protected void btnAlter_Click1(object sender, EventArgs e)
{
this.Title = ListBoxUser.SelectedValue;
}
protected void ListBoxUser_SelectedIndexChanged(object sender, EventArgs e)
{
textUage.Text = ListBoxUser.SelectedItem.Text.ToString();
textUname.Text = "TEST";
}
}
//------------------------------end of code-----------------------------------------
Object reference not set to an instance of an object.
错误!很郁闷, 加 ToString()也不行.
ListBox.SelectedValue也一样问题.
全部代码如下.
谢谢解答!//------------------------------beginning of code-----------------------------------------using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{
DataTable dt = new DataTable();
DataRow myDataRow;
protected void Page_Load(object sender, EventArgs e)
{
BindDataToListBox();
} private void BindDataToListBox()
{
string conStr = ConfigurationManager.ConnectionStrings["ASPDOTNET"].ConnectionString;
string cmdText = "SELECT * FROM Users";
SqlConnection myConnection = new SqlConnection(conStr);
SqlDataAdapter da = new SqlDataAdapter(cmdText, myConnection);
da.Fill(dt);
ListBoxUser.DataSource = dt;
ListBoxUser.DataTextField = dt.Columns["Uname"].ToString();
ListBoxUser.DataValueField = dt.Columns["UID"].ToString();
ListBoxUser.DataBind();
}
protected void btnAlter_Click1(object sender, EventArgs e)
{
this.Title = ListBoxUser.SelectedValue;
}
protected void ListBoxUser_SelectedIndexChanged(object sender, EventArgs e)
{
textUage.Text = ListBoxUser.SelectedItem.Text.ToString();
textUname.Text = "TEST";
}
}
//------------------------------end of code-----------------------------------------
ListBoxUser.DataValueField = dt.Columns["UID"].ToString();
--------------
ListBoxUser.DataTextField = "Uname";
ListBoxUser.DataValueField = "UID";
改成
this.Title.Text = ListBoxUser.SelectedValue;
//listBox1.SelectedItem就没有Text属性
里面有很详细使用下拉列表等控件的例子
究竟问题何在啊?!
在上面的代码后面加上
if(ListBoxUser.Items.Count > 0)
{
ListBoxUser.SelectedIndex = 0;
}
======================
接下来,在
textUage.Text = ListBoxUser.SelectedItem.Text.ToString();
textUname.Text = "TEST";
的==前面==加上
if(ListBoxUser.SelectedIndex == -1)
{
return;
}
ListBoxUser.DataTextField = dt.Columns["Uname"].ToString();
ListBoxUser.DataValueField = dt.Columns["UID"].ToString();
ListBoxUser.DataBind();然后改成
ListBoxUser.DataTextField = "Uname";
ListBoxUser.DataValueField = "UID";
ListBoxUser.DataSource = dt;
ListBoxUser.DataBind();
试试
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class Default4 : System.Web.UI.Page
{
private const string NAME = "Uname";
private const string UID = "UID";
protected void Page_Load(object sender, EventArgs e)
{
//如果是postback
if(IsPostBack)
{
//返回
return;
}
//下面的代码仅仅在不是postback的时候执行
DataTable dt = new DataTable();
//下面的模拟取数据
dt.Columns.Add(NAME, typeof (string));
dt.Columns.Add(UID, typeof (int));
DataRow dr;
for (int i = 0; i < 10; ++i)
{
dr = dt.NewRow();
dr[NAME] = String.Format("Str{0}", i);
dr[UID] = i;
dt.Rows.Add(dr);
}
//上面的模拟取数据
ListBox1.DataSource = dt;
//不建议这样写。
//ListBox1.DataTextField = dt.Columns[NAME].ColumnName;
//ListBox1.DataValueField = dt.Columns[UID].ColumnName;
//最好还是使用静态变量
ListBox1.DataTextField = NAME;
ListBox1.DataValueField = UID;
ListBox1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
if(ListBox1.SelectedIndex == -1)
{
return;
}
Title = ListBox1.SelectedValue;
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(ListBox1.SelectedIndex == -1)
{
return;
}
TextBoxName.Text = ListBox1.SelectedItem.Text;
TextBoxValue.Text = ListBox1.SelectedItem.Value;
}
}