Friday, March 16, 2012

Passing a variable: options

Hi, I am trying to pass three variables from a main page to a pop up window: I have a few options, but I can't figure out which to use! Here is what I am using to generate the new popup page:

<ahref="javascript:;"onclick="window.open('_search.aspx','child')">Search</a>

Is there a way to do this using <asp:button...> ? The problem is simply cosmetic.

Anways, back to my options:

1) A cookie. It's a very small page and I've never used cookies till now, so I don't want it to get overly complicated. I just need to pass the text values of three variables to this new window.

2) Using what I found through browsing the forums, I think this is using SessionID (can someone let me know if it is?):

On parent page: Session("var1")=newString

On child page: dim newString As String = Session("var1")

How does that work? Is this information stored in the cache on client/server? Does it use SessionID?

3) Response.Redirect("page.aspx", false) - this would work, as I believe it does not call Response.End() in that case, but how can this open a new window?

Thanks for your time!

James

To pass the varaibles in between pages, you can use session variables..

Try this code.. here i am passing 4 variabels from main page to PopUp window..

In Form1:

privatevoid Button1_Click(object sender, System.EventArgs e)

{

Session["name"]=TextBox1.Text;

Session["add"]=TextBox2.Text;

Session["phoneNum"]=TextBox3.Text;

Session["email"]=TextBox4.Text;

}

In PopupWindow:

privatevoid Page_Load(object sender, System.EventArgs e)

{

Label1.Text=Session["name"].ToString();

Label2.Text=Session["add"].ToString();

Label3.Text=Session["phoneNum"].ToString();

Label4.Text=Session["email"].ToString();

}

Hope it helps..


I think you generally use cookies only if you need to persist the data.

When a user starts a session, the data area for that user gets created. Storing values in session variables such as you show in #2 is a common way to do what you are trying to do. This area is unique to the user and stored on the server.

Response.redirect redirects your browser to the new page. It does not open a new window.

Also, depending on the size and type of data you need to pass, you might look into using a querystring as well.


1) cookies are just as easy to set as a session var.

2) session will work fine.

3) response.redirect CANT open a new window, its a server call

Have you looked at the QueryString option? ...page.aspx?id=myVar
This will pass the var directly to the page.

You could also store the var in a table/db, then read that field on the new page.

Lots of ways to do this.


Hey, thanks for the help. I had looked at QeryString, but I want to pass multiple variables and I was getting errors. Now, that may have had to do with my implementation :) I will look into these options - thanks again!

James


...page.aspx?var1=Nice&var2=Nicer&var3=Nicest
Great! Thanks :)

Alright, I ended up passing over the QueryStrings in order to use session variables. At this point though, there's a small issue I'd like to fix. When I click the button, I open a new page via this script:

PublicSub newSearch_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)

Dim jScriptStringAsString

jScriptString ="<SCR" +"IPT>"

jScriptString +="window.open('_search.aspx');"

jScriptString +="</SCR" +"IPT>"

Session("resultOrg") = searchOrg.Text

Session("resultCert") = searchCert.Text

Session("resultID") = searchID.Text

Response.Write(jScriptString)

EndSub

This new page has the session variables and can do what I need with it. However, when I click the search, the parent page executes a postback - while this is only a small annoyance, it seems to be a wasteful way to program. Is there a way to disable the postback on this particular button click? I've tried HTML buttons, but get "error on page" -

<buttonclass="Button"type="button"onclick="newSearch_Click"runat="server">Search</button>

<asp:ButtonID="searchButton"Text="Search"CssClass="Button"runat="server"OnClick="newSearch_Click"/>


couple options, but if you want to have the Session variable set FROM CODEBEHIND then you will have to do a postback...


Hi
Why the following instrution donesn't work?

Response.Redirect("inventario.aspx?quem=" + (GridView1.Rows(0).Cells(0).Text) &"nivel=" + (Passe_txt.Text))

I only get thequem value. The variablenivel is empty

Other question: How can i use request.form() in aspx?

Thank you


Found the solution

Response.Redirect("inventario.aspx?quem=" + (GridView1.Rows(0).Cells(0).Text) &"&nivel=" + (Passe_txt.Text))

0 comments:

Post a Comment