C# 通过数据库填充复选框
我必须用来自数据库的数据填充复选框,但我的页面上没有显示任何复选框。请告诉我正确的方法。在C#中,我编写的页面加载方法如下:C# 通过数据库填充复选框,c#,asp.net,data-binding,C#,Asp.net,Data Binding,我必须用来自数据库的数据填充复选框,但我的页面上没有显示任何复选框。请告诉我正确的方法。在C#中,我编写的页面加载方法如下: public partial class dbTest1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string Server = "al2222"; string Username = "hshshshsh";
public partial class dbTest1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string Server = "al2222";
string Username = "hshshshsh";
string Password = "sjjssjs";
string Database = "database1";
string ConnectionString = "Data Source=" + Server + ";";
ConnectionString += "User ID=" + Username + ";";
ConnectionString += "Password=" + Password + ";";
ConnectionString += "Initial Catalog=" + Database;
string query = "Select * from Customer_Order where orderNumber = 17";
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(query, conn))
{
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (!IsPostBack)
{
Interests.DataSource = dr;
Interests.DataTextField = "OptionName";
Interests.DataValueField = "OptionName";
Interests.DataBind();
}
}
conn.Close();
conn.Dispose();
}
}
}
}
在.aspx中,我有一个:
<asp:CheckBoxList ID="Interests" runat="server"></asp:CheckBoxList>
请告诉我完成此操作的正确方法。虽然您的问题已经得到了回答(通过连接字符串注释),但我想我会加入一个可能的方法来重写此内容。我一开始是作为一个评论,但它有点长和笨拙。请注意,这并不能直接回答你的问题,但是要考虑代码的清洁性和在回发时可能的(可能非常温和)性能提升。
protected void Page_Load(object sender, EventArgs e)
{
// If we're in postback, let's not poll the database.
if (Page.IsPostback)
return; // Change this if you do need some postback processing here.
// I assume that in the real world you pull this info from web.config
string Server = "al2222";
string Username = "hshshshsh";
string Password = "sjjssjs";
string Database = "database1";
string ConnectionString = "Data Source=" + Server + ";";
ConnectionString += "User ID=" + Username + ";";
ConnectionString += "Password=" + Password + ";";
ConnectionString += "Initial Catalog=" + Database;
string query = "Select * from Customer_Order where orderNumber = 17";
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(query, conn))
{
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
// Going to assume that you're only getting 1 record
// due to apparent key (orderNumber = 17) in query?
// You can also consider "if (dr.Read())", but fundamentally
// they will do the same thing.
while (dr.Read())
{
Interests.DataSource = dr;
Interests.DataTextField = "OptionName";
Interests.DataValueField = "OptionName";
Interests.DataBind();
}
// I've excised the calls to .Close() and .Dispose(),
// as the using block covers them for you.
}
}
}
我们为什么要走这条路
Page\u Load()
。当然,您可以将其更改为if
/else
,如果还需要在回发上进行一些加载事件处理,则可以将组括起来。这也简化了循环内代码使用
模块为您处理/关闭了连接。因此,您不需要额外的代码李>
值得深思。请确认您的SQL语句已执行,并且该页面上没有其他错误。你试过运行调试器吗?因为您的代码看起来很好。在您的示例中,您应该在打开db连接和查询数据之前检查回发。请注意,不要在代码中编译连接字符串,而是将其放入
web.config
。谢谢!你是对的,我没有给出正确的查询字符串。谢谢