我希望在将查询绑定到转发器后使用数据库中的一些数据。但我不确定我是如何做到这一点的。这是我的代码:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
SqlCommand cmd = new SqlCommand("SELECT * FROM kontakt", conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
Repeater_Beskeder.DataSource = reader;
Repeater_Beskeder.DataBind();
foreach (RepeaterItem row in Repeater_Beskeder.Items)
{
if (reader.Read())
{
Panel Vis_Panel = (Panel)row.FindControl("Panel_Vis_Besked");
if (Request.QueryString["id"].ToString() == reader["id"])
{
Vis_Panel.Visible = true;
}
}
}
conn.Close();
我的读者不会工作,因为它已经被绑定到我的中继器,所以我很丢失。 我希望你们中的一些人能够解决这个问题。
答案 0 :(得分:2)
ItemDataBound事件在Repeater控件中的项目受数据绑定但在页面上呈现之前发生。
void Repeater_Beskeder_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
// This event is raised for the header, the footer, separators, and items.
// Execute the following logic for Items and Alternating Items.
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
Panel Vis_Panel = (Panel)row.FindControl("Panel_Vis_Besked");
if (Request.QueryString["id"].ToString() == reader["id"])
{
Vis_Panel.Visible = true;
}
}
}