open page and set session var. with one click

G

Guest

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???
 
P

Peter Chadwick (MCP)

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)
(e-mail address removed)
 
G

Guest

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??
 
P

Peter Chadwick (MCP)

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)
(e-mail address removed)
 
G

Guest

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

Peter Chadwick (MCP)

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)
(e-mail address removed)
 
G

Guest

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
 
P

Peter Chadwick (MCP)

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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,162
Latest member
GertrudeMa
Top