0
我必须使用来自数据库的数据填充复选框,但是在我的页面上没有显示复选框。请让我知道正确的方法来做到这一点。在C#中,我写的Page_Load方法是这样的:通过数据库填充复选框
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>
请告诉我做到这一点的正确方法。
请验证您的SQL语句是否执行,并且该页面上没有其他错误。你有没有试过运行调试器?因为你的代码看起来很好。 – 2010-04-15 15:34:06
在您的示例中,您应该在打开数据库连接和查询数据之前检查回传。 – 2010-04-15 15:35:00
在一个不相关的说明中,不是在代码中编译连接字符串,而是将其放在'web.config'中。 – RedFilter 2010-04-15 15:37:09