Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

.Net with C#. Take values from one page to a new page

Posted on 2009-02-21
17
Medium Priority
?
404 Views
Last Modified: 2012-06-27
Hi,
I have created a page where I get some values I save like this

In button_ok_click

String test = pnrTextBox.Text;
        string output = pnr.IsValidFormat(test);
        bool flag = pnr.isMale();
       
        if(flag)
        {
            ViewState.Add("isMale", "Man");
           
        }
        else
        {
            ViewState.Add("isMale", "Woman");
        }

What i want is that the word woman or man will come to my next page. I thought ViewState.Add was used but it doensent work.

On the other page when I have clicked the ok button I get an error:
Check if the value is null or create new

I have writen like this to get the value out

    protected void Page_Load(object sender, EventArgs e)
    {
        resultLabel.Text = ViewState["IsMale"].ToString();

    }


Is this the wrong way to go? What is the correct way? How should I do it?
One thing u might need to know is that i have put PostBackUrl to the next page where the error happens. Does this mean that it doesnt go throw the code and just jump to the next page? And if it is like that what should I do so it first run the code and then takes me to the new page?
0
Comment
Question by:Mickeys
17 Comments
 
LVL 11

Assisted Solution

by:Salim Fayad
Salim Fayad earned 200 total points
ID: 23699036
ViewState saves information for the same page. So, you cannot pass data to the next page using the ViewState. You can use one of the following:
  1. Session: it will save data for the logged in user for all his pages
  2. QueryString: you can pass your data from page to page using the querystring.
I prefer the second way because this way you don't have to manage your sessions in your application and you will have your page independant from the previous page, but only dependant on the querystring which means that wherever you need to call it, all you have to do is to pass the data in your querystring and not through a session and what if you forgot to do that and the session already had previous data.
0
 
LVL 7

Accepted Solution

by:
thiyaguk earned 1600 total points
ID: 23699185
Hi,
 ViewStates are saved in that same page and cannot be used from other pages.
You can use Session Variable or QueryString to pass strings. To pass lot of information from one page to another page use crosspage posting.


  if(flag)
        {
            Session["isMale"] = "Man";
           
        }
        else
        {
            Session["isMale"] = "Woman";
        }
 
and in other page:
 
   protected void Page_Load(object sender, EventArgs e)
    {
        resultLabel.Text = Session["IsMale"].ToString();
 
    }

Open in new window

0
 
LVL 6

Assisted Solution

by:regsworld
regsworld earned 200 total points
ID: 23699389
1.
See:
 http://msdn.microsoft.com/en-us/library/caxa892w.aspx
 

2.
OR use QueryString
In Page1.aspx: Response.Redirect("Page2.aspx?IsMale=" + (pnr.isMale()? "Man" : "Woman") );
In Page2.aspx: Request.QueryString["IsMale"];

Ref:
 http://www.java2s.com/Code/ASP/Request/GetquerystringfromRequestC.htm
 http://authors.aspalliance.com/aspxtreme/sys/web/httprequestclassquerystring.aspx



3. persist viewstate
 http://geekswithblogs.net/joelross/archive/2006/03/23/73180.aspx
1.
In button_ok_click
 
String test = pnrTextBox.Text;
        string output = pnr.IsValidFormat(test);
        bool flag = pnr.isMale();
        
        if(flag)
        {
            Session["isMale"] = "Man";
            
        }
        else
        {
            Session["isMale"] = "Woman";
        }
 
    protected void Page_Load(object sender, EventArgs e)
    {
        resultLabel.Text = Session["IsMale"].ToString();
    }
 
 
 
 
 
 
2.
In button_ok_click
 
String test = pnrTextBox.Text;
        string output = pnr.IsValidFormat(test);
        bool flag = pnr.isMale();
        
        if(flag)
        {
            Session["isMale"] = "Man";
            
        }
        else
        {
            Session["isMale"] = "Woman";
        }
 
    protected void Page_Load(object sender, EventArgs e)
    {
        resultLabel.Text = Session["IsMale"].ToString();
    }

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Mickeys
ID: 23699404
Well I have tried the Session thing and I still get the same error:

