Tuesday, August 31, 2010

How to solve problem: Page load fire twice when manually call __doPostBack in Javascript?

Scenario:
When user press Enter key in a textbox, then do a postback by __doPostBack.
<script type="text/javascript">
$('#<%=TextBoxName.ClientID%>').keypress(function (event) {
if (event.keyCode == 13) {
__doPostBack('', '');
}
});
</script>
Problem: Page load fire twice.
Solution: put Textbox ClientID into first parameter of __doPostBack

<script type="text/javascript">
$('#<%=TextBoxName.ClientID%>').keypress(function (event) {
if (event.keyCode == 13) {
__doPostBack('<%=TextBoxName.ClientID%>', '');
}
});
</script>

No comments:

Post a Comment