By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,572 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

open page and set session var. with one click

P: n/a
Hi all,
I'm having a little problem here.. my code is:
Session("test") = Button46.Text
Button46.Attributes.Add("Onclick", "opennewpage()")
i have this code inside the button click, which means that it needs 2 clicks
to take place. becuase the first click sets it and the 2nd does it, or
something like that...
if i move the "button46.attributes.add("onclick","opennewpage()" )" code to
the page load event, it works great... 1 click and the page opens..

now my problem is how do i set the session variable and open the page with 1
click..
if i leave the session var in the button click and the other in the page
load, then it will always be back one, if you know what i mean...

any ideas???

--
Ad****@hotmail.com
Nov 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi,

Hmmm, interesting. I see what you are doing, and can understand the
problem. The trouble is you are adding the onclick event of the button
after it has been clicked, and indeed after the page has been posted
back to the server, which you've already noticed.

I don't know what your opennewpage() function does, but I assume it at
least redirects the browser to another page. Is there any reason this
is done in script? e.g. does it do client side validation?

If it is simply a redirect, or functionality that can be handled server
side I would handle everything in the code-behind. For example:

Session("test") = Button46.Text;
Response.Redirect("mynewpage.aspx");
Regards,

Peter Chadwick (MCP)
pe**@code-explorers.com

Nov 19 '05 #2

P: n/a
Hi peter,
thanks for the reply.. I don't need to do any client side validation, but i
do need to keep the window that's open now still open, and open the new page
in a new window...
response.redirect("") will just replace the current window.. but at least it
will do it with one click

do you know server side script that will open the page in a new window??

"Peter Chadwick (MCP)" wrote:
Hi,

Hmmm, interesting. I see what you are doing, and can understand the
problem. The trouble is you are adding the onclick event of the button
after it has been clicked, and indeed after the page has been posted
back to the server, which you've already noticed.

I don't know what your opennewpage() function does, but I assume it at
least redirects the browser to another page. Is there any reason this
is done in script? e.g. does it do client side validation?

If it is simply a redirect, or functionality that can be handled server
side I would handle everything in the code-behind. For example:

Session("test") = Button46.Text;
Response.Redirect("mynewpage.aspx");
Regards,

Peter Chadwick (MCP)
pe**@code-explorers.com

Nov 19 '05 #3

P: n/a
Hmmm, I don't know of anyway of starting a new window server side.

However, I have just found a work-around to your problem I think.

Rather than adding javascript to your button on the postback, do your
session stuff as normal then add some javascript code to the PAGE. As
the page is rendered after the button event the js code will be written
out and if it's not in a function it will run straightaway.

For example I added the following to the button onclick event in the
code-behind:

Session["test"] = Button1.Text;
this.RegisterStartupScript("MyScript",
"<script>\nalert('test!');\n</script>\n");

When I clicked the button the page posted back and ran this code. When
the page rendered the javascript fired straight away. Replace my simple
alert box with your redirect and you should be away...
Good luck,

Regards,

Peter Chadwick (MCP)
pe**@code-explorers.com

Nov 19 '05 #4

P: n/a
Hi peter,
I tried a few different ways with the code written in the button click and
some in the page, but couldn't get anything to work..
this is my opennewwindow() code:
Try
Dim y As String

y = "window.open('" & pagePath & "')"
opener.Attributes.Add("OnClick", y)
opener.Dispose()
opener = Nothing
y = ""
Catch ex As Exception
End Try

this is the only way to get it to open in a new window...
how else can i do it...
i'm using asp.net and vb.net code, with javascript...

"Peter Chadwick (MCP)" wrote:
Hmmm, I don't know of anyway of starting a new window server side.

However, I have just found a work-around to your problem I think.

Rather than adding javascript to your button on the postback, do your
session stuff as normal then add some javascript code to the PAGE. As
the page is rendered after the button event the js code will be written
out and if it's not in a function it will run straightaway.

For example I added the following to the button onclick event in the
code-behind:

Session["test"] = Button1.Text;
this.RegisterStartupScript("MyScript",
"<script>\nalert('test!');\n</script>\n");

When I clicked the button the page posted back and ran this code. When
the page rendered the javascript fired straight away. Replace my simple
alert box with your redirect and you should be away...
Good luck,

Regards,

Peter Chadwick (MCP)
pe**@code-explorers.com

Nov 19 '05 #5

P: n/a
Hi,

I'm sorry but I don't really understand the code you posted above. Is
this the function you want to call when the user clicks the button? I
thought your opennewwindow() code was javascript?

Anyway, if your opennewwindow() javascript code is already in your page
and you just want to call it when the button is clicked, how about
something like this for your button click (vb.net code-behind) code:

Session("test") = Button46.Text
Page.RegisterStartupScript("OpenNewWindow", "<script>" & vbCrLf &
"opennewwindow()" & vbCrLf & "</script>"

This puts the function call to opennewwindow() directly in the page on
the postback, rather than the button (which requires the user to click
it). Btw, the "OpenNewWindow" is just a key to identify the block of
script you are inserting. It can be any string you like really.

Hope this makes some sense. If i'm not being very clear (I'm not the
best teacher), feel free to e-mail me your page (and code-behind class
of course) and I'll have a look and hopefully get you going...
Regards,

Peter Chadwick (MCP)
pe**@code-explorers.com

Nov 19 '05 #6

P: n/a
Thanks for all the help peter, but i could never get this to work properly...
i tried your code here but kept getting an error

"Peter Chadwick (MCP)" wrote:
Hi,

I'm sorry but I don't really understand the code you posted above. Is
this the function you want to call when the user clicks the button? I
thought your opennewwindow() code was javascript?

Anyway, if your opennewwindow() javascript code is already in your page
and you just want to call it when the button is clicked, how about
something like this for your button click (vb.net code-behind) code:

Session("test") = Button46.Text
Page.RegisterStartupScript("OpenNewWindow", "<script>" & vbCrLf &
"opennewwindow()" & vbCrLf & "</script>"

This puts the function call to opennewwindow() directly in the page on
the postback, rather than the button (which requires the user to click
it). Btw, the "OpenNewWindow" is just a key to identify the block of
script you are inserting. It can be any string you like really.

Hope this makes some sense. If i'm not being very clear (I'm not the
best teacher), feel free to e-mail me your page (and code-behind class
of course) and I'll have a look and hopefully get you going...
Regards,

Peter Chadwick (MCP)
pe**@code-explorers.com

Nov 19 '05 #7

P: n/a
Hi,

You're welcome. And the offer still stands - if you would like me to
take a look at the page just e-mail it over (it's sometimes easier to
understand the problem when you can see the whole picture).
Regards,

Peter Chadwick
pe**@code-explorers.com

Nov 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.