Solved

Web User Control Call function only once per page

Posted on 2006-07-17
12
412 Views
Last Modified: 2008-02-01
I have a Web User Control which is a JavaScript calendar.

I want to have the user control print the javascript includes file only once on the page even if I have 20 calendars on the page.

do you know how this is done?
0
Comment
Question by:icredes
  • 6
  • 6
12 Comments
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 17122745
In your ASPX page:

<script language=javscript src="myjsinclude.js">

-Nauman.
0
 
LVL 1

Author Comment

by:icredes
ID: 17122838
thank you...

What I am looking for is something similar to

sub process only once

literal1.text = "<script language=javscript src=""myjsinclude.js"">";

end sub

I don't want it to show up several times on the page

and

I don't want it to show up if the Calender object is not on the page.
0
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 17123044
If it is just an include file, then I recommend you follow the classic src approach. Otherwise, you can use the following:

if your_page_has_calendar_control then
 if literal1.text = "" Then
    literal1.text = "<script language=javscript src=""myjsinclude.js"">";
 end if
end if

-Nauman.
0
 
LVL 1

Author Comment

by:icredes
ID: 17123135
is there any way to include it within the object so that all code is self contained within the object.

I could place all kinds of stuff in the the main page, but I don't want to update it on every page that has a calendar.

do you understand what I am requesting?
0
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 17123550
If you are using ASP.NET 2.0 and MasterPage, you can include it in MasterPage.

If you are using ASP.NET, then the best and easiest approach is javascript src attribute.

What ASP.NET version are you using?

-Nauman.
0
 
LVL 1

Author Comment

by:icredes
ID: 17124145
ugh...

I don't think you are understanding what I am asking....

I want 1 - one - self contained object that has a function or sub or something WITHIN the object to run only once per page (and not be on the page, but be in the object)  we are talking about Web User Controls

Keep everything in the ascx file.

oh... and ... I am not using ASP 2.0

there is a php equivalent called include_once which will include a file only once during the whole file's operation.

do you understand what I am requesting?

remeber... nothing in the aspx file or the template but all SELF CONTAINED in one object/ascx file

ugh...
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 17124578
If you want to use ASCX object, then you need to reference that ASCX page in your ASPX page wherever your page reference the calendar control. Without referecing the ASCX page, you will not be able to make it work. Will it be possible for you to add the reference?

Second option include the reference to the JS file once per session in Global.asax:

 Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session is started
        Response.Write("<script language=javascript src=inc/myjs.js></script>")
 End Sub


Another complex option would be to write the HTTP handler that will check if a Page contain the Calendar object and if so include the reference to the JS file or User control.

--Nauman.
0
 
LVL 1

Author Comment

by:icredes
ID: 17124762
not getting it...

I know I have to refer in the aspx file to add the ascx file.

I guess all the experts are gone...and we are left with bumbling idiots trying to get the question.
0
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 17125126
icredes,

First of all your question did not contain enough information. What I have posted is according to the information you have provided. Therefore, I will be thankful if you explain your question in future either in a more effective manner or do not call experts "idiots" since they are the ones taking their valuable time trying to help people in need!

>>I want 1 - one - self contained object that has a function or sub or something WITHIN the object to run only once per page (and not be on the page, but be in the object)

[I bet even you are not able to understand what you are trying to say here. ]

Now back to your question. You have made it enough complex by saying "I want to have the user control print the javascript includes file only once on the page even if I have 20 calendars on the page." Reading that one can easily assume what you are taking about is a JS file. Then you posted you wanted something like literal1.text = "<script language=javscript src=""myjsinclude.js"">"; and I answered you according to that. People participating in your question give you hints most of the time. You should try to think and make your application work using the mentioned options.

How does your caneldar ASCX look like? Is it something like this:

<%@Control ......%>
<script language="javascript" src="JS_INCLUDE.js"></script>
.
.
.
.
.
More tags
.
.
.
.
.

Am I right when I say you want to include the A_JS_INCLUDE.js file once in your page where the calendar is declared and if you refer to this user control in ASPX page more than one time, you want to include JS_INCLUDE.js only once? If it is so, then the best option is to take the src statement out of ASCX and add it to your application in Global.asax Session_Start event. Since this is the include file, your application will not be affected if the page do not have the calendar control and where a calendar control is available on a page it will use the referenced JS file. Make sense?

-Nauman.
0
 
LVL 1

Author Comment

by:icredes
ID: 17125166
Questions can always be asked to clarify questions..

Let us try to break it down in simpler terms

can you create a block of code within an ascx object that resides only within the ascx object or its class that will run only once during the page without calling code outside of the object or calling properties.

Is .net intuitive enough to does this task

YES or NO?
0
 
LVL 25

Accepted Solution

by:
nauman_ahmed earned 125 total points
ID: 17125378
If its a javascript segment then you can call Page.IsClientScriptBlockRegistered() in the Page_Load event  to determine if a specific JS block is registerd or not and then register it.

 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If (Not Page.IsClientScriptBlockRegistered("clientScript")) Then
            Page.RegisterClientScriptBlock("calendar", "<script src=""include.js""></script>")
        End If

    End Sub

Are you trying to implement something like this?

ASP.NET Pop-Up Date Picker Control
http://www.dotpitchstudios.com/mainsite/default.aspx?pageid=7

-Nauman.
0
 
LVL 1

Author Comment

by:icredes
ID: 17129485
that was great and exaclty what I wanted for my problem.

it is a shame it was such a headache getting there
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now