?
Solved

Web User Control Call function only once per page

Posted on 2006-07-17
12
Medium Priority
?
421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

777 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