我正在使用此处找到的 BulkEditGridView 控件(继承 GridView):http: //blogs.msdn.com/b/mattdotson/archive/2005/11/09/real-world-gridview-bulk-editing.aspx
现在我有一个保存按钮,当它被击中时应该只列出所有修改过的项目,但我得到的是“没有更新”。即使我更改了 DataGrid 中的文本。为什么 DirtyRows 没有正确填充?我在更新数据库之前对此进行了测试。
我的 aspx 页面如下所示:
<rwg:BulkEditGridView ID="EditableGrid" AutoGenerateColumns="False" OnRowUpdating="EditableGrid_RowUpdating" OnRowEditing="EditRecord" SaveButtonID="SaveButton" runat="server">
<Columns>
<asp:BoundField HeaderText="Question" DataField="Question" />
<asp:BoundField HeaderText="A" DataField="a" />
<asp:BoundField HeaderText="B" DataField="b" />
<asp:BoundField HeaderText="C" DataField="c" />
<asp:BoundField HeaderText="D" DataField="d" />
<asp:BoundField HeaderText="E" DataField="e" />
<asp:BoundField HeaderText="F" DataField="f" />
<asp:BoundField HeaderText="Correct Answer" DataField="CorrectAns" />
<asp:CheckBoxField HeaderText="Shuffle Answers" DataField="ShuffleAnswers" />
<asp:BoundField HeaderText="Course Objective" DataField="CourseObjective" />
</Columns>
</rwg:BulkEditGridView>
我背后的代码如下所示:
public partial class editExam : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string courseCode = "";
courseCode = Request.QueryString["code"];
if (courseCode.Length > 0)
{
//Show all of the course questions.
Connection conn = new Connection();
SqlParameter p = new SqlParameter("@CourseCode", SqlDbType.VarChar, 10);
p.Value = Connection.validateString(courseCode);
conn.close();
//Gets the exam to display from the database
DataTable dt = conn.query("getExam", p);
if (dt.Rows.Count == 0)
{
//Display an error message
displayError();
}
else
{
//Show all courses
buildPage(dt);
}
}
else
{
//Display an error message
displayError();
}
}
private void buildPage(DataTable dt)
{
EditableGrid.DataSource = dt;
EditableGrid.DataBind();
}
protected void SaveButton_Click(object sender, EventArgs e)
{
if (EditableGrid.DirtyRows.Count == 0)
{
pageContent.InnerHtml = "No updates have been made.";
}
}
protected void EditableGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
StringBuilder rowIndexes = new StringBuilder();
foreach (GridViewRow row in EditableGrid.DirtyRows)
{
rowIndexes.Append((row.RowIndex + 1) + ", ");
}
pageContent.InnerHtml = "updates made to: " + rowIndexes.ToString();
}
}