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. ..
source to share
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
source to share
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.
source to share
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
}
}
source to share