ONYX
ONYX

Reputation: 5859

Update Event not getting new values

Im having trouble with my update command. It finds the textbox controls but it doesn't get the new values entered into it and i can't figure out why. i've done some tutorials and im not getting far. In my Update event is a class called Pages which updates the text and that works if i add the text values manually. The problem is accessing the newly updated text from my textboxes.

    <asp:GridView ID="CustomGridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="Name" AutoGenerateSelectButton="true"
    ShowHeaderWhenEmpty="True" ShowFooter="true" AutoGenerateEditButton="true" OnDataBound="CustomGridView_DataBound"
    OnRowEditing="CustomGridView_CancelEditCommand" OnRowCommand="CustomGridView1_RowCommand" 
    OnLoad="CustomGridView1_Load" OnRowUpdated="CustomGridView1_RowUpdated" OnRowUpdating="CustomGridView1_RowUpdating" OnRowCancelingEdit="CustomGridView1_RowCancelingEdit"
     ShowHeader="true">
            <Columns>
        <asp:TemplateField HeaderText="Page Name" HeaderStyle-HorizontalAlign="Left">
            <ItemTemplate>
                <%# Eval("Name") %>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="Name" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Path" HeaderStyle-HorizontalAlign="Left">
            <ItemTemplate>
                <%# Eval("Path") %>
            </ItemTemplate>
            <EditItemTemplate>                    
                <asp:TextBox ID="Path" runat="server" Text='<%# Bind("Path") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Route Value" HeaderStyle-HorizontalAlign="Left">
            <ItemTemplate>
                <%# Eval("RouteValue") %>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="RouteValue" runat="server" Text='<%# Bind("RouteValue") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="RegExp" HeaderStyle-HorizontalAlign="Left">
            <ItemTemplate>
                <%# Eval("RegExp") %>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="RegExp" runat="server" Text='<%# Bind("RegExp") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>        
</asp:GridView>

This part here isn't firing i prefere the method RowUpdated Method in the answer is there a reason why my Updated Event is not firing

protected void CustomGridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
    if (e.Exception == null && e.AffectedRows == 1)
    {
        Pages pages = new Pages();
        SystemPage SySPage = new SystemPage();
        SySPage.Name = e.NewValues[0].ToString();
        SySPage.Path = e.NewValues[1].ToString();
        SySPage.RouteValue = e.NewValues[2].ToString();
        SySPage.RegExp = e.NewValues[3].ToString();
        pages.Update(SySPage, xmlFile);

        CustomGridView1.EditIndex = -1;
        BindData();
    }

}

protected void CustomGridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

    for (int i = 0; i < CustomGridView1.Columns.Count - 1; i++)
    {
        DataControlFieldCell cell = CustomGridView1.Rows[CustomGridView1.EditIndex].Cells[i] as DataControlFieldCell;
        CustomGridView1.Columns[i].ExtractValuesFromCell(e.Keys, cell, DataControlRowState.Edit, false);
    }

    Pages pages = new Pages();
    SystemPage SysPage = new SystemPage();
    SysPage.Name = e.NewValues[0].ToString();
    SysPage.Path = e.NewValues[1].ToString();
    SysPage.RouteValue = e.NewValues[2].ToString();
    SysPage.RegExp = e.NewValues[3].ToString();
    pages.Update(SysPage, xmlFile);

    lblInsert.Text = e.NewValues[3].ToString();

    CustomGridView1.EditIndex = -1;
    BindData();
}

Upvotes: 0

Views: 2811

Answers (1)

Lloyd
Lloyd

Reputation: 2942

To get the values AFTER updating you should handle the GridViews RowUpdated Event http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowupdated.aspx not the RowUpdating.

<asp:GridView OnRowUpdated="GridViewUpdatedEventHandler" />

Edit The updated fields in the GridView are exposed by a property, NewValues, in the GridViewUpdatedEventArgs which is exposed as a parameter of the RowUpdated event.

Code as requested I cannot test this code as I am not near a dev pc. Add the following method to your code file and set the GridViews OnRowUpdated property to point to the method.

protected void CustomGridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        if ((e.Exception == null) && (e.AffectedRows == 1))
        {
            Pages pages = new Pages();
            SystemPage SysPage = new SystemPage();
            SysPage.Name = e.NewValues[0].ToString();
            SysPage.Path = e.NewValues[1].ToString();
            SysPage.RouteValue = e.NewValues[2].ToString(); ;
            SysPage.RegExp = e.NewValues[3].ToString(); ;
            pages.Update(SysPage, xmlFile);

            CustomGridView1.EditIndex = -1;
            BindData();
        }
        else
            // TO DO: ALERT the user the update errored

    } 

Upvotes: 1

Related Questions