惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Cybersecurity and Infrastructure Security Agency CISA
月光博客
月光博客
Apple Machine Learning Research
Apple Machine Learning Research
量子位
Hugging Face - Blog
Hugging Face - Blog
罗磊的独立博客
小众软件
小众软件
T
Tailwind CSS Blog
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
V
Visual Studio Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
The Exploit Database - CXSecurity.com
T
Tenable Blog
博客园 - 叶小钗
宝玉的分享
宝玉的分享
P
Privacy International News Feed
T
Tor Project blog
博客园_首页
AWS News Blog
AWS News Blog
雷峰网
雷峰网
C
Cisco Blogs
Help Net Security
Help Net Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
博客园 - 【当耐特】
T
Threat Research - Cisco Blogs
Last Week in AI
Last Week in AI
K
Kaspersky official blog
人人都是产品经理
人人都是产品经理
Recent Commits to openclaw:main
Recent Commits to openclaw:main
S
Schneier on Security
博客园 - Franky
W
WeLiveSecurity
L
LINUX DO - 热门话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Proofpoint News Feed
大猫的无限游戏
大猫的无限游戏
腾讯CDC
L
Lohrmann on Cybersecurity
J
Java Code Geeks
美团技术团队
博客园 - 司徒正美
The Cloudflare Blog
V
V2EX

博客园 - josephshi

识别文本文件编码 文件分割 合并 小软件 呵呵 APACHE+ASP.NET 出现问题 compiere/adempiere+pgsql8.2+RHEL4+jdk1.5 学习 memcached 地震... Visual Studio 2008 简体中文正式版下载及序列号(无使用期限限制,正式版) How to Write a Provider Model Retrieving middle rows from a table Snap it! - How to take a screen shot using .NET - josephshi 我用hsqldb 找出闰年 "一个特牛的日期时间判断正则表达式"--我的修改版 - josephshi - 博客园 Copy a table from one database to another in SQL Server 2005 个人用Mozilla FIREFOX的感受 RSS阅读量大于页面访问量 美工太差,效果不好看 有点怪怪的 ASP.NET Validation Controls – Important Points, Tips and Tricks
Highlight a Row in GridView without a postback using ASP.NET and JavaScript
josephshi · 2008-04-02 · via 博客园 - josephshi

Selecting a row in the GridView causes a postback. In order to highlight a row in the GridView, you have to set the ‘SelectedRowStyle’ property which takes effect when the postback occurs. In this article, we will see how to highlight a row without causing a postback.

We will be using the RowCreated event of the GridView. A GridViewRow object is created for each row in the control before the GridView is rendered. Whenever a row in the GridView gets created, the RowCreated event is fired. Using this event, we can customize the behavior of the GridView. For e.g.: adding client script to the row or customizing the content of the row. Let us see an example where we will be adding some client script to the GridView. I assume that you have some experience of creating data sources and binding controls to it.

Perform the following steps:

Step 1: Create an asp.net website. In the Default.aspx page, add a GridView and a SqlDataSource control to it.

Step 2: Configure the connection of SqlDataSource to point to the Northwind database.  Create query for the Select command to fetch records from the Customer table. The resultant code will look similar to the one given below:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"

            SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address], [City] FROM [Customers]">

        </asp:SqlDataSource>

The web.config will look similar to the following

<connectionStrings>

            <add name="NorthwindConnectionString" connectionString="Data Source =.;Integrated Security = SSPI; Initial Catalog=Northwind;"/>

</connectionStrings>

Step 3: Once the SqlDataSource has been configured, bind the GridView to this data source. Also set ‘AllowPaging’ and ‘AllowSorting’ to true. The mark up will look similar to the following:

<body>

    <form id="form1" runat="server">

    <div>

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True">

            <Columns>

                <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />

                <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />

                <asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />

                <asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" SortExpression="ContactTitle" />

                <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />

                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />

            </Columns>

        </asp:GridView>

    </div>

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"

            SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address], [City] FROM [Customers]">

        </asp:SqlDataSource>

    </form>

</body>

Step 4: Now switch to the design mode and select the GridView. Go to the properties window (F4) and click on the lightning like bolt to display the events of the GridView. Double click the RowCreated event to add the event. The mark up will look similar to the following

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True" OnRowCreated="GridView1_RowCreated">

...

</asp:GridView>

Step 5: In the code behind of Default.aspx, add the following code to the RowCreated event handler

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)

    {

        e.Row.Attributes.Add("onMouseOver", "this.style.background='#eeff00'");

        e.Row.Attributes.Add("onMouseOut", "this.style.background='#ffffff'");       

    }

As you are already aware that the GridView is rendered as a HTML table and each row as  <TR>. In the code shown above, we are using the Attributes property of the AttributeCollection to add extra properties to the <TR> element. The onMouseOver and the onMouseOut events are added that enable the row to change its color whenever the mouse is over a particular row.

Run the application and see the color of the rows changing, that too without a postback!!

Well that was a quick overview of the RowCreated event. You can also use the same event to find the index of the row clicked.  Just use e.Row.DataItemIndex.ToString() to retrieve the selected row index information.

I hope you liked the article and I thank you for viewing it.

If you liked the article, please subscribe to my RSS feed over here.