• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 487
  • Last Modified:

Editing Javascript in code

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
tonelm54
Asked:
tonelm54
  • 2
1 Solution
 
JosephEricDavisCommented:
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
 
tonelm54Author Commented:
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
 
JosephEricDavisCommented:
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

Independent Software Vendors: 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!

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