3

NET application, I have inserted a button that call a Javascript function (OnClientClick event) and a VB.NET function (OnClick event)

<asp:Button OnClientClick="jsfunction() " OnClick="vbfunction" Text="Submit" runat="server" />

The problem is that when I click the button, it refreshes the page and delete the content of the text boxes.

I have tried with inserting return false; on the OnClienClick event, but it doesn't execute the OnClick Event.

How can I avoid the page reload ?

P.S.: At the end of the Javascript function a new window is opened window.open(newWindow.aspx), but I want that the first page mantain the value inserted by the user in the Text Boxes.

Thanks in advance :)

user2217039
  • 131
  • 2
  • 4
  • 11
  • The textbox shouldnt lose its value unless you manually clear it or you arent using the ViewState – Ian May 04 '13 at 08:57
  • @lan 1- because i have never used AJAX can you explain me how i can use it to resolve my problem ? 2- I use html text boxes which content is deleted at page refresh. This doesn't happen if i use asp text boxes, but i have to use html text boxes (I have not decided this :( ) Thanks in advance... – user2217039 May 05 '13 at 14:41

4 Answers4

6

You need to use return statement at two points.

OnClientClick="return jsfunction();"

function jsfunction()
{
  //
  return false;
}

OR, you can return false after the function call like this.

OnClientClick="jsfunction(); return false;"

Note if you want to do postback conditionally then you need to return true or false.

OnClientClick="return jsfunction();"

function jsfunction()
{
  if(conditionForPostBack)
      return true;
  else
      return false;
}
Adil
  • 146,340
  • 25
  • 209
  • 204
  • 1
    "I have tried with inserting return false; on the OnClienClick event, but it doesn't execute the OnClick Event." – Ian May 04 '13 at 08:55
  • I already explained. The OP already used this and it worked, but since the postback is prevented with "return false", it isnt what they want. Im pretty sure theyre looking for AJAX – Ian May 04 '13 at 09:04
2

or you can disable the submit behaviour. By default asp.net renders button as submit button. if you disable submit behaviour it will render button as button type

<asp:Button UseSubmitBehavior="false" OnClientClick="jsfunction() " OnClick="vbfunction" Text="Submit" runat="server" />

But with this code it will not fire server side event "OnClick"

Ujjwal Manandhar
  • 2,194
  • 16
  • 20
0

if you are not going to trigger the button with C# Codebehind function, then you dont need to use asp:Button. Therefore you can use a regular html .

<button id='btn_1' onclick='ajax_function()'>Button</button>

html button is much easier and faster. if you use asp:button, then you should use clientid() function to catch the control to trigger the ajax.

Arif YILMAZ
  • 5,754
  • 26
  • 104
  • 189
0

Searching for the same thing as you i find a patch:

If you call a method server side, you can use AJAX with the update panel, but that didn't worked for me. But you can save what you want in Session, so you have it as far as Session lasts.

// Save at SessionParameter the elementToSave we want.
this.Session["SessionParameter"] = elementToSave;

// Retrieve the info from the Session
ElementYouNeededToSave = (TypeOfTheElement)Session["SessionParameter"];

Hope this will help someone in my situation.