我正在尝试遍历数据库,并根据查询为每一行创建一个按钮。我应该得到3个按钮作为回报,但是只创建了一个。
DataTable dt = new DataTable();
con.Open();
string TeamID = (string)Session["TeamID"];
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand("SELECT * FROM Players1 WHERE
TeamID = " + teamID, con);
myCommand.Parameters.AddWithValue("@TeamID", TeamID);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
foreach (Object ob in myReader)
{
Button1.Text = "" + (myReader["Name"].ToString());
}
}
我应该根据数据库中的数据获得3个按钮
答案 0 :(得分:2)
您需要自己创建Button
。
对于从myReader
对象外观here中获取数据不是很确定
DataTable dt = new DataTable();
con.Open();
string TeamID = (string)Session["TeamID"];
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand("SELECT * FROM Players1 WHERE
TeamID = " + teamID, con);
myCommand.Parameters.AddWithValue("@TeamID", TeamID);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
foreach (Object ob in myReader)
{
Button btn = new Button();
btn.ID = "Btn_" + ob["Id"]; //I suppose the table has an Id
btn.Text = ob["Name"]; //I also suppose the table has a Name column
//for click events
//btn.Click = btn_Click_Event;
}
}
答案 1 :(得分:-1)
我不知道您应该在哪里获得三个按钮。 从外观上看,您只是在更改Button1的文本,它是一个按钮。 您要更改Button1的文本三次。 我想您还希望Button2和Button3也能更改。