Using JavaScript To Select GridView Rows

來源:互聯網
上載者:User
Sample Table Used

I am actually using Microsoft SQL Server 2005, with the attached DEMO app. You will need to change the Web.Config file to point to your own SQL Server installation. For the GridView data I have chosen to use the master database, dbo.spt_values table, that comes with SQL Server 2005

The master table structure, and the query that the demo app uses are as follows:

How It Works

It's very easy actually, all we do is use a <asp:SqlDataSource/> web control to bind to the master database (remember you'll need to change the connection section in the web.config to point at your own database). The GridView data is obtained using a simple select statement SELECT TOP 5 * FROM dbo.spt_values table, and then have the GridView control use this <asp:SqlDataSource/>. Then we include a <asp:CommandField ShowSelectButton="True" Visible="False" /> field which we set to be invisible.

One more point to note is that the Page must have the following directive set in order to allow the JavaScript postback mechanism that is described below.

So in the page declarations section, ensure the following is set EnableEventValidation="false"

So that's the ASPX file (web form), but we also need to write some code in the code behind and use a little bit of JavaScript. So the code behind (C# for the attached demo) looks like :

Collapse
using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partial class grid : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){}/// <summary>/// Check the item being bound is actually a DataRow, if it is,     /// wire up the required html events and attach the relevant JavaScripts/// </summary>/// <param name="sender">GridView1</param>/// <param name="e">The event args for the RowDataBound event</param>protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e){//check the item being bound is actually a DataRow, if it is, //wire up the required html events and attach the relevant JavaScriptsif (e.Row.RowType == DataControlRowType.DataRow){e.Row.Attributes["onmouseover"] ="javascript:setMouseOverColor(this);";e.Row.Attributes["onmouseout"] ="javascript:setMouseOutColor(this);";e.Row.Attributes["onclick"] =ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);}}/// <summary>/// Show the 1st cell value in the web pages TextBox to show the user/// it is actually selecting rows at client side/// </summary>/// <param name="sender"> GridView1</param>/// <param name="e">The event args for the SelectedIndexChanged event///    </param>protected void GridView1_SelectedIndexChanged(object sender, EventArgs e){TextBox1.Text = GridView1.SelectedRow.Cells[1].Text;}}

This works by attaching 2 JavaScripts to the current GridView row.

  • One for onmouseover which simply sets the current row to be highlighted a certain color. I chose Yellow, but you can change that.
  • One for onmouseout which simply resets the current row to be the original color

There is also a clever line as given below:

e.Row.Attributes[<span class="cpp-string">"onclick"</span>] = ClientScript.GetPostBackClientHyperlink(this.GridView1, <span class="cpp-string">"Select$"</span> + e.Row.RowIndex);

This cunningly creates a client hyperlink which posts back the current ASPX web form, using the Select$0 to select row 0 say.

The ASPX pages JavaScript is as follows:

&lt;script language=<span class="cpp-string">"javascript"</span> type=<span class="cpp-string">"text/javascript"</span>&gt;var oldgridSelectedColor;function setMouseOverColor(element){oldgridSelectedColor = element.style.backgroundColor;element.style.backgroundColor='yellow';element.style.cursor='hand';element.style.textDecoration='underline';}function setMouseOutColor(element){element.style.backgroundColor=oldgridSelectedColor;element.style.textDecoration='none';}&lt;/script&gt;

And that's it. So what we get is now a nice GridView control, that we select rows with using JavaScript, and it looks like a table, rather than a table plus some nasty SELECT button, or a hyperlink column (that is only being used as a row selection method anyway).

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.