3

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

13
  • I'm fuzzy on what exactly you're trying to do. Are you wanting a button to be clickable and disable multiple clicks so you can ensure only a single postback or do you want the postback disabled for the button entirely? Commented Mar 12, 2012 at 18:21
  • @Brettski, No its not... Commented Mar 12, 2012 at 18:21
  • @JoelEtherton, I have a GridView full of checkboxes so when you click on a btnCommit if it Posts Back I loose the GridView and all the checkboxes... Commented Mar 12, 2012 at 18:23
  • @Nick: It sounds like you're describing the problem based on how you've already decided you want to solve it (trying to short-circuit a button), rather than describe the problem itself. Take a step back and tell us what you're trying to achieve here, because right now it's very unclear what you want such a button to even do, let alone what you want it to not do. Commented Mar 12, 2012 at 18:24
  • @David sir yes sir... I have made an UPDATED section of the question. Thanks for your suggestions. Commented Mar 12, 2012 at 18:30

7 Answers 7

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Is there a way to have the event OnClick fire right away without loading a page_load... I have a GridView full of checkboxes and when a user click on a save button it takes you to the onClick function where then data gets saved into a database... What currently happens is if the user click on a save button the Page_Load loads again and all the selections made by the checkboxes get erased...
I'm not sure but I think you can persist the checkbox statuses with viewstate. But if viewstate is enabled, then unfortunately, that's the evil part of asp.net: Page lifecycle. You can try using ajax if you don't want a full postback.
Then you should only setup the checkboxes (and probably gridview databind etc) in the pageload inside a "if(!IsPostBack){}" statement, so that the checkboxes are only set on first page load and not during a postback. See usage -> msdn.microsoft.com/en-us/library/…
@Simon thats the way I currently have it set up if (!IsPostBack) { if (Request.QueryString["ID"] != null) { tbName.Enabled = false; tbDescription.Enabled = false; } gridDataBind(); }
@Simon but even though I have the bind set in the (!IsPostBack) once the page recycles I cant see any checkboxes
1

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

10 Comments

I already had it set up that way but it still is unable to get the checkboxes once it gets into button Click event.
Once your click fires you will need to call the databind function again to populate the gridview. I will often have a function called SetPageData() - called in the pageload check, and then called after a button is clicked and some actions are taken.
During the initial page load I call a function gridDataBind() which calls get the data out of database and auto generates the checkboxes with default selection. Once the user makes changes to those selections i than the current selection get stored back into database.
So, if I had the GridView bind fire again it will loose the data selected by user.
Grid binding is not required again as Viewstate is default true. So it should persist the HTML.
|
1

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.

Comments

1

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.

Comments

0

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

7 Comments

Thats the way I already have it. But during the button click after it reloads the Page_Load and gets into button click event it looses the gridview.
and what's happening in button click? are you binding grid data to null ?
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Request.QueryString["ID"] != null) { tbName.Enabled = false; tbDescription.Enabled = false; } gridDataBind(); } }
and what's happening in button click?
On the button click it goes into Page_Load checks if its a post back which it is then goes into btnCommit_Click() and thats where I check for GridColumns and get all the checkboxes and when I loop through GridColumns there are none.
|
0

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.

Comments

0

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.