Disable submit to asp.net click button

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

I've done some research online and most of them use onClientClick and use javascript. Is there a way to call the asp.net function "btnCommit_Click" in onClick instead of using onclientclick and using javascript?

If not, how can I enable the asp.net function with javascript?

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

      

UPDATED I have a GridView that contains checkboxes, as soon as the user makes a change and clicks the Save button, the Back message pops up and I lose all the highlighted checkboxes, which I was thinking about disabling the postback in the OnClick event of the button will resolve the issue. ..

+3


source to share


7 replies


I have a GridView that contains checkboxes, as soon as the user makes their changes and click the Save button, the Back message appears and I lost all selected checkboxes

You have to keep Page_Load

under Page.IsPostback

when the value is false as shown below.

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

      

This will not disable the grid selection made after the button was pressed.




Your GridView EnableViewState must be True


Your template fields should be on the designer page

0


source


I didn't understand exactly what you are trying to achieve. But if you want both js and server side to add 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 event Onclick

will never be raised. otherwise, return true and postback will occur.
Update:
Based on comments and updating, if you want to keep the state of the checkboxes in your gridview, you need to avoid overwriting anything that might affect the states of your controls in the Page_Load event. It just means that you should check (IsPostback

) on the Page_Load event, and if that's true, you shouldn't be doing anything in your UI elements or you'll lose your states.



+3


source


It looks like your problem is not that you are binding the data and page settings inside the first page load check.

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
    }
}

      

+1


source


While you can only use VBScript with Internet Explorer (I don't believe other browsers support it), this is considered bad practice. To run VB / C # .net you need to do some kind of callback / callback to the server. Also, use Javascript.

0


source


Don't use Autopostback=true

for any form controls if you want to submit form data to a server function

* Form page

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

      

* Server side *

Sub btn_Click()

      

// code here

End Sub

      

0


source


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

       <input type='button' /> 

      

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

0


source


If you don't want to load the page or redirect the page when the asp button is clicked, you just need to change the CausesValidation property to false.

0


source







All Articles