Monday, March 26, 2012

Pass values

Hi,
How to pass values from one to another page using post method? And how to retrieve them at another page?

ASP.NET uses by default POST method, so what is your question?

Regards


You can use a querystring through the URL, the session object, application object, Server.Transfer().

Depending on what you need, those are a few of the ways to maintain state off the top of my head.
In old asp 3.0. I'd use something like <form action="page2.asp"...>
Then In page two I'd use request to retrieve values.
How to achieve this with asp.net?

"In old asp 3.0. I'd use something like <form action="page2.asp"...>
Then In page two I'd use request to retrieve values."
In .Net you don't post do a different page, by default it post's back to itself. All web controls that have viewstate enabled will have their values stored during the postback and be accessable when the page post backs to itself.
Perhaps if you give us a specific example of what you're trying to accomplish it'd be a bit easier to help you specifically where you need it?
Lets say I want to have simple search funcionality. Search string whentyped in any page, should be passed to results.aspx page. There shouldexact searchs occur and results be displayed.
So...?

In c# code you should do something like this:

Response.Redirect("Results.aspx?searchString="+searchBox.Text+); //put a parameter in the redirect page

Note that the "searchString" is the name of the parameter you areadding to the redirect, you can call it whatever you want. ThesearchBox.Text is the TextBox of search.

Now in the page you've redirected, you will extract the parameter

string textSearch = Response.QueryString["searchString"];

Now that I'm here, how did you develop your search engine?Do you checkevery table in your database and return every values that match thesearch?If you have news, studies in your database you show for examplenews and then the news related, and then the studies and then therelated studies for example?

Thanks, hope I did help!

Hi,

If you have html page with search form, for example, you can add <form action="Result.aspx" type="post"> in it then you can get all post variables using Request.Form[variableName]. In case you use form as aspx page you do not need to transfer to other page - you can and should process all events (buttons clicks etc) right in this page. It's common for asp.net and it's recommend to use this technique.

But if you need to transfer post data to other aspx page you can use Server.Transfer() method in, for instance, button click event. Then you can get all post variables including controls state from previous aspx page using Request.Form.

In asp.net 2 you can also use cross page postback (read more about it therehttp://msdn2.microsoft.com/en-us/library/ms178139(vs.80).aspx).


I'm have the same problem. How can I get the Query String in VB?

do server.transfer.

Let me give you an example.

-- default page --<form runat="server"> ... <asp:textbox id="text1" runat="server" text="value1here"/> <asp:textbox id="text2" runat="server" text="value2here"/> ... <asp:button id="btn1" runat="server" text="button1"/> <asp:button id="btn2" runat="server" text="button2"/> ...</form>-- this is the btn1 onclick behavior you want to go page1.aspx right? --Protected Sub btn1_Click(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles btn1.Click server.transfer("page1.aspx", true)end sub-- this is the way to show text1 at page1.aspx --Protected Sub Page_Load(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles Me.Load label1.text = request.form("text1")end sub

that's it. you can pass the values within http request, because when you click buttons, the values gets saved to http request via postback. alll you are doing is passing values in http request in next page. form submission is done for you automatically.

improvise and you'll succeed.

Hope this helps and let me know what happens.

Jae.


Great but what if iis in a datalist? I can't get the value out.

you can declare a hidden field:

 <asp:DataList ID="datalist1" runat="Server"> <ItemTemplate> <asp:HiddenField ID="hidden" Value='<%#eval("valuetopass")%>'> </ItemTemplate> </asp:DataList>

user server.transfer afterwards then retrieve value by:

request.form("hidden")
Hope this helps.
Jae.

If you are using .NET 2.0, you can set the postBackURL on the form. This will allow your page to directly post to your new page.

Nick


Do you know how to do this in VB.net?

Sorry, I got it

Request.QueryString(

"Variable")

Thanks for the help.Big Smile

0 comments:

Post a Comment