Solved

Editing Javascript in code

Posted on 2010-11-23
3
452 Views
Last Modified: 2012-05-10
Good afternoon,
My ASP.net application writes some javascript depending on several options selected on a page, and then attaches it to a button, so onClick it fires the javascript.

The issue I have is its a pain clicking the 'Compile' button to attach the Javascript to my button, then having to click the button.

I have through about setting a javascript method to press the button for me 1 second after the page completes load, but it causes 2 problems, 1 I cant seem to guarantee what the button is called during run-time, and the second is the click just causes the page to post back again, and its stuck in a loop.

What I thought about doing is having a javascript segment on the page:-
         <script type="test/javascript" id="javaRunMe" runat="server">
         </script>

And then adding the javascript inside my code:-
         javaRunMe.innerTest = myJavaCode
But this didnt work either, just gave me an error about 'Object reference not set to an instance of an object'

I did also think about writing a Javascript method to see if Button2 had any attributes set, and if it did run them, but again I ran into the problems of I cant garantee what the buttons name is, and I couldnt find any way to test Button2 attributes.

Any ideas?????????
0
Comment
Question by:tonelm54
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
JosephEricDavis earned 500 total points
ID: 34197492
So first of all, where is your custom javascript being created?  I'm guessing it is being created on the server right?  And then being placed onto the page using code like this below?

ClientScript.RegisterStartupScript(this.GetType(), "JSScript", sb.ToString());

Or something similar?

If that is the case than you should just be able to put the jquery document completed function on the page and attach a click event with to the button your talking about so it calls your custom function...

So on the server...

StringBuilder sb = new StringBuilder();
sb.append("function yourCustomFunction() {");
sb.append("//whatever code you decided to add in here.");
sb.append("}");
ClientScript.RegisterStartupScript(this.GetType(), "CustomScript", sb.ToString());

Then on your client you can do something like this...

$(function() {
     $('#yourbutton').click(function() {
          yourCustomFunction();
     });
});

Where the html button in your page has its id set to "yourbutton"

Let me know if this is anywhere in the direction you're heading on this, or if you need any more help trying to piece together what I'm showing you here.
0
 

Author Comment

by:tonelm54
ID: 34203830
Well, actually JosephEricDavis, I havnt tried your way, but seems to make sence.

Ive tried adapting your code to suit my needs:-
       Dim sb As New StringBuilder
        sb.Append(loadcmd)

        ClientScript.RegisterStartupScript(Me.GetType, "JSScript", sb.ToString)
The idea is to just run the code once loaded, I would prefer not to have the user click a button.

But when it compiles it puts my code outside the script tags:-
<script type="text/javascript">
//<![CDATA[
(function() {var fn = function() {$get("MainContent_ToolkitScriptManager1_HiddenField").value = '';Sys.Application.remove_init(fn);};Sys.Application.add_init(fn);})();//]]>
</script>
window.open("viewChargeInvoice.aspx?startDate=2010-11-02&endDate=2010-11-15&Contract=Administration");window.open("viewChargeInvoice.aspx?startDate=2010-11-02&endDate=2010-11-15&Contract=Anti-Virus");window.open("viewChargeInvoice.aspx?startDate=2010-11-02&endDate=2010-11-15&Contract=Audits");<script type='text/javascript'>new Sys.WebForms.Menu({ element: 'NavigationMenu', disappearAfter: 500, orientation: 'horizontal', tabIndex: 0, disabled: false });</script>
<script type="text/javascript">
//<![CDATA[
Sys.Application.add_init(function() {
    $create(Sys.Extended.UI.CalendarBehavior, {"format":"yyyy-MM-dd","id":"MainContent_TextBox1_CalendarExtender"}, null, null, $get("MainContent_TextBox1"));
});
Sys.Application.add_init(function() {
    $create(Sys.Extended.UI.CalendarBehavior, {"format":"yyyy-MM-dd","id":"MainContent_TextBox2_CalendarExtender"}, null, null, $get("MainContent_TextBox2"));
});
//]]>
</script>


BTW- My code is the:-
window.open("viewChargeInvoice.aspx?startDate=2010-11-02&endDate=2010-11-15&Contract=Administration");window.open("viewChargeInvoice.aspx?startDate=2010-11-02&endDate=2010-11-15&Contract=Anti-Virus");window.open("viewChargeInvoice.aspx?startDate=2010-11-02&endDate=2010-11-15&Contract=Audits");

Any ideas what Im doing wrong?

Thank you
0
 
LVL 7

Expert Comment

by:JosephEricDavis
ID: 34206151
So I need more detail on what your currently doing and what you're trying to do.  

Are you dynamically creating the javascript on the server or is it being done on the client?

You mentioned that previously the user was having to click a button twice?  Now are you saying that you don't want the user to have to click a button at all and you would just like to have your code run as soon as the page loads?  Give me more of a description of what you need to have happen.

Tell me anything else you think might be helpful for me to understand your situation.  The more detail you provide me the easier it will be for me to provide a solution for you.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

708 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

15 Experts available now in Live!

Get 1:1 Help Now