Getting server side javascript over client side
I have created c开发者_JAVA百科ustom control using asp.net 2.0. The control contains a textbox txtDate. I have also created a javascript file DateMask.js which contains a function maskDate(). I have attached the maskDate() with textbox using -
txtDate.Attributes.Add("onkeypress","maskDate()");
I have also registered the script using ClientScript.RegisterStartupScript.
When I execute the aspx page containing my custom control it is generating script error showing that maskDate() is undefined.
Could anybody tell me what exactly the problem is?
Thanks for your cooperation.
One way to do it would be to place a literal control above your textbox, and assign the script to it in the code behind:
literal1.Text = "<script>function maskDate() {...}</script>";
The benefit to this, is you would not need to have to reference the script file with some tricky relative paths depending on where your usercontrol is used.
Make sure you didn't forget <form runat="server" ID="Form1"></form>
at the end of the <head>
tag!
As you can read in Using JavaScript Along with ASP.NET 2.0 under "The Difference Between Page.ClientScript.RegisterStartupScript and Page.ClientScript.RegisterClientScriptBlock" they rely on the location of the form tag.
We have shown two different methods for placing JavaScript functions on an ASP.NET page—so what is the difference? The main difference is that the RegisterStartupScript method places the JavaScript at the bottom of the ASP.NET page right before the closing element. The RegisterClientScriptBlock method places the JavaScript directly after the opening element in the page. So what difference does this make? It can make quite a bit of difference, as we will see.
精彩评论