【问题标题】:Highlight gridview row in update panel without posting back在更新面板中突出显示 gridview 行而不发回
【发布时间】:2010-08-07 07:33:29
【问题描述】:

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

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。

【问题讨论】:

    标签: c# .net asp.net gridview


    【解决方案1】:

    How to highlight gridview when row is selected

    为此,您必须在 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() + "')");
            }            
        }
    

    并添加这个脚本

    <script language="javascript" type="text/javascript">
        var gridViewCtlId = '<%=ctlGridView.ClientID%>';
        var gridViewCtl = null;
        var curSelRow = null;
        function getGridViewControl()
        {
            if (null == gridViewCtl)
            {
                gridViewCtl = document.getElementById(gridViewCtlId);
            }
        }
    
        function onGridViewRowSelected(rowIdx)
        {
            var selRow = getSelectedRow(rowIdx);
            if (curSelRow != null)
            {
                curSelRow.style.backgroundColor = '#ffffff';
            }
    
            if (null != selRow)
            {
                curSelRow = selRow;
                curSelRow.style.backgroundColor = '#ababab';
            }
        }
    
        function getSelectedRow(rowIdx)
        {
            getGridViewControl();
            if (null != gridViewCtl)
            {
                return gridViewCtl.rows[rowIdx];
            }
            return null;
        }
    </script>
    

    它会突出显示选定的行..

    【讨论】:

    • 我应该提到我有代码来做突出显示,这是添加这个和选择在一起的问题。此代码是对我的 +1 的改进,谢谢。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      ... 或 。你需要把它应用到里面的元素上。

      您可以尝试以下几个调整-

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

      第一个在代码中对我有用。没有任何方便的东西来测试第二个。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多