在更新面板中突出显示gridview行而不回发

我在更新面板中有一个gridview,其中包含以下代码来选择一行,这反过来会更新另一个更新面板,其中包含表单记录中的详细信息。

protected void gvMainGrid_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { //Make the entire row clickable to select this record //Uses javascript to post page back e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';"; e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';"; e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(this.gvMainGrid, "Select$" + e.Row.RowIndex)); } } 

我从数据库手动绑定gridview并且不想重新绑定网格只是为了突出显示该行,但我似乎无法向onclick事件添加任何javascript,它似乎要么显示GetPostBackClientHyperlink或行高亮显示JavaScript的。

如何在选择行时突出显示gridview

为此,您必须在OnRowCreated事件的代码隐藏文件中编写此代码,或者您也可以在网格的OnRowDataBound事件中编写此代码…

  protected void ctlGridView_OnRowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes.Add("onclick", "onGridViewRowSelected('" + e.Row.RowIndex.ToString() + "')"); } } 

并添加这一个脚本

  

它会突出显示所选行..

我正在努力将点击事件添加到行数据绑定:

 e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(this.gvMainGrid, "Select$" + e.Row.RowIndex)); e.Row.Attributes.Add("onclick", "onGridViewRowSelected('" + e.Row.RowIndex.ToString() + "')"); 

使用’;’在行突出显示方法后添加PostBack选择 似乎有效。

 e.Row.Attributes.Add("onclick", "onGridViewRowSelected('" + e.Row.RowIndex.ToString() + "');" + ClientScript.GetPostBackClientHyperlink(this.gvMainGrid, "Select$" + e.Row.RowIndex)); 

首先,您不能将文本修饰应用于

…或

。 您需要将其应用于内部元素。

您可以尝试以下几项调整 –

 e.Row.Attributes.Add("onmouseover", "this.style.cursor='hand';"; e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(this.gvMainGrid.ClientId, "Select$" + e.Row.RowIndex)); 

第一个代码对我有用。 没有任何方便测试第二。