Gridview 仅在新页面上出现 "RowEditing not handled" 错误
Gridview is giving the "RowEditing not handled" error only when on a new page
我目前正在使用 Visual Studio 构建我的项目,我让它自动生成“编辑”按钮。我的 Gridview 允许通过搜索框进行搜索,一个页面最多只能处理 10 行。在第一次加载 Gridview 时,Edit 按钮和随附的 Update 和 Cancel 完美运行美好的。但是,一旦我点击一个新页面,当我尝试点击编辑时它会触发这些错误(第二个错误是当我以某种方式显示更新和取消按钮时):
The GridView 'ProjectTable' fired event RowEditing which wasn't handled.
The GridView 'ProjectTable' fired event RowCancelingEdit which wasn't handled.
这是怎么回事?这是我的 aspx 页面:
<head runat="server">
<title></title>
<style>
.hiddencol
{
display: none;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="SearchBox" runat="server"></asp:TextBox>
<asp:DropDownList ID="SearchParameterList" runat="server">
<asp:ListItem Selected="True" Value="Project_Name">Project Name</asp:ListItem>
<asp:ListItem Value="Product">Product</asp:ListItem>
<asp:ListItem Value="Description">Description</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="SearchButton" runat="server" Text="Search" OnClick="SearchButton_Click" />
<asp:Button ID="ClearButton" runat="server" OnClick="ClearButton_Click" Text="Clear" />
<br />
<br />
<asp:GridView ID="ProjectTable" runat="server" Font-Names="Verdana,Arial" Font-Size="12px" AutoGenerateColumns="False" AutoGenerateEditButton="True" DataSourceID="PopulateProjectTable" AllowPaging="True" OnPageIndexChanging="ProjectTable_PageIndexChanging>
<AlternatingRowStyle BackColor="#BFE4FF" />
<PagerStyle BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" />
<HeaderStyle Height="30px" BackColor="#6DC2FF" Font-Size="12px" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" />
<RowStyle Height="20px" Font-Size="12px" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" />
<Columns>
<asp:BoundField DataField="ProjID" HeaderText="ProjID" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" SortExpression="ProjID" />
<asp:BoundField DataField="Project_Name" HeaderText="Project Name Value" ReadOnly="True" SortExpression="Project_Name" />
<asp:BoundField DataField="Product" HeaderText="Product" SortExpression="Product" ReadOnly="True" />
<asp:BoundField DataField="Product_Edit" HeaderText="Product (alternate)" SortExpression="Product_Edit" />
<asp:BoundField DataField="Description" HeaderText="Description" ReadOnly="True" SortExpression="Description" />
<asp:BoundField DataField="Description_Edit" HeaderText="Description (alternate)" SortExpression="Description_Edit" />
<asp:BoundField DataField="Comment" HeaderText="Comment Submission/Approval" SortExpression="Comment" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="PopulateProjectTable" runat="server"
ConnectionString="<%$ ConnectionStrings:ODSConnectionString %>"
SelectCommand="SELECT [Project_Name], [Product], [Product_Edit], [Description], [Description_Edit], [Comment], [ProjID] FROM [Pipeline_Detail]"
UpdateCommand="UPDATE [Pipeline_Detail] SET Product_Edit = @Product_Edit, Description_Edit= @Description_Edit, Comment = @Comment WHERE ProjID = @ProjID">
<UpdateParameters>
<asp:Parameter Name="ProjID"/>
<asp:Parameter Name="Product_Edit" Type="String"/>
<asp:Parameter Name="Description_Edit" Type="String"/>
<asp:Parameter Name="Comment" Type="String"/>
</UpdateParameters>
</asp:SqlDataSource>
</div>
</form>
下面是我的代码:
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SearchButton_Click(object sender, EventArgs e)
{
ProjectTable.PageIndex = 0;
ProjectTable.DataSourceID = null;
PopulateProjectTable.SelectCommand = "SELECT [Project_Name], [Product], [Product_Edit], [Description], [Description_Edit], [Comment], [ProjID] FROM [Pipeline_Detail] WHERE " + SearchParameterList.SelectedValue.ToString() + " LIKE '%" + SearchBox.Text + "%'";
ProjectTable.DataSource = PopulateProjectTable;
ProjectTable.DataBind();
}
protected void ClearButton_Click(object sender, EventArgs e)
{
ProjectTable.PageIndex = 0;
ProjectTable.DataSourceID = null;
PopulateProjectTable.SelectCommand = "SELECT [Project_Name], [Product], [Product_Edit], [Description], [Description_Edit], [Comment], [ProjID] FROM [Pipeline_Detail] WHERE Project_Name LIKE '%%'";
ProjectTable.DataSource = PopulateProjectTable;
ProjectTable.DataBind();
SearchBox.Text = "";
}
protected void ProjectTable_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
ProjectTable.PageIndex = e.NewPageIndex;
ProjectTable.DataSourceID = null;
PopulateProjectTable.SelectCommand = "SELECT [Project_Name], [Product], [Product_Edit], [Description], [Description_Edit], [Comment], [ProjID] FROM [Pipeline_Detail] WHERE " + SearchParameterList.SelectedValue.ToString() + " LIKE '%" + SearchBox.Text + "%'" ;
ProjectTable.DataSource = PopulateProjectTable;
ProjectTable.DataBind();
}
}
您需要实施这些事件。在您的标记中,您将自动生成编辑按钮设置为 true,但您从未实现 On Row Editing 事件和 On Row Cancelling Edit 事件。将您的标记更新为:
<asp:GridView ID="ProjectTable" runat="server" Font-Names="Verdana,Arial" Font-Size="12px" AutoGenerateColumns="False" AutoGenerateEditButton="True" DataSourceID="PopulateProjectTable" AllowPaging="True" OnPageIndexChanging="ProjectTable_PageIndexChanging" OnRowEditing="ProjectTable_OnRowEditing" OnRowCancelingEdit="ProjectTable_OnRowCancellingEdit">
然后将该方法添加到后面的代码中:
protected void ProjectTable_OnRowEditing(object sender, GridViewEditEventArgs e)
{
}
protected void ProjectTable_OnRowCancellingEdit(object sender, GridViewCancelEditEventArgs e)
{
}
我目前正在使用 Visual Studio 构建我的项目,我让它自动生成“编辑”按钮。我的 Gridview 允许通过搜索框进行搜索,一个页面最多只能处理 10 行。在第一次加载 Gridview 时,Edit 按钮和随附的 Update 和 Cancel 完美运行美好的。但是,一旦我点击一个新页面,当我尝试点击编辑时它会触发这些错误(第二个错误是当我以某种方式显示更新和取消按钮时):
The GridView 'ProjectTable' fired event RowEditing which wasn't handled.
The GridView 'ProjectTable' fired event RowCancelingEdit which wasn't handled.
这是怎么回事?这是我的 aspx 页面:
<head runat="server">
<title></title>
<style>
.hiddencol
{
display: none;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="SearchBox" runat="server"></asp:TextBox>
<asp:DropDownList ID="SearchParameterList" runat="server">
<asp:ListItem Selected="True" Value="Project_Name">Project Name</asp:ListItem>
<asp:ListItem Value="Product">Product</asp:ListItem>
<asp:ListItem Value="Description">Description</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="SearchButton" runat="server" Text="Search" OnClick="SearchButton_Click" />
<asp:Button ID="ClearButton" runat="server" OnClick="ClearButton_Click" Text="Clear" />
<br />
<br />
<asp:GridView ID="ProjectTable" runat="server" Font-Names="Verdana,Arial" Font-Size="12px" AutoGenerateColumns="False" AutoGenerateEditButton="True" DataSourceID="PopulateProjectTable" AllowPaging="True" OnPageIndexChanging="ProjectTable_PageIndexChanging>
<AlternatingRowStyle BackColor="#BFE4FF" />
<PagerStyle BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" />
<HeaderStyle Height="30px" BackColor="#6DC2FF" Font-Size="12px" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" />
<RowStyle Height="20px" Font-Size="12px" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" />
<Columns>
<asp:BoundField DataField="ProjID" HeaderText="ProjID" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" SortExpression="ProjID" />
<asp:BoundField DataField="Project_Name" HeaderText="Project Name Value" ReadOnly="True" SortExpression="Project_Name" />
<asp:BoundField DataField="Product" HeaderText="Product" SortExpression="Product" ReadOnly="True" />
<asp:BoundField DataField="Product_Edit" HeaderText="Product (alternate)" SortExpression="Product_Edit" />
<asp:BoundField DataField="Description" HeaderText="Description" ReadOnly="True" SortExpression="Description" />
<asp:BoundField DataField="Description_Edit" HeaderText="Description (alternate)" SortExpression="Description_Edit" />
<asp:BoundField DataField="Comment" HeaderText="Comment Submission/Approval" SortExpression="Comment" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="PopulateProjectTable" runat="server"
ConnectionString="<%$ ConnectionStrings:ODSConnectionString %>"
SelectCommand="SELECT [Project_Name], [Product], [Product_Edit], [Description], [Description_Edit], [Comment], [ProjID] FROM [Pipeline_Detail]"
UpdateCommand="UPDATE [Pipeline_Detail] SET Product_Edit = @Product_Edit, Description_Edit= @Description_Edit, Comment = @Comment WHERE ProjID = @ProjID">
<UpdateParameters>
<asp:Parameter Name="ProjID"/>
<asp:Parameter Name="Product_Edit" Type="String"/>
<asp:Parameter Name="Description_Edit" Type="String"/>
<asp:Parameter Name="Comment" Type="String"/>
</UpdateParameters>
</asp:SqlDataSource>
</div>
</form>
下面是我的代码:
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SearchButton_Click(object sender, EventArgs e)
{
ProjectTable.PageIndex = 0;
ProjectTable.DataSourceID = null;
PopulateProjectTable.SelectCommand = "SELECT [Project_Name], [Product], [Product_Edit], [Description], [Description_Edit], [Comment], [ProjID] FROM [Pipeline_Detail] WHERE " + SearchParameterList.SelectedValue.ToString() + " LIKE '%" + SearchBox.Text + "%'";
ProjectTable.DataSource = PopulateProjectTable;
ProjectTable.DataBind();
}
protected void ClearButton_Click(object sender, EventArgs e)
{
ProjectTable.PageIndex = 0;
ProjectTable.DataSourceID = null;
PopulateProjectTable.SelectCommand = "SELECT [Project_Name], [Product], [Product_Edit], [Description], [Description_Edit], [Comment], [ProjID] FROM [Pipeline_Detail] WHERE Project_Name LIKE '%%'";
ProjectTable.DataSource = PopulateProjectTable;
ProjectTable.DataBind();
SearchBox.Text = "";
}
protected void ProjectTable_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
ProjectTable.PageIndex = e.NewPageIndex;
ProjectTable.DataSourceID = null;
PopulateProjectTable.SelectCommand = "SELECT [Project_Name], [Product], [Product_Edit], [Description], [Description_Edit], [Comment], [ProjID] FROM [Pipeline_Detail] WHERE " + SearchParameterList.SelectedValue.ToString() + " LIKE '%" + SearchBox.Text + "%'" ;
ProjectTable.DataSource = PopulateProjectTable;
ProjectTable.DataBind();
}
}
您需要实施这些事件。在您的标记中,您将自动生成编辑按钮设置为 true,但您从未实现 On Row Editing 事件和 On Row Cancelling Edit 事件。将您的标记更新为:
<asp:GridView ID="ProjectTable" runat="server" Font-Names="Verdana,Arial" Font-Size="12px" AutoGenerateColumns="False" AutoGenerateEditButton="True" DataSourceID="PopulateProjectTable" AllowPaging="True" OnPageIndexChanging="ProjectTable_PageIndexChanging" OnRowEditing="ProjectTable_OnRowEditing" OnRowCancelingEdit="ProjectTable_OnRowCancellingEdit">
然后将该方法添加到后面的代码中:
protected void ProjectTable_OnRowEditing(object sender, GridViewEditEventArgs e)
{
}
protected void ProjectTable_OnRowCancellingEdit(object sender, GridViewCancelEditEventArgs e)
{
}