单击GridView行上的任意位置以进入编辑模式

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

#0

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

#1

I didn't test this as I don't use GridView, but I do it with other controls. You should be able to put hidden button in the row and call Click on the row via JavaScript:

protected override void Render( System.Web.UI.HtmlTextWriter writer )
{
    foreach( GridViewRow row in gvwOrderItems.Rows )
    {
        if( row.RowType == DataControlRowType.DataRow )
        {
            Button b = new Button();
            b.ID = "whatever";
            b.CommandName = "edit";
            b.Attributes.Add("style","display:none;")
            row.Controls.Add(b);
            // enable click on row to enter edit mode
            row.Attributes.Add("onclick" ,"document.getElementById('" bla.ClientID "').click();");
        }
    }
    base.Render( writer );
}

推荐文章

在teextarea中插入文本无法识别新行

在teextarea中插入文本无法识别新行

推荐文章

获取矩阵项的排名?

获取矩阵项的排名?

推荐文章

Google Maps Apiv3+Load map on page Load+a为空

Google Maps Apiv3+Load map on page Load+a为空

推荐文章

问题PostgreckSqlIt提供程序

问题PostgreckSqlIt提供程序

推荐文章

未生成mysql修复线程

未生成mysql修复线程

推荐文章

“find”和“ls”与GNU并行

“find”和“ls”与GNU并行

推荐文章

如何在.NET中为MVP模式公开用户控件的属性

如何在.NET中为MVP模式公开用户控件的属性

推荐文章

MvcMailer:上下文已在跟踪具有相同资源Uri的不同实体

MvcMailer:上下文已在跟踪具有相同资源Uri的不同实体

推荐文章

如何在PowerShell中获取字符串而不是数组?

如何在PowerShell中获取字符串而不是数组?

推荐文章

是否可以将数组存储到EncryptedLocalStore项中?空气

是否可以将数组存储到EncryptedLocalStore项中?空气

推荐文章

电脑出现分割错误?

电脑出现分割错误?

推荐文章

emacs标准/缓冲区显示表更改(音译实验)

emacs标准/缓冲区显示表更改(音译实验)

推荐文章

python/pygame,将输入从一个类传递到另一个类

python/pygame,将输入从一个类传递到另一个类

推荐文章

从接收委托类型参数的方法调用具有不同签名的方法

从接收委托类型参数的方法调用具有不同签名的方法

推荐文章

弹出式UIPickerView(iphone)

弹出式UIPickerView(iphone)

推荐文章

获取自定义生成步骤的包含目录

获取自定义生成步骤的包含目录