Solved

Web User Control Call function only once per page

Posted on 2006-07-17
12
414 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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
 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

810 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