Link to home
Start Free TrialLog in
Avatar of tonelm54
tonelm54

asked on

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?????????
ASKER CERTIFIED SOLUTION
Avatar of JosephEricDavis
JosephEricDavis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tonelm54
tonelm54

ASKER

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