Select button with one click and only open with two clicks

6

I have an ImageButton in webform that represents directories in a grid using repeater. How to do that with 1 click just select the button, and only open with two clicks?

 <asp:Repeater ID="rtInlineBlock" runat="server">
                        <ItemTemplate>
                            <div class="block">
                                <asp:HiddenField ID="idDirectorio" runat="server" Value='<%# Eval("guid") %>' />
                                <asp:ImageButton ID="btSend" runat="server" ImageUrl='<%# Eval("imgPath") %>' OnClick="btSend_Click" />

                                <div class="bottom">
                                    <asp:Label ID="lblNome" runat="server" Text='<%# Eval("xInfo") %>' />
                                </div>
                            </div>
                        </ItemTemplate>
                    </asp:Repeater>  

Codebehind:

 protected void btSend_Click(object sender, ImageClickEventArgs e)
    {
        var btSend = sender as ImageButton;
        var rtItem = btSend.Parent as RepeaterItem;
        var idDiretorioSelecionado = rtItem.FindControl("idDirectorio") as HiddenField;

        //faz algo

        }

I just want you to enter this example with a double click.

That way he called the event by clicking once, I wanted to click once and just select and I can handle it, and with two open the event.

    
asked by anonymous 25.02.2015 / 21:18

1 answer

0

One solution is to use the jQuery dblclick event.

Example:

ASPX

<asp:Repeater ID="rtInlineBlock" runat="server">
    <ItemTemplate>
        <div class="block">
            <asp:HiddenField ID="idDirectorio" runat="server" Value='<%# Eval("guid") %>' />
            <asp:ImageButton ID="btSend" ClientIDMode="Static" runat="server" ImageUrl='<%# Eval("imgPath") %>' OnClientClick="return false;" />


            <div class="bottom">
                <asp:Label ID="lblNome" runat="server" Text='<%# Eval("xInfo") %>' />
            </div>
        </div>
    </ItemTemplate>
</asp:Repeater>
<script type="text/javascript">
    $(function () {
        $("#btSend").dblclick(function () {
            $.ajax({
                type: 'POST',
                url: '<%= ResolveUrl("~/Default.aspx/SendClick")%>',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json'
            });
        });
    });
</script>

Notice the addition of the ClientIDMode="Static" attribute in the ImageButton. It is necessary for us to select the element via jQuery with $ ("# btSend"). Additionally, you must remove the original OnClick event and include the OnClientClick property="return false;" to prevent PostBack from occurring on the page when you click the button.

Code behind:

[WebMethod]
public static void SendClick()
{
    //faz algo
}

We are making use of a WebMethod to be called via AJAX on the client. Note that the method must be static and be decorated with the [WebMethod] attribute. In my example the function is as void, but you can return any type, including complex types. The framework takes care of serialization for JSON.

Note: For a call to a WebMethod to work in a WebForms application, you must change the following setting in the ~ / App_Start / RouteConfig.cs file:

From:

settings.AutoRedirectMode = RedirectMode.Permanent;

To:

settings.AutoRedirectMode = RedirectMode.Off;

Without this, the AJAX request receives a 401 (unauthorized) return.

I hope I have helped you.

    
04.03.2015 / 18:43