Why do ASP.NET LinkButton controls with OnClientClick attribute and disabled by setting Enabled="false" still render onclick event handler in HTML while Button controls don't?
It seems counter-intuitive. Since anchors can't really be disabled in browsers, it makes more sense not to attach an onclick event (and href attribute) if it has been set disabled on server-side.
Well I would agree that it doesn't server much purpose, but without changing the way the linkbutton renders with one of the many methods built into asp.net there really isn't anything you can do about it. Unless you want to conditionally handle clicks in clientside code and check element attributes. This is just the way it is currently implemented so when you need the functionality of a button that can be disabled it is best to stay way from linkbuttons or anchors entirely.
This really has little to do with asp.net.
A hyperlink button still fires the onclick event even when disabled. Bottom line: baked into HTML. (An input tag, when disabled, does not fire.)
Click Me!
Related
In my website I have a vb.net ascx control which contains a number of textboxes and a button. When the button is clicked I want to disable it, then perform some actions using the values of the textboxes, and then enable the button again. The problem i'm having is that i cannot disable the button.
The ascx control contains an asp:Panel, and inside that is an asp:UpdatePanel which contains the textboxes and the button. Could the asp:Panel or the asp:UpdatePanel be preventing the button being disabled? I can empty the textboxes without a problem, but nothing seems to work on the button.
I've tried
btnButton.Enabled = False
and
btnButton.Attributes.Add("disabled")
but they have no effect. Neither does setting the Visible property to false. The UpdateMode on the Asp:UpdatePanel is set to Always. I'm not too familiar with Asp:Panels or Asp:UpdatePanels so i'm guessing it's something to do with them. Does anyone know the correct way to do this?
After some more research I came across the solution. I just needed to add some attributes to my button like this:
<asp:Button ... OnClientClick="this.disabled=true;" UseSubmitBehavior="false" />
This will disable the button when it is clicked, but still allows the codebehind to run as well. When the codebehind has completed the button will be enabled again.
I have a user control with a delete button. When a button is clicked, an event fires which deletes a record from the database. Now, the control is placed in Default.aspx. The whole body markup of Default.aspx (including the user control with its button itself) resides in <form runat="server"> as required by ASP.NET. Everything works so far.
However, the problem is when I put some validation controls inside Default.aspx (meaning inside <form runat="server"> because otherwise the page will report server errors). When validation controls are added, the delete button in the user control stops working. Clicking on this button no longer triggers the event as before.
Now, I disabled event validation in Default.aspx using EnableEventValidation="false" directive. I am also including UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None; in the code behind file. However, none of this helps.
How do I fix this problem and make the button clickable?
Update:
I know for sure that the validation controls are causing the problem, because I only need to add EnableClientScript="False" to each of them, and the button becomes clickable. But I don't want to rewrite validation on the client side manually!
It turns out the PostBack cannot occur if the form is not IsValid regardless of what element causes it. As long as that element (button) is inside the form with runat="server"that is invalid, the posback will not happen.
A very simple workaround is to just make the Button in my user control bypass validation: CausesValidation="False" (thanks to this question).
Another solution and maybe a more efficient one, is to use ValidationGroup. This way, all TextBoxes together with the Submit Button will belong to one group, and those controls that do not belong to that group will NOT be validated. In fact, they might have their own ValidationGroup; this will avoid interference between different controls within one Web Form.
I have a radiobutton list and three placeholders in my page, out of which the radiobutton list,first and third placeholder are within updatepanel, second placeholder is not within updatepanel.
When radiobutton list selectionindex is changed, I want all three placeholders invisible. Placeholder2.visible=false code executes but still Placeholder2 is visible.
How to resolve this.
Thanks,
Viknesh.A
You should put all your placeholders in update panel on reload the page when the radio button hits (full post back) by setting AutoPostBack="true"
You should understand that by default changin radio button on a client only affects client html, so you need to pass that info to server.
Another option is to have client onclick for radiobutton and write your custom javascript function to hide your second placeholder, but don't forget to manage that situation on the server as well, when postback (either ajax or not) will occur.
Move Placeholder2 inside the UpdatePanel.
Or don't use an UpdatePanel at all.
Or use JavaScript to hide it, instead of server-side code.
I'm a rookie in .net. I'm using an AjaxToolKit Accordion Control and when I put a button in, the "onclick" event is not raising. When I use a dropDownList, if I select "autoPostBack", the event raises normaly (instead, nothing occours). But with buttons I cannot define the "autoPostBack" (its implicit?). It's bringing me several troubles.
Thank you if you can help.
I've discovered the problem (moreover two problems): I'm using AjaxControlToolKit MaskEditExtenders and MaskEditValidators, and a PopUpControlExtender. When the form isn't fulfilled correctly, the MaskEditExtenders/Validators somehow disables form submiting. Also, TargetControlID property of popupControlExtender was set to the button in question. In this case, the event isn't raised.
I noticed that the
asp:Button
for example will work without javascript enabled in the browser. However any other control with an "OnClick" or "OnServerClick" event must use the javascript_doPostback.
Are there any controls besides the Button that don't need to use javascript?
I want to know because I want to be able to style the Control however I want without it looking like a button, and I want it to still work without the user having javascript enabled.
Any suggestions?
I think the answer is none. The button submits the form (as buttons will do) but no other controls auto-submit a form without a client side event handler.
Note that this also applies to the Link button.
_doPostback is the heart of ASP.NET. If you don't want to require the use of Javascript then ASP.NET isn't the language for you.