Dynamic Javascript in ASP button

W

Wayne Sepega

I have inherited from System.Web.UI.WebControls and overrode the OnRender of
my new button. In the on render I generate some JavaScript and add it as an
attribute as follows:

this.Attributes.Add("onclick", javascript);

I have the following issues:

1) I have to add to the javascript the following:
this.Page.ClientScript.GetPostBackEventReference(this, null);
When added, this causes the post back to fire even if the client
validation fails. Also the javascript gets both the page reference I added
as well as the PostBackWithOptions.

2) Don't add in GetPostBackEventRefernce
A page with no validation doesn't have the call to the post back,
but if there is client validation then the post back with options gets
generated.


The above issues occur even if I add the javascript to the OnClientClick
event.

I'm fairly new to writing ASP.net controls, so if I am putting/generating
the javascript in the wrong place or missing something obvious please let me
know.

Thanks
Wayne
 
B

bruce barker \(sqlwork.com\)

you need to understand submit button and client script. normally, unlike
other asp.net controls, buttons postback with the need of client script as
the browser handles it. if you use validators, the the browsers default
behavior is overridden by attaching client script to the button. this client
script cancels the automatic postback, and calls client script to validate
and conditionally postback. if you add you own client script it must be
compatiable with .nets onclick code. view the souce and see the generated
code and adjust your client code.

-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top