Disable LinkButton when OnClick Event Fires - asp.net

I have an asp:LinkButton as follows:
<asp:LinkButton ID="LinkButtonNewServicesCategory" runat="server"
OnClientClick="this.disabled=true;return false;"
style="float:left;margin-right:5px;" CausesValidation="False">new services category</asp:LinkButton>
The intent is that when the LinkButton is clicked, it disables itself and returns false to prevent the postback (this control is used as a trigger for an animation).
The behavior I'm experiencing is that the LinkButton correctly triggers the animation and returns false, but does not disable itself.
It's inside an updatepanel, but I'm sure that no postback is occurring.
Why doesn't this.disabled=true work?

I'm not familiar with the way asp.net interacts with js but to disable an element with javascript use:
this.disabled=disabled; // not disabled=true
Then, to enable it again, remove the disabled attribute.

Related

asp.net detect DropDownList.SelectedIndexChanged after Button Click (JS disabled)

I have a DropDownList which is connected to a TextBox. When the event SelectedIndexChanged is fired, the text in the Textbox changes. The content of the textbox can also be changed by user input. At the end of the page the user has to submit the data (button).
When javascript is enabled everything works fine, but I have also users where javascript is disabled.
Without javascript the SelectedIndexChanged event of the DropDownList gets called after the Button-Click (before the Click-Event of the button is processed). The user input to the textbox gets overwritten by the SelectedIndexChanged event.
How can I detect in Code behind if the SelectedIndexChanged event was triggerd by the button click? How can I get the text of the textbox changed by the user?
EDIT:
Can I use the property IsPostBackEventControlRegistered? This always seems to be true when I click the button, but is false in SelectedIndexChanged event.
Well, it IS possible, but not easy. You would essentially need to track whether javascript is enabled by using a hidden field, do something like:
<asp:HiddenField ID="JSEnabled" runat="server" ClientIDMode="Status" Value="F" />
<script type="text/javascript">
function checkForJavaScript() {
$("#JSEnabled").val("T");
}
</script>
You call this script from window.onload or jquery.ready event, whatever JS option you like, and this can tell you whether you have JS enabled to do the selectedindexchanged event, or if a button triggered it because JS wasn't enabled. To tell whether the button was clicked and caused the postback, you can check the:
Request.Form.Get("__EVENTTARGET")
Field and see if the ID matches the UniqueID of the button. Every control in ASP.NET sets its ID (usually, but not always, for certain reasons) here. Although, I'm not sure, you may have to set UseSubmitBehavior="False" for that to happen. A hidden field can be manipulated, so it's not fullproof.
The question you should ask is do you really need that complexity?

__doPostBack won't work if JavaScript is disabled

If the JavaScript of browser is disabled then __dopostback won't work. I am looking for the alternative to this because I have radio button and drop down list controls in web page that should fire postback even if JS is disabled.
Autopostback cannot be made to work without javascript, as a workaround, you could put a button next to the radio button/drop down list only for those users with javascript disabled to manually trigger a post back:
<noscript><asp:Button ID="uxManualPostback" runat="server" Text="Go" /></noscript>
In the code behind (server side) you would then hook up the button's click event to do same thing the autopostback of the radio button/drop down list would have done.

When I remove the "disabled" attribute from an asp:button it becomes enabled but doesn't postback?

When I remove disabled attribute using jquery from asp:button it becomes enabled this button do partial postback even the OnClientClick event not fire and server side too reference of ClientID of button is perfect
Thanks in advance

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

Fire Textbox.TextChange event without a button click

I want to call Textbox.OnTextChange event without having to click a submit button or any button. How can I do this? As I enter text in the textbox, I want the ontextchange event to fire.
I would elaborate this further.I've a function for validating my entry in textbox which I'm calling ontextchange property.Now this function should be called without me changing focus from my textbox.
You can use onkeypress , onkeyup and onkeydown events for this.
http://www.w3schools.com/tags/tag_input.asp
Example :
<asp:TextBox ID="TextBox1" runat="server" onKeyPress="javascript:alert('Key Pressed');"></asp:TextBox>
you can set AutoPostBack to true on the textbox control. when the client side change event fires (when the focus changes from the textbox to something else), the page will do a postback automatically.
If you want a postback:
You want to use the onkeypress or onkeyup javascript event to run the __doPostBack javascript method. __doPostPack has some parameters you will need to look up on msdn
Otherwise just use the onkeypress or onkeyup javascript event to do whatever else it is you need with javascript.

Resources