Gridview/datagrid row and double click event Implementation code _. NET Tutorials

Source: Internet
Author: User

Features: Click on the selected row and double-tap to open the detail page
Description: The Click event (onclick) uses the SetTimeout delay to modify the delay time, depending on the actual need, and when the double-click is Dbl_click, the response of the Click event is canceled by the global variable.
The usual way to handle rows is to choose to process in Rowdatabound/itemdatabound, where I choose to process in Page.render, at least based on the following considerations
1, RowDataBound only after the call DataBind will trigger, postback through ViewState create empty pieces do not trigger if you need more processing, you need to separate part of the logic into the rowcreated and other events
2, and we want to use the Clientscript.getpostbackeventreference and Clientscript.registerforeventvalidation methods to register the security script, which needs to be in the page's In the Render phase to process . aspx (run directly)

<%@ page language= "C #"%> <%@ Import namespace= "System.Data"%><%--http://community.csdn.net/expert/ topicview3.asp?id=5767096--%><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "><script runat=" server > protected void Page_Load (object sender, EventArgs e) {if (!             IsPostBack) {loadgridviewproductdata ();         Loaddatagridproductdata (); }} protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e) {/* Can of course be here For client-side script binding, however, I chose to handle the overridden page in the Render method because 1. RowDataBound is only triggered after calling DataBind, the postback creates an empty piece by ViewState without triggering if more processing is required, you need to separate part of the logic into the rowcreated and other events in 2. And we want to use the Clientscript.getpostbackeventreference and Clientscript.registerforeventvalidation methods for security The registration of the script, which needs to be processed in the Render phase of the page */} protected void Datagrid1_itemdatabouND (object sender, DataGridItemEventArgs e) {//Hides the secondary button column int cellindex = 0; E.item.cells[cellindex].     attributes["style"] = "Display:none";        } void Loadgridviewproductdata () {DataTable dt = Createsampleproductdata ();         Gridview1.datasource = DT;         Gridview1.databind ();        } void Loaddatagridproductdata () {DataTable dt = Createsampleproductdata ();         DataGrid1.DataSource = DT;     Datagrid1.databind (); } #region Sample Data static DataTable Createsampleproductdata () {datatable tbl = new DataTable ("Produc        TS "); Tbl.         Columns.Add ("ProductID", typeof (int)); Tbl.                 Columns.Add ("ProductName", typeof (String)); Tbl.         Columns.Add ("UnitPrice", typeof (decimal)); Tbl.        Columns.Add ("CategoryID", typeof (int)); Tbl.         Rows.Add (1, "Chai", 18, 1); Tbl.         Rows.Add (2, "Chang", 19, 1); Tbl.         Rows.Add (3, "aniseed syrup", 10, 2); Tbl. Rows.Add(4, "Chef Anton ' s Cajun seasoning", 22, 2); Tbl.         Rows.Add (5, "Chef Anton ' s Gumbo Mix", 21.35, 2); Tbl.         Rows.Add (Zaanse Koeken, 9.5, 3); Tbl.         Rows.Add ("Chocolade", 12.75, 3); Tbl.                Rows.Add ("Maxilaku", 20, 3);     return TBL; } #endregion protected override void Render (HtmlTextWriter writer) {//GridView foreach (GridViewRow row in gridview1.rows) {if (row. RowState = = Datacontrolrowstate.edit) {//Edit status row.                 Attributes.remove ("onclick"); Row.                 Attributes.remove ("ondblclick"); Row.                 Attributes.remove ("style"); Row.                 attributes["title"] = "edit Line";             Continue } if (row. RowType = = Datacontrolrowtype.datarow) {//Click the event, in order to respond to the double-click event, you need to delay the click Response and may need to increase latency//gain as needed ASP.N ET built-in postback script function, return __dopostback (&LT;&LT;EVENTTARGET&GT;&GT;, <<EventArgument>>)//Can be directlyHard-coded write script, row is not recommended. attributes["onclick"] = String.Format ("Javascript:settimeout (\" if (Dbl_click) {{dbl_click=false;}} Else{{{0}}};\ ", 1000*0.3);", Clientscript.getpostbackeventreference (GridView1, "select$" + row.)                 Rowindex.tostring (), true)); Double-click, set Dbl_click=true to cancel the click Response Row. attributes["ondblclick"] = String.Format ("Javascript:dbl_click=true;window.open (' dummyproductdetail.aspx? Productid={0} '); ", Gridview1.datakeys[row. RowIndex].                 Value.tostring ()); Row.                 attributes["style"] = "cursor:pointer"; Row.             attributes["title"] = "Click to select a row, double click to open the Detail page"; }}//DataGrid foreach (DataGridItem item in Datagrid1.items) {if (item. ItemType = = ListItemType.EditItem) {item.                 Attributes.remove ("onclick"); Item.                 Attributes.remove ("ondblclick"); Item.                 Attributes.remove ("style"); Item. attributes["TitlE "] =" edit Line ";             Continue } if (item. ItemType = = ListItemType.Item | | Item. ItemType = = ListItemType.AlternatingItem) {//Click events in order to respond to double-click events, Delay 1 s, may require additional delay//get assisted Support as needed Postback button//relative, GridView supports direct CommandName as <<EventArgument>> so no secondary button buttons required Btnhiddenpostbutton = Item.                 FindControl ("Btnhiddenpostbutton") as Button; Item. attributes["onclick"] = String.Format ("Javascript:settimeout (\" if (Dbl_click) {{dbl_click=false;}}                                 Else{{{0}}};\ ", 1000*0.3);", clientscript.getpostbackeventreference (Btnhiddenpostbutton, null)); Double-click//double-click to set Dbl_click=true to cancel clicking response to item. attributes["ondblclick"] = String.Format ("Javascript:dbl_click=true;window.open (' dummyproductdetail.aspx? Productid={0} '); ", Datagrid1.datakeys[item. ItemIndex].                                  ToString ()); Item. attributes["style"] = "Cursor:pointer"; Item.             attributes["title"] = "Click to select a row, double click to open the Detail page"; }} base.     Render (writer); } </script>

  

Gridview/datagrid row and double click event Implementation code _. NET Tutorials

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.