Overwrite values when using gridview Edit?

Posted by sah302 on Stack Overflow See other posts from Stack Overflow or by sah302
Published on 2010-06-11T20:49:58Z Indexed on 2010/06/11 20:52 UTC
Read the original article Hit count: 293

Filed under:
|
|
|

I am using a GridView which is bound to a LinqDataSource and using the automatic edit and delete buttons. However, I don't want the user to edit two of the columns manually, but done automatically. Specifically username who last updated the entry, and the date it was updated.

The gridview only contains 3 columns: Name, Date modified, last updated by. Right now when the user clicks the edit button they can only edit the name column (other two set to read-only). Upon clicking the update button, I want the other 2 fields to update as well based on some extra code. I thought this was done in the code behind within the event rowUpdating, but it doesn't seem to work.

My gridview:

<asp:GridView ID="gvNewsSources" runat="server" AutoGenerateColumns="False" 
    DataSourceID="ldsNewsSource" AutoGenerateDeleteButton="True" 
    AutoGenerateEditButton="True" CellPadding="4" ForeColor="#333333" 
    GridLines="None" DataKeyNames="Id">
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <Columns>
    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
        <asp:BoundField DataField="LastUpdatedBy" HeaderText="Last Updated By" 
            SortExpression="LastUpdatedBy" ReadOnly="True" />
        <asp:BoundField DataField="DatedModified" HeaderText="Dated Modified" 
            SortExpression="DatedModified" ReadOnly="True" />


    </Columns>
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="#999999" />
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>

My code behind:

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub gvNewsSources_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvNewsSources.RowUpdating
    e.NewValues("LastUpdatedBy") = GetUser.GetUserName
    e.NewValues("DateModified") = Date.Now()

    lblOutput.Text = e.NewValues("DateModified").ToString()
End Sub

End Class

Yet when I run through this, I get no errors, but the values aren't being updated in the database or in the gridview. I ran through debug mode and the new values dictionary starts at 1 and ends up being 3 by the end of the rowUpdating event and the value is being set (tested by output the newValue of Datemodified), but it isn't saving.

What am I doing wrong?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about vb.net