第一:需要先绑定再遍历。
第二:遍历处理过要更新。
参考我批量更新时的代码:
//遍历更新学生信息表
protected void BtnUpdate_Click(object sender, EventArgs e)
{
int UpdateCount = 0;//更新成功数
for (int i = 0; i <= RptStudents.Items.Count - 1; i++)
{
HyperLink LnkName = (HyperLink)RptStudents.Items[i].FindControl("LnkName");
TextBox TxtAge = (TextBox)RptStudents.Items[i].FindControl("TxtAge");
if (LnkName == null)
continue;
studentsInfo.EName = LnkName.Text.ToString();
studentsInfo.Age = Convert.ToInt32(TxtAge.Text.ToString());
studentsInfo.Adddate = DateTime.Now.ToString();
RepeaterItem item = LnkName.Parent as RepeaterItem;
Label LabFId = item.FindControl("LabId") as Label;
studentsInfo.Id = LabFId.Text.ToString();
if (LabFId.Text.ToString() != "")
{
if (students.Update(studentsInfo) > 0)
UpdateCount += 1;
}
}
ScriptManager.RegisterStartupScript(UpDPanelStudents, typeof(UpdatePanel), "JsAlert", "alert('更新" + UpdateCount.ToString() + "条记录成功!')", true);
BindRptStudents("1");//更新完要绑定
}
1.处理前repeater没有DataBind()
2.如果是回发,不能禁用viewState