Getting values from one aspx page to other - asp.net

Hi
I have two aspx pages page1.aspx and page2.aspx. I have an InsertButton on page1. when I click Insert,I am opening page2.aspx using javasript( window.open). Now, in page2, i am giving some input in textboxes. when click on OK button on page2,that page has to be closed and I have to use those inputs in page1. How can I do this..
Could some one help me pls??
(I tried using session. But It didn't wrk for me.)
thanks,

try storing the values in page2.aspx in session variables.it will definitely work

its so simple....first you must bring that specified value to a session
like,,,,
session["PID"]=text1.text.ToString();
after that you must declare a public string/int/any variable on the another page(where u want to get the session value)
then pass the session["PID"] to the publically declared variable(public string VARNAME)
like,,,,,,
VARNAME=Session["PID"].Tostring();

Related

Property in viewstate different on one page to another

This is really weird error i'm getting and i'll try and explain as best I can.
I have two pages - Page 1 (form) and Page 2 (completed page)
From page 1 I put a variable into a database and then do a server.transfer to page two like so...
Server.Transfer("Page2.aspx", True)
On page 2 I then grab the variable called paymentOnHold which is set on Page 1 and goes into the database...
Here is how I set paymentOnHold on Page 1
Public Property paymentOnHold() As String
Get
Dim _paymentOnHold As Object = ViewState("paymentOnHold")
If _paymentOnHold IsNot Nothing Then
Return CType(_paymentOnHold, String)
Else
Return Nothing
End If
End Get
Set(ByVal value As String)
If Not String.IsNullOrEmpty(value) Then
ViewState("paymentOnHold") = value
Else
ViewState("paymentOnHold") = Nothing
End If
End Set
End Property
...
paymentOnHold = Date.Now.ToString("yyyyMMddHHmmss")
Here's how I grab the value on Page 2...
Dim myValue As String
If TypeOf PreviousPage Is Page1 Then
myValue = DirectCast(PreviousPage, Page1).paymentOnHold
End If
In my development environment where the databases are local the value in the DB and the value on page 2 both match - as you would expect...
In live environment the DB value is 3 or 4 seconds different (before) the one on Page 2 - even though I do not reset it or anything?
This has been driving me crazy for the last few hours and cannot work it.
Does anyone have any ideas/suggestions as to what might be causing this?
Thanks in advance
This could be an issue of saving the view state in first Page (form-1)
In asp.net Page lifecycle
1. Initalization (controls raise their Init event)
2. Load ViewState (Only on post back)
3. Load PostbackData
4. Load
5. Raise PostbackEvent
6. Save View State
7. Render
Server.Transfer() stops rendering the current page and starts rendering another one.That's why Server.Transfer() cannot be used to redirect to pages served by another server.
If you are doing Server.transfer before Event--> 6. Save View State you are not saving viewstate on the form-1
Solution
Response.redirect and session cache, as it is intended to exist per user and across multiple pages in the application.
Using ViewState in this manner is a brittle solution, because ViewState is not intended to exist outside of the scope of the page it was initiated in, much less passed between pages, which I realize you are not quite doing, but you are getting dangerously close to doing it.
The better approach is to use Session cache, as it was intended to exist per user and span multiple page requests.
Try this:
To store in Session, do this:
Session("PaymentOnHold") = [Date].Now.ToString("yyyyMMddHHmmss")
To retrieve a value from Session, do this:
' First check to see if the value is in Session cache or not
If Session("PaymentOnHold") IsNot Nothing Then
' Everything in Session cache is stored as an object so you need to cast it to get it out
Dim datePaymentOnHold As DateTime = TryCast(Session("PaymentOnHold"), DateTime)
End If
Now the Session value will be available no matter how you navigate to pages (Server.Transfer or Response.Redirect).

Passing data between different URLs

I need to check where did the incoming request came from before loading a page
ex:
for user to view www.mysite/page1.aspx
request should come through www.othersite/page1.aspx
so on page1 load in mysite i need to check whether the request came from page1 in othersite.
i have tried Page.Request.UrlReferrer but i saw there some posts which tells every browser might not support Page.Request.UrlReferrer.
i can not pass visible parameters on URL.
This is a common issue when you do not want to allow request from arbitrary sites.
What you can do is, create a variable in session and put this variable in the Page1.aspx. When the page posts back, you should get that variable back and it should also match the one stored in the session. If it does not, you can be sure that the request is from some other server.
You can use PostBackUrl on the start page
And access your parameters with PreviousPage in the arrived page
if (this.PreviousPage != null)
{
var control = Page.PreviousPage.FindControl("..."); //Adjust your Id and add cast
}
Nota : This was also created to provide greater security redirection setting.

How to pass value from web form to another web form?

Can anybody tell me how to pass a value from one web form to another web form without using a query string and session?
You can pass the Values over different pages via QueryString like:
Response.Redirect("yourNextpage.aspx?identifier=DesiredValue");
On your next page you can retrieve the value like this:
Request.QueryString["identifier"];
Other Preferred way would be Server.Transer() and Postbackurl.
Refer this link for various possible ways.
there are several ways you can pass parameters between pages.
Using a Query String
Getting Post Information from the Source Page
Using Session State
Getting Public Property Values from the Source Page
Getting Control Information from the Source Page in the Same Application
for more detail visit followng link.
http://msdn.microsoft.com/en-us/library/6c3yckfw.aspx
You could use a Querystring in this case:
Page.Response.Redirect("show.aspx?id=1");
And then read it on the other end:
int id = Page.Request.QueryString["id"];
Using PostBackURL, ex:
PostBackUrl="~/result.aspx"
and on result.cs (Page Load)
lblEmployeeNumber.Text = HttpContext.Current.Request.Form["txtEmployeeNumber"];
With Session:
For example you login the system and your id is 123123123.
string userid = 123123123;
Session["userid"] = userid;
When you go another page/pages your session is alive when your session timeout.
<system.web>
<sessionState timeout="1250"/>
</system.web>
It seems what you're looking for is something like the flash-, view- or conversation scope in Java EE and Ruby on Rails.
For ASP.NET you could perhaps take a look at this one: Is there an equivalent of JSF #ViewScope in ASP MVC?
depends on type and how much information you wish to transfer. for instance, if you want to transfer some variable (strings or integer values) you consider to use querystring (you can found here major information). for instance, if you want to transfer typed objects (class instance) you consider to use session (you can found here major information).

How to get data from a control into another ASP.net page?

I'm creating a time sheet for work to learn more about asp and making database connections I am also using this time to prepare for my next C# and database design class which start on Wednesday. I'd like to know how I can get data from default.aspx and display it in timesheetdisplay.aspx, and I would also like to know how I can make it so the person doesn't have to enter the full id "100000111" as it appears in the database just the last 3.
<asp:TextBox id="xBadgeTextBox" runat="server" width="100px"></asp:TextBox>
As far as passing data between pages you can pass it via QueryString, Session variables, or by persisting it to some sort of data store such as a Database. In the situation above I would look at passing via Querystring parameter. Be sure that if you do do this that you validate the data on the new page to ensure its safety and validity before using it (think SQL Injection Attack).
How to: Pass Values Between ASP.NET Web Pages
As far as your second question goes I would say that this could be handled on the server side if you are sure that the last 3 digits will always be unique. Or were you looking to prompt the user entering data similar to Google? If so look at the AutoComplete Extender in the AJAX Control Toolkit or look at doing something similar in JQuery.
If you're redirecting from page to page, consider using the Server.Transfer("timesheetdisplay.aspx", true) method when navigating away from your default.aspx page. Note the second parameter, true, which will persist all ViewState and QueryString data across from page to page.
I would generate a unique key, store the value you are transfering in the users session, redirect the user and include the key in the query string, grab the key, and then get the value. Something like this:
//---On Default---
var value = "can be a single string or even a complext object...";
var keyName = Guid.NewGuid().ToString();
HttpContext.Current.Session[keyName] = value;
HttpContext.Current.Response.Redirect("timesheetdisplay.aspx?SID=" + keyName);
//---On TimeSheet---
var getKeyName = HttpContext.Current.Request.QueryString["sid"].ToString();
var myValue = HttpContext.Current.Session[keyName];
To get the id from a partial ID I would do it just like Muhammad Akhtar said:
select * From yourtable where id like '%111'

Adding a condition in the url

I would line to add a test condition in an asp.net form such that:
1) From page1.aspx I manually add a query string parameter so that I can trigger the rest of the process in test mode like so: page1.aspx?test=true . This flag must be added in the query string.
2) When I click on a asp.net button in page1.aspx, I am redirected to page2.aspx in test mode
because of teh attached querystring
It seems that I have to work around the postback model of asp.net this is not very straight forward.
Any idea how I can achieve the above behavior?
Thanks
It sounds like you're using a form that posts, but you want to stay in "test" mode. That is, you're not using HTTP-GET so it's not realistic to pass QS variables around.
What I'd do is stash a variable in your Session to set the user's session test mode. So adding &test=true would trigger a Session["TestMode"] = true; before you move to the next page.
Try this (in server-side code)
Response.Redirect("Page2.aspx?Test=" + Request.QueryString["Test"]);

Resources