3

I already made my code. But when user enter the EnterKey, it is calling 2 button event on server side. I want to call only one event.

I have three Asp.net buttons,

<asp:Button ID="btnSearch" class="btn btn-info" runat="server" Text="SEARCH" Width="100" OnClick="btnSearch_Click" />
<asp:Button ID="btnSaveFile" runat="server" Text="SaveFile" OnClick="btnSaveFile_Click"/>
<asp:Button ID="btnLoadData" CssClass="btn btn-info"  runat="server" Text="Ok" OnClick="btnLoadData_Click" />

Those 3 buttons will perform 3 different things. I have a textbox, once user enter the data and when they press the enter I want to call the btnSaveFile click event.

This is that text box

<asp:TextBox ID="txtFileName" CssClass="form-control" runat="server"></asp:TextBox>

Note

I already define the click function for btnSaveFile here is the code

$('#btnSaveFile').click(function () {
    var fileNames = $('#txtFileName').val();
    var flag = 'returnTrue';
    $.ajax({
        url: 'ReportTotalSalesPivot.aspx/getFileExistOrNot',
        method: 'post',
        async: false,
        contentType: 'application/json',
        data: '{fileName:"' + fileNames + '",reportName:"TotalSales"}',
        dataType: 'json',
        success: function (data) {
            if (data.d === 'dataExist') {
                flag = 'returnFalse';
                $('#lblSaveErrorMsg').text('This filename already exist. Please change the name');
            }
            else {
                //alert('else');
                $(".FilterTable > tbody > tr").each(function () {
                    $("#hiddenTableRecord").val($("#hiddenTableRecord").val() + $(this).find(".FieldNameID").html() + "$$$" + $(this).find(".OperatorID").html() + "$$$");
                });
                $("#hiddenTableRecord").val('');
                return true;
            }
        },
        error: function (error) {
            alert('Please Call Administrator');
        }
    });
    //alert('faisal'+flag);
    if (flag === 'returnFalse') {
        return false;
    }

});

The below code is for to call the above function.

$('#txtFileName').keydown(function (e) {
    //enter key
    var key = e.which;
    if (key == 13) {
        $('#btnSaveFile').click();
        return false;
    }
})

Problem

keydown function is working fine. But at the same time when I press the enter key it is calling btnSearch server side code also. It should not call btnSearch. It should call btnSaveFile on server side

Doubt

Totally I have 3 server side button, But it is calling only 2 why? (Just want to know the reason). Target: want to call only one button

Thanks

4
  • Can you inspect the code using any DevTool like Chrome inspect. Commented Apr 3, 2017 at 7:07
  • use the attribute UseSubmitBehaviour="false" like <asp:Button ID="button1" runat="server" UseSubmitBehavior="false" Text="just a button" /> Commented Apr 3, 2017 at 7:08
  • @RonyLoud What is the use of UseSubmitBehavior="false"?. So this button will not execute any more when user press the enter key? Commented Apr 3, 2017 at 7:24
  • UseSubmitBehavior Commented Apr 3, 2017 at 8:04

1 Answer 1

1

however i would suggest put your button and textbox into update panel and then set default button on update panel. like below:

<asp:Panel ID="Panel1" runat="server" DefaultButton="btnSearch">     
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> 
    <ContentTemplate> 
        <input runat="server" id="txtSearch" class="form-control" type="text" placeholder="Search the Store..." /> 
        <asp:Button ID="btnSearch" class="btn btn-info" runat="server" Text="SEARCH" Width="100" OnClick="btnSearch_Click" /> 
    </ContentTemplate></asp:UpdatePanel> 
</asp:Panel>
Sign up to request clarification or add additional context in comments.

6 Comments

you would have asked in comments.
don't have much reputation point to add comments on your question.
however i would suggest put your button and textbox into update panel and then set default button on update panel. like below: <asp:Panel ID="Panel1" runat="server" DefaultButton="btnSearch"> <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate> <input runat="server" id="txtSearch" class="form-control" type="text" placeholder="Search the Store..." /> <asp:Button ID="btnSearch" class="btn btn-info" runat="server" Text="SEARCH" Width="100" OnClick="btnSearch_Click" /> </ContentTemplate></asp:UpdatePanel> </asp:Panel>
let me know either it works for you or no. in this code no need to add Jquery code specifically for enter button..update panel will do by self
This is not exact answer, any how it is helpful to find the solution. Now I got it. thanks. I have edited your answer, please approve it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.