Click and double-click an event in the row of the GridView/DataGrid to implement the Code _. Net tutorial.

Source: Internet
Author: User

Click and double-click an event in the row of the GridView/DataGrid to implement the Code _. Net tutorial.

Function: Click the selected row and double-click to open the details page.
Note: The setTimeout delay is used for clicking an event (onclick), and the delay time is modified based on actual needs. When you double-click an event, the global variable dbl_click is used to cancel the event response.
Common row processing methods will be processed in RowDataBound/ItemDataBound. Here I select Page. Render for processing, at least based on the following considerations:
1. RowDataBound is triggered only after DataBind is called. Creating an empty part through ViewState for sending a message is not triggered. If more processing is required, you need to separate some logics into RowCreated and other events.
2. In addition, we want to use the ClientScript. GetPostBackEventReference and ClientScript. RegisterForEventValidation methods to register security scripts. The latter must process. aspx (run directly) in the page Render phase)

<% @ 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) {/* You can bind client scripts here,, I chose to process in the Render method of the reload page, because 1. rowDataBound is triggered only after DataBind is called. If more processing is required, you need to separate some logics into RowCreated and other events. and we want to use ClientScript. getPostBackEventReference and ClientScript. registerForEventValidation Method for security script The latter must be in the Render stage of the page to process */} protected void DataGrid1_ItemDataBound (object sender, DataGridItemEventArgs e) {// hide 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 = CreateSampleProduct Data (); maid = dt; maid () ;}# region sample data static DataTable CreateSampleProductData () {DataTable tbl = new DataTable ("Products"); 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, "Ch Ang ", 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 (47, "Zaanse koeken", 9.5, 3); tbl. rows. add (48, "Chocolade", 12.75, 3); tbl. rows. add (49, "Maxilaku", 20, 3); return tbl ;}# endregion protected override void Render (HtmlTextWriter writer) {// GridView foreach (GridViewRow r Ow 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 row"; continue;} if (row. rowType = DataControlRowType. dataRow) {// click the event. In response to the double-click event, you need to delay the click response. You may need to increase the latency as needed. // obtain ASP. NET built-in sending back Script Function, return _ doPostBack (<EventTarget>, <EventArgument>) // can be written in hard encoding directly 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 and set dbl_click = true to cancel the 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 and double-click to open the details 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 row "; continue;} if (item. itemType = ListItemType. item | item. itemType = ListItemType. alternatingItem) {// click the event. In response to the double-click event, the latency is 1 s. You may need to increase the latency // obtain the secondary support sending back button as needed. // relatively speaking, the CommandName can be directly used as <EventArgument>. Therefore, no auxiliary Button is required. findControl ("btnHiddenPostButton") as Button; item. attributes ["onclick"] = String. format ("javascript: setTimeout (\" if (dbl_click) {dbl_c Lick = false ;}} else {{ 0 }};\ ", 1000*0.3);", ClientScript. getPostBackEventReference (btnHiddenPostButton, null); // double-click and set dbl_click to true to cancel the Response 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 and double-click to open the details page" ;}} base. render (writer) ;}</script> 

  

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.