Leon
Leon

Reputation: 581

Gridview is giving the "RowEditing not handled" error only when on a new page

I am currently using Visual Studio to build my project and I'm having it automatically generate the Edit button. My Gridview allows for searching via a search box and a page only handles up to 10 rows. On the very first load of the Gridview, the Edit button and the accompanying Update and Cancel work perfectly fine. However, once I click on a new page, it fires these errors when I try to click Edit (the second error was when I somehow got the Update and Cancel button to show):

The GridView 'ProjectTable' fired event RowEditing which wasn't handled.
The GridView 'ProjectTable' fired event RowCancelingEdit which wasn't handled.

What is going on? Here is my aspx page:

<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>

And here is my code behind:

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();

    }
}

Upvotes: 0

Views: 687

Answers (1)

John Paul
John Paul

Reputation: 837

You need to implement those events. In your markup you set auto generate edit button to true but you never implement the On Row Editing event and On Row Cancelling Edit event. Updating your markup to:

<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">

and then add that method to your code behind:

protected void ProjectTable_OnRowEditing(object sender, GridViewEditEventArgs e)
{

}

protected void ProjectTable_OnRowCancellingEdit(object sender, GridViewCancelEditEventArgs e)
{

}

Upvotes: 0

Related Questions