T L
T L

Reputation: 514

Updated By column in Gridview

I have a grid view which is bound to an ObjectDataSource. After users have finished updating their data, I would like to put their username or userid in the UpdatedBy column to keep track who has updated what record. The UpdatedBy column could be hidden or readonly on the page so users can't just put any text in there.

Here is my UpdatedBy field:

    <asp:BoundField DataField="UpdatedBy" HeaderText="Updated By" SortExpression="UpdatedBy" />
    <asp:TemplateField HeaderText="Updated By">
        <asp:itemTemplate>
            <asp:Label ID="lblUpdatedBy" runat="server" Text='<% #Eval("UpdatedBy") %>' Width="30" ></asp:Label>
        </asp:itemTemplate>
        <asp:editItemTemplate>
            <asp:TextBox ID="txtUpdatedBy" runat="server" CssClass="Gridview_EditItem" Text='<% #Bind("UpdatedBy") %>' Width="30" ></asp:TextBox>
        </asp:editItemTemplate>
    </asp:TemplateField>

I put updating code in RowUpdating event, but for some reason it doesn't work.

protected void gvProductQuery_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
   GridViewRow gr = gvProductQuery.Rows[e.RowIndex];
   TextBox tb = gr.FindControl("txtUpdatedBy") as TextBox;
   tb.Text = cUser.DomainLogin;
}

The tb is Null in the above code. Please help. Or is there an easier way to do this? I'm using VS2012

Thanks

Upvotes: 0

Views: 468

Answers (1)

ebram khalil
ebram khalil

Reputation: 8321

try this out :

protected void gvProductQuery_RowEditing(object sender, GridViewEditEventArgs e)
{
   gvProductQuery.EditIndex = e.NewEditIndex;
   gvProductQuery.DataBind();
}

protected void gvProductQuery_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
   GridViewRow row = gvProductQuery.Rows[gvLevels.EditIndex];
   TextBox tb = row.FindControl("txtUpdatedBy") as TextBox;
   tb.Text = cUser.DomainLogin;
}

Upvotes: 1

Related Questions