[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 581
  • Last Modified:

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

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>

It WORKED!!!!

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

TIA
0
Kelmen
Asked:
Kelmen
  • 6
  • 4
  • 2
  • +1
2 Solutions
 
aki4uCommented:
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.
0
 
KelmenAuthor Commented:
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.
0
 
KelmenAuthor Commented:
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......
0
Industry Leaders: 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!

 
aki4uCommented:
"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>")


0
 
RejojohnyCommented:
>>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?

Rejo
0
 
bsdotnetCommented:
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?
0
 
KelmenAuthor Commented:
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.....
0
 
RejojohnyCommented:
>>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 ..
0
 
bsdotnetCommented:
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>")
        Else
            TextBox1.Text = DateTime.Now.ToString
        End If
    End Sub
0
 
RejojohnyCommented:
just curious, any particular reason for a grade C?
0
 
KelmenAuthor Commented:
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:

>alert('submitting.....');...submit();<

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.

:D
0
 
KelmenAuthor Commented:
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
0
 
RejojohnyCommented:
>>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 ..


0
 
KelmenAuthor Commented:
mmm... ok

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

thx again, anyway
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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