将多个控件绑定到 ASP.Net WebForm 中的同一数据源
Binding multiple controls to the same datasource in a ASP.Net WebForm
如何将多个控件绑定到同一个数据源?
我通过存储过程调用从 table 获取数据,并希望所有控件都绑定到该数据。
下拉列表绑定成功,其他的绑定失败。
为了让它工作,我必须为每个控件创建一个单独的函数并调用存储过程和绑定。
private void BindServerControls()
{
string strErrorMessage = "";
SqlDataReader LanguagesDataReader = null;
try
{
dbFunc.OpenDB();
SqlCommand LanguagesCmd = new SqlCommand("dbo.SelectAllLanguages", dbFunc.objConn);
LanguagesCmd.Parameters.Clear();
LanguagesCmd.CommandType = CommandType.StoredProcedure;
LanguagesDataReader = LanguagesCmd.ExecuteReader();
if (LanguagesDataReader.HasRows == true)
{
// Bind a dropdownlist.
DropDownList1.DataSource = LanguagesDataReader;
DropDownList1.DataTextField = "ProgrammingLanguage";
DropDownList1.DataValueField = "ID";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, "Choose...");
// Bind a list of Radio buttons.
RadioButtonList1.DataSource = LanguagesDataReader;
RadioButtonList1.DataTextField = "ProgrammingLanguage";
RadioButtonList1.DataValueField = "ID";
RadioButtonList1.DataBind();
// Bind a ListBox.
ListBox1.DataSource = LanguagesDataReader;
ListBox1.DataTextField = "ProgrammingLanguage";
ListBox1.DataValueField = "ID";
ListBox1.DataBind();
// Bind a CheckBoxList.
CheckBoxList1.DataSource = LanguagesDataReader;
CheckBoxList1.DataTextField = "ProgrammingLanguage";
CheckBoxList1.DataValueField = "ID";
CheckBoxList1.DataBind();
// Bind a BulletList.
BulletedList1.DataSource = LanguagesDataReader;
BulletedList1.DataTextField = "ProgrammingLanguage";
BulletedList1.DataValueField = "ID";
BulletedList1.DataBind();
}
else
{
Status.Text = "There are currently no entries in the
database.";
}
}
catch (Exception ex)
{
Status.Text = "Unable to get the entries from the database.";
}
finally
{
if (LanguagesDataReader != null)
{
LanguagesDataReader.Close();
}
dbFunc.CloseDB();
}
}
不是绑定 dataReader 对象,而是在 DataTable 中加载数据,然后使用它进行绑定
DataTable dtLanguages = new DataTable();
using(SqlDataReader LanguagesDataReader = LanguagesCmd.ExecuteReader()) {
dtLanguages.Load(LanguagesDataReader);
}
if(dtLanguages.Rows.Count > 0) {
// Bind a dropdownlist.
DropDownList1.DataSource = dtLanguages;
DropDownList1.DataTextField = "ProgrammingLanguage";
DropDownList1.DataValueField = "ID";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, "Choose...");
}
原因是 DataReader 是顺序的、只向前连接的数据源,一旦你通过它完成数据遍历,你就不能返回开始并再次读取。
如何将多个控件绑定到同一个数据源?
我通过存储过程调用从 table 获取数据,并希望所有控件都绑定到该数据。
下拉列表绑定成功,其他的绑定失败。 为了让它工作,我必须为每个控件创建一个单独的函数并调用存储过程和绑定。
private void BindServerControls()
{
string strErrorMessage = "";
SqlDataReader LanguagesDataReader = null;
try
{
dbFunc.OpenDB();
SqlCommand LanguagesCmd = new SqlCommand("dbo.SelectAllLanguages", dbFunc.objConn);
LanguagesCmd.Parameters.Clear();
LanguagesCmd.CommandType = CommandType.StoredProcedure;
LanguagesDataReader = LanguagesCmd.ExecuteReader();
if (LanguagesDataReader.HasRows == true)
{
// Bind a dropdownlist.
DropDownList1.DataSource = LanguagesDataReader;
DropDownList1.DataTextField = "ProgrammingLanguage";
DropDownList1.DataValueField = "ID";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, "Choose...");
// Bind a list of Radio buttons.
RadioButtonList1.DataSource = LanguagesDataReader;
RadioButtonList1.DataTextField = "ProgrammingLanguage";
RadioButtonList1.DataValueField = "ID";
RadioButtonList1.DataBind();
// Bind a ListBox.
ListBox1.DataSource = LanguagesDataReader;
ListBox1.DataTextField = "ProgrammingLanguage";
ListBox1.DataValueField = "ID";
ListBox1.DataBind();
// Bind a CheckBoxList.
CheckBoxList1.DataSource = LanguagesDataReader;
CheckBoxList1.DataTextField = "ProgrammingLanguage";
CheckBoxList1.DataValueField = "ID";
CheckBoxList1.DataBind();
// Bind a BulletList.
BulletedList1.DataSource = LanguagesDataReader;
BulletedList1.DataTextField = "ProgrammingLanguage";
BulletedList1.DataValueField = "ID";
BulletedList1.DataBind();
}
else
{
Status.Text = "There are currently no entries in the
database.";
}
}
catch (Exception ex)
{
Status.Text = "Unable to get the entries from the database.";
}
finally
{
if (LanguagesDataReader != null)
{
LanguagesDataReader.Close();
}
dbFunc.CloseDB();
}
}
不是绑定 dataReader 对象,而是在 DataTable 中加载数据,然后使用它进行绑定
DataTable dtLanguages = new DataTable();
using(SqlDataReader LanguagesDataReader = LanguagesCmd.ExecuteReader()) {
dtLanguages.Load(LanguagesDataReader);
}
if(dtLanguages.Rows.Count > 0) {
// Bind a dropdownlist.
DropDownList1.DataSource = dtLanguages;
DropDownList1.DataTextField = "ProgrammingLanguage";
DropDownList1.DataValueField = "ID";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, "Choose...");
}
原因是 DataReader 是顺序的、只向前连接的数据源,一旦你通过它完成数据遍历,你就不能返回开始并再次读取。