Disable/Enable a button inside a panel on an ascx control in a vb.net website from codebehind not JQuery - asp.net

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.

Related

How to prevent validation for an event handler of a user control?

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.

Telerik RadGrid - EditForm events when RadGrid is inside Repeater

I have a Telerik RadGrid which uses the EditFormSettings like this:
<EditFormSettings EditFormType="WebUserControl" UserControlName="~/UserControls/MyUserControl.ascx" >
<EditColumn ButtonType="ImageButton" />
</EditFormSettings>
Within MyUserControl.ascx, I have a button which I'd like to trigger an event handler when clicked. The two ways I know of doing this are as follows:
OnClick="btnOkay_Click" as a button attribute, where the event handler is in the code behind
CommandName="Update" as a button attribute, where this is handled by the event handler passed into the OnUpdateCommand attribute of the RadGrid
Both of these work. However my problem is that they stop working when the RadGrid is within a user control within a Repeater ASP.NET control. In this case, the button event handlers do not get fired (in either of the methods above).
I notice that when I bind my repeater:
rpt.DataSource = Data
rpt.DataBind()
If I wrap that in a if(!Page.IsPostBack), then the edit form doesn't appear at all when I click on the RadGrid row's edit link. So I had to remove that IsPostBack check. I don't think think this is the cause of my problem, as I've tried explicitly not doing this when clicking my update button (by breaking into the debugger). I just thought I'd mention it incase it was related.
Can anyone think of a reason why this would break in a Repeater?
Found the problem with a little help from a work colleague! Turned out to be because I was binding the Repeater in Page_Load rather than Page_Init. Changing it to Page_Init resolved the issue.

Clicking on linkbutton, don't want validators to fire

I have a profile page with a bunch of textboxes and validators. The validation works fine right now. My issue is that I have added a couple of link buttons that go to different pages. When I click on a linkbutton the validators fire, which I don't want. I just want to go to the next page.
I tried disbling the validators in the linkbutton click event but it didn't work. How do I stop the validators firing?
Thanks
Simply set the CausesValidation atrribute to false on the linkbuttons in the markup:
CausesValidation="false"
LinkButton.CausesValidation Property

Problem with Ajax control toolikt Modal Pop control

I have an ajax control toolkit modal popup on my page and in that modal popup i have a gridview on which user select some item through checkbox on each row of gridview. Whenever user check or uncheck on checkbox my modal popup automatically hide. I have set autopost property of checkbox set to true becuase im perporfing some calculation on each checkchanged event. what may be the problem
Your page is posting back because of the autopostback="true" on the checkbox, thus hiding the modal popup.
Look up 'ASP.Net Page Lifecycle' for further understandinf. It is important to know how this works.
I'm sure you'd also like to know how to solve this.
You could:
Set AutoWireUp=false on the page, but then you'd have to wire all events on the page manually. Since you aren't familiar with the Page Lifecycle, I'm not sure how successful you'd be.
Use a javascript-only modal popup.
Perhaps an UpdatePanel can be used.

ASP.NET Button vs Linkbutton Enabled="false" behavior

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!

Resources