Hi

I would like to run a javascript function (to confirm deletion) from a web control (asp:button) before posting back to the same page.

I have read about how to do this in several places (mybutton.attributes.add... )

However, the fact that I have asp.net validation controls on my page means that I automatically end up with two onclick attributes in my HTML and the javascript function is not called.

Has anybody encountered (and hopefully solved) this?

Thanks in advance

Duncan

Dani AI

Generated

Brief diagnosis: the ASP.NET validator framework injects its own client-side script into a button's onclick. Adding a second onclick via Attributes.Add produces two onclick attributes in the rendered HTML; browsers only honor one, so the custom confirm never runs. The practical fixes are either to stop ASP.NET from injecting validation on that button or to provide a single client-side handler that runs validation and then the confirmation.

A few workable approaches (with trade-offs):

  • If the delete action does not require validation, prevent the validator script from being added by setting CausesValidation="false" on the delete button (as noted). This is the simplest, lowest-risk option.

  • If validation must run first, combine validation and confirmation into one client script. On ASP.NET 2.0+ the easiest is OnClientClick that calls Page_ClientValidate() and then shows confirm. Example markup:

<asp:Button ID="btnDelete" runat="server" Text="Delete"
  OnClientClick="if (typeof(Page_ClientValidate) == 'function') { if (!Page_ClientValidate()) return false; } return confirm('Are you sure you want to delete this item?');"
  OnClick="btnDelete_Click" />

For older setups, set the same string into Attributes["onclick"] from code-behind so only one onclick attribute is emitted.

  • If combining scripts on the button is awkward, attach confirmation on form submit only when the delete button was clicked. Pattern: set a small JS flag in the delete button onclick, then in form.onsubmit show confirm when the flag is set. This preserves whatever validator wiring ASP.NET uses and keeps confirm last in the chain.

Troubleshooting notes for the calendar/'length is null' problem mentioned by : enable browser dev tools to capture the exact file/line of the error, temporarily toggle validator client-side scripting (diagnostic only) to see if validator JS collides with the calendar, check script load order and duplicate IDs or global function names, and always enforce server-side validation (Page.IsValid) so client-side tweaks do not bypass safety (as emphasized).

Recommended Answers

All 8 Replies

Hi

I would like to run a javascript function (to confirm deletion) from a web control (asp:button) before posting back to the same page.

I have read about how to do this in several places (mybutton.attributes.add... )

However, the fact that I have asp.net validation controls on my page means that I automatically end up with two onclick attributes in my HTML and the javascript function is not called.

Has anybody encountered (and hopefully solved) this?

Thanks in advance

Duncan

Hi
use custom validator for the purpose.

pls get back to me if its not solved.

thnks n regards
siddartha

siddartha_pal, I think you misunderstand the issue.

dslander, what type of validation controls are you using? Can you just incorporate your onclick logic to the javacript for the validation?

I am using both required field validators and regular expression validators.

I could probably work around the issue by using custom validators but I would rather do it with the controls I have if at all possible (it's easier & tidier).

If I resort to using custom validators then I'd be as well coding the whole thing (validation & confirmation) in the javascript onclick event. I could do this but would rather use the more concise & specific .net controls if I can.

I am using both required field validators and regular expression validators.

I could probably work around the issue by using custom validators but I would rather do it with the controls I have if at all possible (it's easier & tidier).

If I resort to using custom validators then I'd be as well coding the whole thing (validation & confirmation) in the javascript onclick event. I could do this but would rather use the more concise & specific .net controls if I can.

hi there
I am not getting how u have two onclick attributes? and why u need two onclick there.
I think when your javascript function will return true only then server side event handler should execute. Seems I am anot able to get u properly.

thnks :)

I have the same probleb with javasecript code using custom validator
i tried to make the return to true but it failed too
so anyone can give me a hand on this problem or send me a Sample

I came across this problem too.

This simple solution is probably enough:
- disable validation on your delete button (causesvalidation="false")
- add the 'onclick' event as normal using the Attributes property in your code behind file.

Setting causesvalidation to false means .net won't put in it's own 'onclick' method.

For my application, the delete doesn't require any validation (it just deletes the current item), and I think this will apply in the majority of situations. Hopefully those where some form of validation is required, maybe it's simple enough to write custom jscript.

I think it's a bit of a short-fall in the current webforms implementation. :|

Troy.

Same problem happened to me and I've found a solution..

Now HERE IS MY SOLUTION;
1) Change EnableClientScript settings to FALSE for each validation rule. (Custom or Regular doesn't matter)
This will post the page back and validate fields at server level.

2) Place your code which you would like to process, if all fields are valid to between followin if statement;
if Page.IsValid then
'Your code
end if

And this worked fine for me, I am positive it is going to work for you too,..

Thanks,
Mert..

Same problem happened to me and I've found a solution..

Now HERE IS MY SOLUTION;
1) Change EnableClientScript settings to FALSE for each validation rule. (Custom or Regular doesn't matter)
This will post the page back and validate fields at server level.

2) Place your code which you would like to process, if all fields are valid to between followin if statement;
if Page.IsValid then
'Your code
end if

And this worked fine for me, I am positive it is going to work for you too,..

Thanks,
Mert..

I am having that same problem too regarding with the .net validator and javascript ...

after i have try your suggestions it didn't work fine for me....

cause im using a java-based calendar and a required field validator
although the calendar is working but whenever it clicks.. for example clicking the year (or month) it skips by 3 , by 4 , by 5 and so on....

and whenever i checked the error.. this is what i've got

----length is 'null' or not an object

how come that is null? and why it is null?

can anyone knows why are those two conflict?

???
:-/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.