Code for clicking and double-clicking events in the row of the GridView/DataGrid)

Source: Internet
Author: User

Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
LoadGridViewProductData ();
LoadDataGridProductData ();
}
}

Protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{
/*
Of course, you can bind the client script here,
However, I chose to process it in the Render method of the reload page, because
1. RowDataBound is triggered only after DataBind is called, and the creation of null parts through ViewState is not triggered
If you need more processing, you need to separate part of the logic into RowCreated and other events.
2. And we want to use
ClientScript. GetPostBackEventReference and ClientScript. RegisterForEventValidation Methods
Register the security script, which must be processed in the Render stage of the page.
*/
}

Protected void maid (object sender, maid 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 = CreateSampleProductData ();

DataGrid1.DataSource = dt;
DataGrid1.DataBind ();
}

# 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, "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 (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 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 row ";
Continue;
}
If (row. RowType = DataControlRowType. DataRow)
{
// Click the event. To respond to the double-click event, you need to delay the click response. You may need to increase the latency as needed.
// Obtain the built-in callback Script Function of ASP. NET and return _ doPostBack (<EventTarget >,< EventArgument>)
// The script can be written with hard encoding. It is not recommended.
Row. 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 (maid item in maid)
{
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 as needed.
// Obtain the secondary support sending back button
// Relatively speaking, the GridView supports using CommandName directly as <EventArgument>, so no auxiliary button is required.
Button 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 and set dbl_click = 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 );
}

 

 

<% @ 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">

// You can embed the above background code here

</Script>

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "Head1" runat = "server">
<Title> ASP. NET DEMO15: Click and double-click event 2 in the GridView row </title>
<Script>
// Auxiliary global variable, indicating whether to double-click
Var dbl_click = false;
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<H3> function: <Li> click the selected row. </li>
<Li> double-click to open the details page </li>
<H3> Note: <Ul>
<Li> This is an improved version of <a href = "GridView/DataGrid http://www.cnblogs.com/Jinglecat/archive/2007/09/20/900645.html"> ASP. net demo 15: supports both row-click and double-click events </a> </li>
<Li> when you click an event (onclick), the setTimeout delay is used. Modify the delay time as needed. </li>
<Li> when you double-click the event, cancel the event response using the global variable dbl_click. </li>
<Li> common row processing methods are processed in RowDataBound/ItemDataBound. Here I select Page. Render for processing, at least based on the following considerations:
<Li style = "padding-left: 20px; list-style-type: square"> RowDataBound is triggered only after DataBind is called. The callback is not triggered when null parts are created through ViewState.
If you need more processing, you need to separate part of the logic into events such as RowCreated </li>
<Li style = "padding-left: 20px; list-style-type: square"> and we want to use
ClientScript. GetPostBackEventReference and ClientScript. RegisterForEventValidation Methods
Register the security script, which must be processed in the Render phase of the page </li>
</Li>
<Li> for "secondary buttons in the DataGrid support sending back", see <a href = "http://www.cnblogs.com/Jinglecat/archive/2007/07/15/818394.html%22%3EASP.NET DEMO8: Add Server events to each row of the GridView </a>
</Ul>
<Br/>
<Input type = "button" id = "Button1" value = "Rebind" onclick = "location. href = location. href;"/>
<Div style = "float: left">
<H3> GridView Version <Asp: GridView ID = "GridView1" DataKeyNames = "ProductID" runat = "server" AutoGenerateColumns = "False" OnRowDataBound = "gridview#rowdatabound">
<SelectedRowStyle BackColor = "CadetBlue"/>
<Columns>
<Asp: TemplateField HeaderText = "ProductName">
<ItemTemplate>
<% # Eval ("ProductName") %>
</ItemTemplate>
<EditItemTemplate>
<Asp: TextBox ID = "txtProductName" runat = "server" Text = '<% # Bind ("ProductName") %>'/>
</EditItemTemplate>
</Asp: TemplateField>
<Asp: BoundField DataField = "UnitPrice" HeaderText = "UnitPrice"/>
</Columns>
</Asp: GridView> </div>
<Div style = "float: left; padding-left: 100px;">
<H3> DataGrid Version <Asp: DataGrid ID = "DataGrid1" DataKeyField = "ProductID" runat = "server" AutoGenerateColumns = "False" OnItemDataBound = "DataGrid1_ItemDataBound">
<SelectedItemStyle BackColor = "CadetBlue"/>
<Columns>
<Asp: TemplateColumn>
<ItemTemplate>
<Asp: Button ID = "btnHiddenPostButton" CommandName = "Select" runat = "server" Text = "HiddenPostButton" style = "display: none"/>
</ItemTemplate>
</Asp: TemplateColumn>
<Asp: BoundColumn DataField = "ProductName" HeaderText = "ProductName"/>
<Asp: BoundColumn DataField = "UnitPrice" HeaderText = "UnitPrice"/>
</Columns>
</Asp: DataGrid> </div>
</Li>
</Div>
</Form>
</Body>
</Html>

 

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.