Generated JavaScript vs. Manually Created JavaScript: Which one comes first?

  • Thread starter Nathan Sokalski
  • Start date
N

Nathan Sokalski

There are many cases in which I want to use the same event for manually
added JavaScript as one that is used for generated JavaScript. For example,
when I set the AutoPostBack property of a TextBox to True, the JavaScript
onchange event is used. However, I may want to execute a piece of JavaScript
before the postback, such as setting the value to 0 if the user changes the
value to "" (Otherwise, I would recieve an error when trying to use a
function such as CInt()). Situations like this also occur quite often when
using Validators with Display="Dynamic". Is there any way to control what
order the JavaScript is added to the event? Does anybody have any
suggestions on a workaround (that doesn't require checking the value in
every place in my code that I use the value)? Thanks.
 
B

bruce barker

the system generated javascript calls are added when the control is
rendered. they are appended to any code specified in the attribute.

the real issue is most of the controls are not client script friendly.
i.e the textbox does not support a onclientchange, or onclientblur, you
have to set the attributes and hope the control passes it on. you also
need to reverse engineer (usually via view source) which events are used.

-- bruce (sqlwork.com)
 
P

PJ on Development

Hi,

yes, you can add any client script event by adding it as an attribute
of any control and it will be rendered properly by ASP.NET.

When ASP.NET is rendering the control it writes any code defined
attribute first, appending any automatically generated code
afterwards.

The code defined attribute is copyed verbatim to the browser. You can
even prevent the automatic code to run adding a cold "return;" at the
end of your event scriptlet.

However, the best way to validate the user entry is at the
Page_Validate event handler, where you can verify every entry of the
user (if you are using AJAX.NET you need to rethink this approach).

At this event you can set the text (or value property) of every user
input and then do not worry about it anywhere in your page.

Just make sure to set the CausesValidation property of the button that
will do some work.

Regards,

Paulo Santos
http://pjondevelopment.50webs.com
 
N

Nick Chan

hi i don't know if this works in PageLoad, but this works in
LoadComplete


Private Sub haha(ByVal o As Object, ByVal e As EventArgs) Handles
Me.LoadComplete
If Not IsPostBack Then
ttt.Attributes("onchange") = "if(this.value=='')this.value=0;" &
ttt.Attributes("onchange")
End If
End Sub
 
P

PJ on Development

Nick,

Yes, it would work on Page_Load, or anywhere else in the code, prior
the call to the Render event.

The rendering of a page occurs at the very last moment in the life of
a page, just prior to GC.

Regards,

Paulo Santos
http://pjondevelopment.50webs.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

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top