System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."
  Source="App_Web_wekhaenq"
  StackTrace:
       at Default2.Page_Load(Object sender, EventArgs e) in c:\Documents and Settings\MFL\Dokumenter\Visual Studio 2005\WebSites\WebSite\WebSite\Inlamning1\Result.aspx.cs:line 16
       at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
       at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
       at System.Web.UI.Control.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
0
 

Author Comment

by:Mickeys
ID: 23699410
Is this because it never reads the code and just jump to my new page? I have put PostBackUrl  to my page2 on the click button.
0
 
LVL 7

Expert Comment

by:thiyaguk
ID: 23699427
You can do this like too :

 protected void Page_Load(object sender, EventArgs e)
    {
        if ( Session["IsMale"] != null )
        {
        resultLabel.Text = Session["IsMale"].ToString();
        }
    }
0
 
LVL 7

Expert Comment

by:thiyaguk
ID: 23699432
In this case , Don't put PostbackURL as it never set the session variable.:
0
 
LVL 7

Expert Comment

by:thiyaguk
ID: 23699439
Press F10, F11 to walkthrough the code. what find what happens. I think you accidentally put the crosspageposting .
0
 

Author Comment

by:Mickeys
ID: 23699441
Well I can but that wont change the result because it will be null and nothing will come out.

The problem. I have three strings I want to take from my page1 to my page2 after I have clicked ok button on page1.

The question. How do i do it. It seems like it just jumps right over to page2 without running the code because I have put the property posbackurl to page2.asax.

So what I need is how do I run the code and then jump over to my page2 and takes out my strings.

I can use Session but I need the code to be run in my button click metohod before I jump over to my page2.

hmmmmm Did I explain ok? :-)
0
 
LVL 7

Expert Comment

by:thiyaguk
ID: 23699447
Please Remove the PostbackUrl property and put this line to redirect to next page:
In Page1  : button_ok_click
 
String test = pnrTextBox.Text;
        string output = pnr.IsValidFormat(test);
        bool flag = pnr.isMale();
        
        if(flag)
        {
            Session["isMale"] = "Man";
            
        }
        else
        {
            Session["isMale"] = "Woman";
        }
 
Response.Redirect ("Page2.aspx");
 

Open in new window

0
 

Author Comment

by:Mickeys
ID: 23699453
Hmm when I removed the postback and used Response.Redirect......
then it worked.

But how do I manage to do the same thing with Session?
Do I really have to use Response.Redirect then or is there another way?
I thought the property was just for this reason.
0
 
LVL 7

Expert Comment

by:thiyaguk
ID: 23699454
And also  Put the name of the file in the quoted string.
 
Response.Redirect ("Page2.aspx");
0
 
LVL 7

Expert Comment

by:thiyaguk
ID: 23699461
Actually, you have done this with session only. But redirection is


Do I really have to use Response.Redirect then or is there another way?
       There is two way to redirect the page after processing and setting  variable
             1. Response.redirect
             2. Server.Transfer

http://www.dotnetspider.com/forum/190078-Difference-between-Response-Redirect-Server-Transfer.aspx
0
 

Author Comment

by:Mickeys
ID: 23699467
ok I got it to work. you will all get your points. :-)

I just need to know if this is the way you should do it? Should I always use response.redirect(....) or is there another way to first run the code in click button and then move on to the new page?
0
 

Author Comment

by:Mickeys
ID: 23699479
O thanks for all your help. I will rate you. :-)
0
 
LVL 7

Expert Comment

by:thiyaguk
ID: 23699485
The Standard method, we are doing is Response.Redirect after the code execution.
0
 

Author Closing Comment

by:Mickeys
ID: 31549534
Thx for your help
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question