簡體   English   中英

如何在 Asp.net c# 中為 GridView 行啟用雙擊和單擊

[英]How to enable double and single click for GridView row in Asp.net c#

我在 Asp.net 中有一個 GridView 行,單擊事件正在處理它
我需要在我的網格行上添加一個雙擊事件。 我該怎么做?

請幫幫我。

請關注以下帖子,這將對您非常有幫助。

帶有 GridView 和 DataList 控件的可單擊和可雙擊行

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        // Get the LinkButton control in the first cell
                        LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];
                        // Get the javascript which is assigned to this LinkButton

          string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");
                        // To prevent the first click from posting back immediately 
                        // (therefore giving the user a chance to double click) pause the 
                        // postback for 300 milliseconds by using setTimeout
                        _jsSingle = _jsSingle.Insert(11, "setTimeout(\"");
                        _jsSingle += "\", 300)";
                        // Add this javascript to the onclick Attribute of the row
                        e.Row.Attributes["onclick"] = _jsSingle;

                        // Get the LinkButton control in the second cell
                        LinkButton _doubleClickButton = (LinkButton)e.Row.Cells[1].Controls[0];
                        // Get the javascript which is assigned to this LinkButton
                        string _jsDouble = ClientScript.GetPostBackClientHyperlink(_doubleClickButton, "");
                        // Add this javascript to the ondblclick Attribute of the row
                        e.Row.Attributes["ondblclick"] = _jsDouble;
                    }
                }



                protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
                {
                    GridView _gridView = (GridView)sender;

                    // Get the selected index and the command name
                    int _selectedIndex = int.Parse(e.CommandArgument.ToString());
                    string _commandName = e.CommandName;

                    switch (_commandName)
                    {
                        case ("SingleClick"):
                            _gridView.SelectedIndex = _selectedIndex;
                            this.Message.Text += "Single clicked GridView row at index " + _selectedIndex.ToString() + "<br />";
                            break;
                        case ("DoubleClick"):
                            Response.Write("<script type='text/javascript'>detailedresults=window.open('Patient//PatientDicomViewPage.aspx');</script>");

                            // Response.Redirect("ViewPatient.aspx");
                            this.Message.Text += "Double clicked GridView row at index " + _selectedIndex.ToString() + "<br />";
                            break;
                    }
                }


    Design of grid

     <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#DEDFDE" 
                BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" 
                OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand">
                <FooterStyle BackColor="#CCCC99" />
                <Columns>                
                    <asp:ButtonField Text="SingleClick" CommandName="SingleClick" Visible="false"/>
                    <asp:ButtonField Text="DoubleClick" CommandName="DoubleClick" Visible="false"/>
                </Columns>
                <RowStyle BackColor="#F7F7DE" />
                <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
                <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />            
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>
<asp:Label id="Message" runat="server" ForeColor="Red" Font-Bold="true"></asp:Label>  

這是另一種僅處理雙擊的解決方案:

在前端添加這些

OnRowDataBound="YourGrid_RowDataBound" OnRowEditing="YourGrid_RowEditing" 

在后端

protected void YourGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["ondblclick"] = Page.ClientScript.GetPostBackClientHyperlink(YourGrid, "Edit$" + e.Row.RowIndex);           
            e.Row.Attributes["style"] = "cursor:pointer";
        }
    }
    
    protected void YourGrid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        // Excecute your code here
        // Example:
        // get the grid
        GridView gv1 = (GridView)sender;
        //get the row
        GridViewRow gvr1 = (GridViewRow)gv1.Rows[e.NewEditIndex];
         // get data from a cell for the row
        string dataInCellOne= gvr1.Cells[1].Text.ToString();
        
       
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM