C# 将DropDownList的返回对象绑定到asp:DropDownList Web表单
为了帮助重用一些代码,我想生成DropDownList对象,并将它们分配给ASP.NET C#中的web表单字段 我似乎无法将我的对象(DropDownList)绑定到我的Web表单。这样做合适吗?有更好的办法吗?我知道我会在其他web表单上使用这个下拉列表,很多人都喜欢它。我想把所有这些放在一个我可以调用的类中 下面是我返回下拉对象的方法 在我的网络表单上,它是一个简单的C# 将DropDownList的返回对象绑定到asp:DropDownList Web表单,c#,asp.net,object,webforms,C#,Asp.net,Object,Webforms,为了帮助重用一些代码,我想生成DropDownList对象,并将它们分配给ASP.NET C#中的web表单字段 我似乎无法将我的对象(DropDownList)绑定到我的Web表单。这样做合适吗?有更好的办法吗?我知道我会在其他web表单上使用这个下拉列表,很多人都喜欢它。我想把所有这些放在一个我可以调用的类中 下面是我返回下拉对象的方法 在我的网络表单上,它是一个简单的 <asp:DropDownList ID="dd_name" runat="server"></asp:
<asp:DropDownList ID="dd_name" runat="server"></asp:DropDownList>
我还试过Bind和其他东西
这可能吗?有更好的方法吗?我已经调整了您的代码,使您能够将数据绑定到dropdownlist
protected ICollection ddNames() {
string selectSQL = "mysql stuff";
string connString = "my string";
SqlCommand cmd = new SqlCommand(selectSQL, conn);
SqlDataReader reader;
// Create a table to store data for the DropDownList control.
DataTable dt = new DataTable();
// Define the columns of the table.
dt.Columns.Add(new DataColumn("NewItemTextField", typeof(String)));
dt.Columns.Add(new DataColumn("NewItemValueField", typeof(String)));
dt.Rows.Add(CreateRow("Unassigned", "999999", dt));
try {
conn.Open();
reader = cmd.ExecuteReader();
while ( reader.Read() ) {
// Populate the table with sample values.
dt.Rows.Add(CreateRow(DataHelpers.GetUserFirstLastFromID(Convert.ToInt32(reader["someid"])), reader["someid"].ToString(), dt));
}
reader.Close();
} catch (Exception ex) {
throw ex;
} finally {
if (conn != null) {
conn.Dispose();
conn.Close();
}
}
return dt;
}
DataRow CreateRow(String Text, String Value, DataTable dt)
{
// Create a DataRow using the DataTable defined in the
// CreateDataSource method.
DataRow dr = dt.NewRow();
// This DataRow contains the NewItemTextField and NewItemValueField
// fields, as defined in the CreateDataSource method. Set the
// fields with the appropriate value. Remember that column 0
// is defined as NewItemTextField, and column 1 is defined as
// NewItemValueField.
dr[0] = Text;
dr[1] = Value;
return dr;
}
最后,您对方法的调用和对dropdownlist的绑定将是:
// Specify the data source and field names for the Text
// and Value properties of the items (ListItem objects)
// in the DropDownList control.
dd_name.DataSource = ddNames();
dd_name.DataTextField = "NewItemTextField";
dd_name.DataValueField = "NewItemValueField";
// Bind the data to the control.
dd_name.DataBind();
// Set the default selected item, if desired.
dd_name.SelectedIndex = 0;
灵感来自哦,回答得不错!这很有效。虽然我没有返回Icollection,而是返回了DataTable@Firemarble我很高兴这是你想要的:-)
protected ICollection ddNames() {
string selectSQL = "mysql stuff";
string connString = "my string";
SqlCommand cmd = new SqlCommand(selectSQL, conn);
SqlDataReader reader;
// Create a table to store data for the DropDownList control.
DataTable dt = new DataTable();
// Define the columns of the table.
dt.Columns.Add(new DataColumn("NewItemTextField", typeof(String)));
dt.Columns.Add(new DataColumn("NewItemValueField", typeof(String)));
dt.Rows.Add(CreateRow("Unassigned", "999999", dt));
try {
conn.Open();
reader = cmd.ExecuteReader();
while ( reader.Read() ) {
// Populate the table with sample values.
dt.Rows.Add(CreateRow(DataHelpers.GetUserFirstLastFromID(Convert.ToInt32(reader["someid"])), reader["someid"].ToString(), dt));
}
reader.Close();
} catch (Exception ex) {
throw ex;
} finally {
if (conn != null) {
conn.Dispose();
conn.Close();
}
}
return dt;
}
DataRow CreateRow(String Text, String Value, DataTable dt)
{
// Create a DataRow using the DataTable defined in the
// CreateDataSource method.
DataRow dr = dt.NewRow();
// This DataRow contains the NewItemTextField and NewItemValueField
// fields, as defined in the CreateDataSource method. Set the
// fields with the appropriate value. Remember that column 0
// is defined as NewItemTextField, and column 1 is defined as
// NewItemValueField.
dr[0] = Text;
dr[1] = Value;
return dr;
}
// Specify the data source and field names for the Text
// and Value properties of the items (ListItem objects)
// in the DropDownList control.
dd_name.DataSource = ddNames();
dd_name.DataTextField = "NewItemTextField";
dd_name.DataValueField = "NewItemValueField";
// Bind the data to the control.
dd_name.DataBind();
// Set the default selected item, if desired.
dd_name.SelectedIndex = 0;