We help IT Professionals succeed at work.

1.1: VB.NET, getting nut over RegisterStartupScript()

Kelmen asked
Medium Priority
Last Modified: 2012-06-27
holy cow,

  I coding some adhoc stuff, trying to make a page to submit it-self using client script and RegisterStartupScript().
But the heck it give me REALLY WEIRED result!!!!

Me.RegisterStartupScript("test01", "<script lang='javascript'>document.forms[0].submit();</script>")

As simple as this, but the script never get rendered in the html!!! And so not working.

But if I change it to <script lang='javascript'>alert(document.forms[0]);</script>


is it somekind of ASP.NET security or something I need to shit-out about?

Watch Question

The script is working, but the problem is that javascript code is cleared on next Page_Load event. As you mentioned, second script is working(you can see the javascript code in the page, but if you combine it with first script then the result is going to be the same) so there is no reason for first script not to work.


when I stated I changed the script to "alert", I'm chaning the same line of code.

the 1st code doing submit, never get rendered.
the 2nd code doing alert, it worked!!!!

Noted again: the changes is on same line, same pieces of code.... I'm doing that just testing, thinking maybe me typo javascript syntax and ASP.NET so -smart- filtered it out or something.

Thx for your comment, but its doesnt stand in my problem.


I wonder maybe someone else like you can get the submit script working on your page?

maybe no ppl try it before, or my IDE or ASP.NET got a ghost inside......
"I wonder maybe someone else like you can get the submit script working on your page?"

try this and then you tell me if it works or not:
Me.RegisterStartupScript("test01", "<script lang='javascript'>document.forms[0].submit();alert(document.forms[0]);</script>")

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
>>Me.RegisterStartupScript("test01", "<script lang='javascript'>document.forms[0].submit();</script>")
where do u have this line? in the page load .. if not, then what i assume is that the script is getting rendered and ur page actually postback .. and the page is shown again .. this time .. the code is not rendered as probably u have this code within a check for "IsPostback" in page_load or in some other event .. the alert is working as the page never gets postback the first time ..

what is that you would like to do .. when do u want the postback to happen?


if you add
            Me.RegisterStartupScript("test01", "<script lang='javascript'>document.forms[0].submit();</script>")
in your page_load, it shall work.

Btw, you want the form to postback to the same page or post to another aspx page?


I'm doing this for adhoc purpose only. To ease my testing. after the page loaded, I want the client script do an auto-postback. nothing fancy, and I know I'm doing.

Think you guys not getting me clear.

the alert and submit coding I tried is on same line of the statement.
Initially submit give nothing, so I chg the javascript-inside to alert something for some reassurance, and the hell it WORKED. but change back to submit still nothing.

in line **** 10 ****:
Me.RegisterStartupScript("test01", "<script lang='javascript'>document.forms[0].submit();</script>")

- not work
- so I changed to:
in line *** 10 ***:
Me.RegisterStartupScript("test01", "<script lang='javascript'>alert(document.forms[0]);</script>")

- and it WORKED, the result is a popup with [object] msg as expected.
- Noting AGAIN, its same piece of coding line.....
- so whether its's in PageLoad or not doesn't matter, because the alert worked, but why not the submit?

nvm, its not really needed for my work.

But I found this strange and I wanna know what's wrong, so the question remain until one can tell the truth or at least give my troubleshootings tips to nail this -ghost- .... whatever....

crazy dotNet.....
>>I want the client script do an auto-postback
as i said that is what is happening .. the line to submit is actually submitting the page .. cld u pls post ur code where u have the registerstartupscript ..

Think you also not getting me clear. It makes a lot of difference coz there are so many page events and each has its own purpose. Different codes place in different event will yield different results.

BTW, I placed your code in page_load and both javascripts worked. I tested it by showing date time in textbox1 on postback

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Page.IsPostBack Then
             Me.RegisterStartupScript("test01", "<script lang='javascript'>document.forms[0].submit();</script>")
            TextBox1.Text = DateTime.Now.ToString
        End If
    End Sub
just curious, any particular reason for a grade C?


Ok, ok, I got it figured out.... silly me.... crazy Kelmen..... kekkekee

as Rejojohny stated, the submit script did rendered on initial loading, and it postbacked (so fast because me testing locally, can't tell the difference), so and 2nd rendering its rendering the script again due to IsPostBack logic chk and code branching off.

akiku code suggestion + johny description sparked an idea to me, so I swapped the script as akiku suggested:


and it DID popup the submit message.

So, its now clear to me that:
1. the submit script did rendered on 1st loaded.
2. it auto-submitted but I can't noticed visually, so never though it did postbacked.
3. the 2nd rendering code path no longer through the script rendering, hence resulting no startup script to me....

its so confusing to me....

I believe if I ever try an interactive debug and start tracing on page_load, I would have noticed it....

hehe..eeee...hhh, sometime coded so ---advanced--- and forgotten some simple things..... kekekek

thx guys.



Well, since you so mind about the grade, if I can get a good script like the last one I shown, it would have save me trouble.

but then I would give B then. Since the cause is purely on me..... kekekeke
>>Well, since you so mind about the grade, if I can get a good script like the last one I shown, it would have save me trouble.
what more did u need? I thought we explained the things pretty well ..

>>Since the cause is purely on me..... kekekeke
hmmm ..

anyway there is no point in taking this any further as i have a feeling u would not understand .. but I would advice you have a look at how the grading works over here ..


mmm... ok

think I will make it default A next time, so that everyone happy....hehehehe

thx again, anyway
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.