Link to home
Start Free TrialLog in
Avatar of Mickeys
MickeysFlag for Sweden

asked on

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

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?
SOLUTION
Avatar of Salim Fayad
Salim Fayad
Flag of Lebanon image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mickeys

ASKER

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)
Avatar of Mickeys

ASKER

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.
You can do this like too :

 protected void Page_Load(object sender, EventArgs e)
    {
        if ( Session["IsMale"] != null )
        {
        resultLabel.Text = Session["IsMale"].ToString();
        }
    }
In this case , Don't put PostbackURL as it never set the session variable.:
Press F10, F11 to walkthrough the code. what find what happens. I think you accidentally put the crosspageposting .
Avatar of Mickeys

ASKER

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? :-)
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

Avatar of Mickeys

ASKER

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.
And also  Put the name of the file in the quoted string.
 
Response.Redirect ("Page2.aspx");
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
Avatar of Mickeys

ASKER

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?
Avatar of Mickeys

ASKER

O thanks for all your help. I will rate you. :-)
The Standard method, we are doing is Response.Redirect after the code execution.
Avatar of Mickeys

ASKER

Thx for your help