最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to call postback using javascript on ASP.NET form - Stack Overflow

matteradmin0PV0评论

I have a web form with textbox and button. I want after "ENTER" key click on textbox postbak form.

I am using next 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 javascript code for button event onclick.

I get two alerts, but postback doesnot happen.

Any ideas what is wrong?

I have a web form with textbox and button. I want after "ENTER" key click on textbox postbak form.

I am using next 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 javascript code for button event onclick.

I get two alerts, but postback doesnot happen.

Any ideas what is wrong?

Share Improve this question edited Sep 23, 2010 at 5:22 Michael Petrotta 61k27 gold badges152 silver badges181 bronze badges asked Apr 29, 2010 at 14:53 AntonAnton 9,98112 gold badges43 silver badges73 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11

ASP.NET already creates a client side javascript method __doPostBack to support postback.

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

Easier way of doing it is to enclose the controls in a panel and use defaultbutton attribute on the panel, like this:

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

Then when you press 'enter' after entering the value in the text box it will behave as if you clicked on the btnSave button.

Post a comment

comment list (0)

  1. No comments so far