Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 429
  • Last Modified:

Web User Control Call function only once per page

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
icredes
Asked:
icredes
  • 6
  • 6
1 Solution
 
nauman_ahmedCommented:
In your ASPX page:

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

-Nauman.
0
 
icredesAuthor Commented:
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
 
nauman_ahmedCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
icredesAuthor Commented:
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
 
nauman_ahmedCommented:
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
 
icredesAuthor Commented:
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
 
nauman_ahmedCommented:
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
 
icredesAuthor Commented:
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
 
nauman_ahmedCommented:
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
 
icredesAuthor Commented:
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
 
nauman_ahmedCommented:
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
 
icredesAuthor Commented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now