单击和双击隐藏/显示LinkBut​​tons

时间:2017-02-04 17:46:51

标签: c# asp.net gridview linkbutton

当我在gridview行上单击时,以及当我双击以仅显示更新时,只需显示删除 LinkButton LinkButton

html代码

                            <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1"
                                CommandArgument='<%# Eval("itemCode") %>'
                                CommandName="DeleteRow" runat="server" >Delete</asp:LinkButton>
                            <asp:LinkButton ID="LinkButton2"
                                CommandArgument='<%# Eval("itemCode") %>'
                                CommandName="Update" runat="server">Update</asp:LinkButton>
                        </ItemTemplate>

后端代码

        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 postbackfor 300 milliseconds by 
            // wrapping the postback command in a 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_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // on single click
            e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
            e.Row.ToolTip = "Click to select row";
            e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
            // on double click
            e.Row.Attributes["ondblclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Edit$" + e.Row.RowIndex);
            e.Row.Attributes["style"] = "cursor:pointer";
        }
    }

我通过检查300毫秒之后是否点击一次双击来区分单击和双击。双击时,我只需要显示更新按钮,然后单击即可删除。

0 个答案:

没有答案