Nick Manojlovic
Nick Manojlovic

Reputation: 1171

Disable Postback on asp.net Button Click

I would like to disable postback on a onClick event of a button.

I did some research online and most are using onClientClick and using javascript. Is there a way to call a asp.net function "btnCommit_Click" on the onClick instead of using onclientclick and using javascript?

If not, how would I be able to incorporate a asp.net function with javascript?

<asp:Button ID="btnCommit" runat="server" Text="Save" TabIndex="5" 
    onclick="btnCommit_Click" />

UPDATED I have a GridView which contains checkboxes, once the user makes their changes and click on a "Save Button" a Post Back occurs and I loose all the selections made to the checkboxes I was thinking of disabling the postback on the OnClick Event of the button would solve that issue...

Upvotes: 3

Views: 38280

Answers (7)

vishal melwani
vishal melwani

Reputation: 11

If you dont want to page load or postback a page when click on asp button, you just have to change a property called CausesValidation to false.

Upvotes: 1

Kevin Vella
Kevin Vella

Reputation: 1879

In most of my cases, what I do is convert the button to an

       <input type='button' /> 

so that I can access it via javascript or jquery. This may not be your case tough.

Upvotes: 1

Varun
Varun

Reputation: 1

Dont use Autopostback=true to any form controls,if you want to send the form data to the server side function

*Form Page*

 asp:Button ID="btn" runat="server" Text="Post Scrap" OnClick="btn_Click"  

*Server side*

Sub btn_Click()

//code here

End Sub

Upvotes: 0

Pankaj
Pankaj

Reputation: 10095

I have a GridView which contains checkboxes, once the user makes their changes and click on a "Save Button" a Post Back occurs and I loose all the selections made to the checkboxes

You have to keep the Page_Load under Page.IsPostback during is's value to false like below..

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        //Your code
    }
}

This will not cause to loose you grid checkbox selection made after clicking the button.


Your GridView EnableViewState must be True


Your template fields must be in designer page

Upvotes: 0

Simon
Simon

Reputation: 577

Sounds like your problem is not putting your databinding and page setup inside a check for first page load.

http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx

private void Page_Load()
{
    if (!IsPostBack)
    {
        // Do your databinding etc here to stop it occuring during postback
    }
}

Upvotes: 1

Kamyar
Kamyar

Reputation: 18797

I didn't exactly get what you are trying to achieve. But if you want to have both js and server-side to gether on an asp.net button, use OnClientClick and Onclick together. if you want to cancel the postback, return false in the OnClientClick js function. This way, the server-side OnClick event will never be called. otherwise, return true and the postback will occur.
Update:
Based on the comments and you update, if you want to persist the state of checkboxes in your gridview, you have to avoid overwriting anything that can affect you controls' states in the Page_Load event. What this simply means that you have to check for (IsPostback) in Page_Load event, and if it's true, you shouldn't do anything on your UI elements or you will lose their states.

Upvotes: 3

Robert Beaubien
Robert Beaubien

Reputation: 3156

While you can use VBScript with Internet Explorer only (I don't believe any other browsers support it), it is considered bad practice. To run VB/C#.net, you have to do some sort of postback/callback to the server. Other than that, use Javascript.

Upvotes: 0

Related Questions