|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Generated JavaScript vs. Manually Created JavaScript: Which one comes first?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. 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) Nathan Sokalski wrote: Show quote > 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. 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 Show quote On Nov 6, 9:57 pm, "Nathan Sokalski" <njsokal***@hotmail.com> wrote: > 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. > -- > Nathan Sokalski > njsokal...@hotmail.comhttp://www.nathansokalski.com/ 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 Show quote On Nov 7, 8:57 am, "Nathan Sokalski" <njsokal***@hotmail.com> wrote: > 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. > -- > Nathan Sokalski > njsokal...@hotmail.comhttp://www.nathansokalski.com/ 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 On Nov 7, 6:58 am, Nick Chan <zzzxtr***@yahoo.com> wrote: Show quote > 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 > > On Nov 7, 8:57 am, "Nathan Sokalski" <njsokal***@hotmail.com> wrote: > > > > > 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. > > -- > > Nathan Sokalski > > njsokal...@hotmail.comhttp://www.nathansokalski.com/- Hide quoted text - > > - Show quoted text - |
|||||||||||||||||||||||