如何在gridview中突出显示一行?

时间:2009-09-02 06:05:36

标签: asp.net gridview highlighting

我的应用程序向用户发送电子邮件,其中包含aspx页面的超链接。 get字符串中有一个guid。 aspx页面显示了包含多个页面的行的gridview。 我的要求是,当点击链接时,应该将用户定向到适当的页面,并且应该突出显示映射到guid的行?

请帮助,紧急。

2 个答案:

答案 0 :(得分:2)

Canavar有一个观点,但是为了一个简单的事情,你可以加载RowDataBound方法并在渲染结束时执行高光。

让我们想象一下(因为你没有提供任何有关它的信息 - 顺便说一句,请给我们一个最详细的问题版本):)

GridView显示了30行,我们需要高亮显示行ID 22。

按照以下步骤操作:

1 - 使用

添加HiddenField
<asp:HiddenField ID="rowID" CssClass="rowids" Value='<%# Eval("YourROWID") %>' />

2 - 现在我们有了行,我们需要的是,当DOM准备就绪时,循环所有行并高亮显示与 selectrow <具有相同值的行/ strong>你在评论中提到

您使用的是jQuery吗?

var selectedRow = '<%= Request["selectrow"] %>';

$(document).ready(function(){

  $(".rowids").each( function() {  // Get and Loop through all class=rowids elements

   if( $(this).value() == selectedRow) {

     // we found it! lets find the TR and add the highlight class to it
      $(this)  // the INPUT ELEMENT
         .parent()   // the TD element
         .parent()   // the TR element
         .addClass("highlight");  // Add the class
   }
  });
});

如果不使用 jQuery (你应该这样做,因为我几乎可以肯定你会在其他地方使用它来改善代码的更多部分)

var selectedRow = '<%= Request["selectrow"] %>';
var arr = document.getElementsByTagName("input");

for (i = 0; i < arr.length; i++) {  // Loop through all input elements

    if( arr[i].className == "rowids" && arr[i].defaultValue == selectedRow ) {
           // it's a rowids element and the value is the one we are looking out

        var tableRow = arr[i].parentNode.parentNode; // get it's TR element
        tableRow.className = tableRow.className + ' hightlight'; // Add the class
        break; // no need to loop through more, we already found the one we are looking for
    }
}

请记住在BODY标记之前的脚本中添加它,因此当在页面中呈现所有元素时将调用它(DOM ready技巧)

答案 1 :(得分:0)

你可以使用JQuery。

$("a").click(function(){
$(this).effect("highlight", {}, 3000);

})

这将是一件简单的事情。希望它有所帮助。