如何在 Asp.net c# 中为 GridView 行启用双击和单击

Posted

技术标签:

【中文标题】如何在 Asp.net c# 中为 GridView 行启用双击和单击【英文标题】:How to enable double and single click for GridView row in Asp.net c# 【发布时间】:2018-02-01 21:53:31 【问题描述】:

我在 Asp.net 中有一个 GridView 行,单击事件正在处理它 我需要在我的网格行上添加一个双击事件。 我该怎么做?

请帮助我。

【问题讨论】:

是的,它在 Windows 窗体中 【参考方案1】:

请关注下面的帖子,这对你真的很有帮助。

Clickable and Double Clickable Rows with GridView and DataList Controls

【讨论】:

【参考方案2】:
        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>  

【讨论】:

【参考方案3】:

这是另一个只处理双击的解决方案:

在前端添加这些

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();
        
       
    

【讨论】:

以上是关于如何在 Asp.net c# 中为 GridView 行启用双击和单击的主要内容,如果未能解决你的问题,请参考以下文章

如何在 C# 中为枚举值添加描述以与 ASP.NET MVC 中的下拉列表一起使用? [复制]

List<T> 的 C# ASP.NET MVC 模型绑定

如何在 ASP.NET Web API 中为 Json.NET 设置自定义 JsonSerializerSettings?

如何在 ASP.Net 中为一组 Web 请求发送身份验证标头

如何在 Asp.Net Core 中为日期绑定设置文化?

如何在 Telerik Grid 中为 ASP.NET MVC 绑定图像