Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click anywhere on a GridView row to enter the edit mode

I want to duplicate the same functionality of the edit button using a single click anywhere in a GridView row.

The code below does this, but with a major problem: if the user clicks off of one textbox to the next, the edit command fires again, and the changes made to the previous textbox revert back to it's original value.

Any suggestions on how to fix this behaviour?

Or, is there a better approach to accomplishing this?

EDIT: This was resolved by adding a check for row.RowState.HasFlag( DataControlRowState.Edit ). See code below:

protected override void Render( System.Web.UI.HtmlTextWriter writer )
{
    foreach( GridViewRow row in gvwOrderItems.Rows )
    {
        if( row.RowType == DataControlRowType.DataRow &&
            row.RowState.HasFlag( DataControlRowState.Edit ) == false )
        {
            // enable click on row to enter edit mode
            row.Attributes["onclick"] =
                ClientScript.GetPostBackClientHyperlink( gvwOrderItems, "Edit$" + row.DataItemIndex, true );
        }
    }
    base.Render( writer );
}
like image 401
jim31415 Avatar asked Aug 31 '11 19:08

jim31415


1 Answers

Check for row.RowState.HasFlag( DataControlRowState.Edit ).

protected override void Render( System.Web.UI.HtmlTextWriter writer )
{
    foreach( GridViewRow row in gvwOrderItems.Rows )
    {
        if( row.RowType == DataControlRowType.DataRow &&
            row.RowState.HasFlag( DataControlRowState.Edit ) == false )
        {
            // enable click on row to enter edit mode
            row.Attributes["onclick"] =
                ClientScript.GetPostBackClientHyperlink( gvwOrderItems, "Edit$" + row.DataItemIndex, true );
        }
    }
    base.Render( writer );
}
like image 164
jim31415 Avatar answered Oct 05 '22 22:10

jim31415