我使用的是位于以下位置的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();
}}
发布于 2012-09-18 01:54:43
我忘了创建一个EditRecord方法。它应该看起来像这样:
protected void EditRecord(object sender, GridViewEditEventArgs e)
{
EditableGrid.EditIndex = e.NewEditIndex;
buildPage();
}我还阅读了这篇文章:http://www.dotnetfunda.com/articles/article29.aspx,它解释了如何正确使用GridView控件。
这也很有用:对于RowUpdating方法:http://dotnetdiscussion.wordpress.com/2008/04/06/aspnet-how-to-use-bulkeditgridview-to-save-hours-in-database-editing/
https://stackoverflow.com/questions/12463749
复制相似问题