I have created custom 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.
Related
I have an iframe that has a dynamic URL based on the results of web API call. What is the best way of setting its src in the aspx page? Here is an example of the iframe:
<iframe id="dynamicframe" runat="server"></iframe>
Should I set it directly in the codebehind like so:
dynamicframe.Attributes["src"] = "http://dynamicurl.com";
Or should I create a property in the codebehind and reference it in the iframe:
<iframe id="dynamicframe" src="<%= dynamicFrameUrl %>"></iframe>
Or some other method altogether?
This is a general question that can stands the same for any html tag.
The alternative third option is to use a literal control and fully render the iframe on code behind as:
txtLiteral.Text = "string.Format(
"<iframe id=\"dynamicfrmae\" src=\"{0}\"></iframe>", PageUrlForSrc );
The different for all methods :
Direct write on page <%= %>
Not work with update panel
Its run the moment the page send to the browser (and not before on the page steps)
Not accessible as control
This is the method that I avoid most. I use it only when I like to left some calculations for later and avoid page cycle, or when I have responce.flush() just before it.
Write it using to literal
Compatible with UpdatePanel
Not accessible as control
Write it as attribute on code behind
Make the control pass the steps of the html cycle
Is accessible else where on the page as variable
The id of this control may change but you can avoid conflicts
All methods have their purpose, and I used then according what they fit best.
Well if you have the URL available to you in the code-behind file I would say option one is the most straight forward. However, I'm a little confused about the question, are you looking for a best-practice?
Hi
I am just wondering why some people suggest RegisterStartupScript() to call client side js while some suggests RegisterClientScriptBlock().
Please make me clear whats the difference between the two as they doing the same operations, for using js statement calls and which one is preferable if I only use js statements like alert, return confirm from codebehind.
The RegisterClientScriptBlock method inserts the client-side script immediately below the opening tag of the Page object's <form runat="server"> element. The code cannot access any of the form's elements because, at that time, the elements haven't been instantiated yet.
The RegisterStartupScript method inserts the specified client-side script just before the closing tag of the Page object's <form runat="server"> element. The code can access any of the form's elements because, at that time, the elements have been instantiated. The choice of which method to use really depends on the "order" in which you want your script to be run by the browser when rendering the page.
Normally you would use RegisterClientScriptBlock if you want to register js-functions and use RegisterStartupScript if you want to call these functions or want to access controls on your page.
For further informations have a look at MSDN.
I am utilizing controls in my asp.net application. I have a register tag the source of which needs to be dynamic. I am using the line below which functions correctly when the full path is specified but when I change it to the variable I get a parser error. Any idea how I can go about doing this?
Thanks
It might be better to use Load Control from the code behind of the aspx page.
If I'm understanding you correctly — you can't use a variable in those directives (Page, Register, etc). They have to be constant expressions.
However, it is possible to dynamically load ASCX controls. You would have to do this in code, though, and it would not involve the Register tag.
I think I summed up the question in the title. Here is some further elaboration...
I have a web user control that is used in multiple places, sometimes more than once on a given page.
The web user control has a specific set of JavaScript functions (mostly jQuery code) that are containted within *.js files and automatically inserted into page headers.
However, when I want to use the control more than once on a page, the *.js files are included 'n' number of times and, rightly so, the browser gets confused as to which control it's meant to be executing which function on.
What do I need to do in order to resolve this problem? I've been staring at this all day and I'm at a loss.
All comments greatly appreciated.
Jason
If the issue is simply that the same file is being embedded multiple times and causing conflict, look into using RegisterClientScriptInclude. If you use the same identifier for all of your calls to RegisterClientScriptInclude only one instance of that file will be embedded:
http://msdn.microsoft.com/en-us/library/2552td66.aspx
However, if the issue is that your methods are being called but they don't know what controls on the page to operate on then you need to figure out how to provide your JS some context. Define a JavaScript object that represents your control on the client-side, and on the server-side emit the call that will instantiate it with the client IDs of the controls you'll be operating on.
We are using CustomValidator to validate User Control. The control works fine until you drop two instances of the Control on the same page, since they reference the exact same JavaScript functions, only one control works. Work around, we appended JavaScript function name with control id.
Validate_SAPDepartment<% =ControlId %>(oSrc, args) {...}
In codebehind, we assinged ClientValidationFunction
CustomValidator1.ClientValidationFunction = "Validate_SAPDepartment" + this.ControlId
This may not be the right approach but it works.
I've had this situation before. You register a separate JavaScript file with the page using the ScriptManager. You can stream this as a resource file embedded into the dll if you wish.
Then you only call into the functions from your control.
Otherwise a completely separate jquery file may also work.
Is it possible without encapsulating these controls in aspx files? The Register tag makes me think that it is not.
You can't call ascx page directly, they can't serve any html without added to aspx or render then dynamically in response to any page/handler.
One possible way would be to have a generic aspx stub page that acts as a host to the ascx in the normal web forms way make a callback to the that page using JQuery's $.ajax() method with some arguments to denote what ascx control you want to load.
Then on your stub / host page, override the render method and render the control directly to the output stream using response.output.write. The callback handler on the client will catch the output and then it can be inserted into the DOM in the normal way or using using the version of $.load() that allows the specification of a DOM element.
hope this helps.
This is one way to load ASCX controls through jQuery, using an ASMX service that creates a Page instance to render the user control.
Since I've started using jQuery/Ajax more and more, I find I've been using server controls less and less.
Things that I used to put into controls are now just individual aspx pages which get loaded into panels.
If you have old ASCX files, it shouldn't be too hard to convert them to ASPX
No.
That said, you could place your ascx inside an UpdatePanel and set the panel's content visible/invisible using the standard Asp.Net postback mechanisms.
Or you could write a separate IHttpHandler which generates HTML code that your Javascript code adds to the page using DOM. This part of the page would however not be accessible in PostBack.
No, ASCX are user-controls, that exist on ASPX. You will need to make it a Page, or convert into Handler.