How to trigger postback using javascript in ASP.NET form

I have a web form with a text box and a button. I want after "ENTER" to click on the postbak text box.

I am using the following code:

onkeypress=" if(event.keyCode==13)
 { alert(2);
WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('ctl00$ContentPlaceHolder1$btnSearch', '', true, '', '', false, false));
alert(2); 
return false;}

Where WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('ctl00$ContentPlaceHolder1$btnSearch', '', true, '', '', false, false));

is the javascript code for the onclick button event.

I get two warnings but postback does not occur.

Any ideas what is wrong?

+3
source share
2 answers

ASP.NET is already creating a __doPostBack client-side javascript method to support postback.

Example: __doPostBack('__Page', 'MyCustomArgument');

+10
source

The easiest way to do this is to enclose the controls in a panel and use the defaultbutton attribute on the panel, for example:

<asp:Panel ID="pan1" runat="server" DefaultButton="btnSave">
    <asp:TextBox ID="txt1" runat="server" />
    <asp:Button ID="btnSave" runat="server" />
</asp:Panel>

, 'enter' , , btnSave.

0

Source: https://habr.com/ru/post/1743371/


All Articles