Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › ASP.Net cross-page referencing
New Posts  All Forums:Forum Nav:

ASP.Net cross-page referencing

post #1 of 11
Thread Starter 
I am having a problem with cross page referencing. I have two pages, one with a form to be fille w/ information about the employee (EnterData.aspx) and the other with a table to subtract taxes and to calculate the net income(DisplayData.aspx).

I keep getting an error stating:
Code:

Error    2    The type or namespace name 'EnterData' could not be found (are you missing a using directive or an assembly reference?)    C:UsersM1330DocumentsVisual Studio 2005WebSitesHomework 2DisplayData.aspx.cs    17    21    C:...Homework 2 

In my EnterData.aspx.cs it appears that the class name (EnterData) is not changing color like it should. I have tried other names and various ways of calling it but to no avail. . . I'll post the code below.


THANK YOU in advance for helping me.

EnterData.aspx.cs

Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class EnterData : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public string GetEmpID
    {

        get
        {
            return txtEmployeeID.Text;
        }
    }

    public string GetEmpName
    {

        get
        {
            return txtEmpName.Text;
        }
    }

    public decimal GetHours
    {

        get
        {
            return Double.Parse(txtHours);
        }
    }

    public Double GetType
    {

        get
        {
            if (rdbEmpCasualLaborer.Checked)
                type = 7.75;
            if (rdbEmpApprentice.Checked)
                type = 10.65;
            if (rdbEmpJourneyman.Checked)
                type = 15.75;

            return type;
        }
    }
}
DisplayData.aspx.cs
Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class DisplayData : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        EnterData prevPage = prevPage = (EnterData) PreviousPage;

        Double grossPay = (prevPage.GetType) *
                                (prevPage.GetHours);
        string empName = prevPage.GetEmpName;
        string empID = prevPage.GetEmpID;


        // -----------Setup row 1 ------------------
        TableRow tr1 = new TableRow();
        tr1.Font.Bold = true;

        // Set up cells for row 1
        TableCell r1c1 = new TableCell();
        r1c1.Text = "Description";
        r1c1.Width = 100;
        TableCell r1c2 = new TableCell();
        r1c2.Text = "Amount";
        r1c2.Width = 100;

        //Add cells to row 1
        tr1.Cells.Add(r1c1);
        tr1.Cells.Add(r1c2);

        // ----------- Setup Row 2 ------------------
        TableRow tr2 = new TableRow();

        TableCell r2c1 = new TableCell();
        r2c1.Text = "Gross Pay";
        r2c1.Width = 100;
        TableCell r2c2 = new TableCell();
        r2c2.Text = grossPay.ToString();
        r2c2.Width = 100;

        // ----------- Setup Row 3 ------------------
        TableRow tr3 = new TableRow();

        TableCell r3c1 = new TableCell();
        r3c1.Text = " ";
        r3c1.Width = 100;
        TableCell r3c2 = new TableCell();
        r3c2.Text = " ";
        r3c2.Width = 100;

        // ----------- Setup Row 4 ------------------
        TableRow tr4 = new TableRow();

        TableCell r4c1 = new TableCell();
        r4c1.Text = "Federal Income Tax";
        r4c1.Width = 100;
        TableCell r4c2 = new TableCell();
        double deduction1 = grossPay * .185;
        r4c2.Text = "$" + deduction1;
        r4c2.Width = 100;

        // ----------- Setup Row 5 ------------------
        TableRow tr5 = new TableRow();

        TableCell r5c1 = new TableCell();
        r5c1.Text = "State Income Tax";
        r5c1.Width = 100;
        TableCell r5c2 = new TableCell();
        double deduction2 = grossPay * .032;
        r5c2.Text = "$" + deduction2;
        r5c2.Width = 100;

        // ----------- Setup Row 6 ------------------
        TableRow tr6 = new TableRow();

        TableCell r6c1 = new TableCell();
        r6c1.Text = "Social Security Tax";
        r6c1.Width = 100;
        TableCell r6c2 = new TableCell();
        double deduction3 = grossPay * .0585;
        r6c2.Text = "$" + deduction3;
        r6c2.Width = 100;

        // ----------- Setup Row 7 ------------------
        TableRow tr7 = new TableRow();

        TableCell r7c1 = new TableCell();
        r7c1.Text = "Medicare/Medicaid Tax";
        r7c1.Width = 100;
        TableCell r7c2 = new TableCell();
        double deduction4 = grossPay*.0075;
        r7c2.Text = "$" + deduction4;
        r7c2.Width = 100;

        // ----------- Setup Row 8 ------------------
        TableRow tr8 = new TableRow();

        TableCell r8c1 = new TableCell();
        r8c1.Text = "Pension Plan";
        r8c1.Width = 100;
        TableCell r8c2 = new TableCell();
        double deduction5 = grossPay*.005;
        r8c2.Text = "$" + deduction5;
        r8c2.Width = 100;

        // ----------- Setup Row 9 ------------------
        TableRow tr9 = new TableRow();

        TableCell r9c1 = new TableCell();
        r9c1.Text = "Health Insurance";
        r9c1.Width = 100;
        TableCell r9c2 = new TableCell();
        double deduction6 = 5.15;
        r9c2.Text = "$" + deduction6;
        r9c2.Width = 100;

        // ----------- Setup Row 10 ------------------
        TableRow tr10 = new TableRow();

        TableCell r10c1 = new TableCell();
        r10c1.Text = " ";
        r10c1.Width = 100;
        TableCell r10c2 = new TableCell();
        r10c2.Text = " ";
        r10c2.Width = 100;

        // ----------- Setup Row 11 ------------------
        TableRow tr11 = new TableRow();

        TableCell r11c1 = new TableCell();
        r11c1.Text = "Net Income";
        r11c1.Width = 100;
        TableCell r11c2 = new TableCell();
        double netPay = grossPay - deduction1 - deduction2
                        - deduction3 - deduction4 - deduction5
                         - deduction6;
        r11c2.Text = netPay.ToString();
        r11c2.Width = 100;
    }
}
 
ESXi Server
(10 items)
 
HTPC
(13 items)
 
CPUMotherboardGraphicsRAM
i7-4790K Asus Sabertooth Mark 2 Radeon HD 6950 GSkill Sniper Series 
Hard DriveCoolingOSMonitor
Samsung XP941 Noctura NH-D14 8.1 Pro QNIX 27", Dell 24", Samsung 22", Sony 24" 
KeyboardPowerCaseMouse
Corsair K95 PC&P 750 NZXT H2 Logitech G500s 
CPUHard DriveHard DriveHard Drive
Phenom II x4 965 WE Red Seagate Intel SSD 
Hard DriveCoolingOSOS
WR Raptor  Silenx Fans x2 120mm Win 7 x2 Unraid 
OSCase
pfsense Rackmount 16 bay hot-swap 
CPUMotherboardGraphicsRAM
i3 2100t Gigabyte integrated 2x 8GB GSkill 
Hard DriveCoolingOSMonitor
OCZ SSD Stock Win7 - Plex Shell Sony XBR2 
PowerCase
Generic 350 watt Repurposed Vidabox 
  hide details  
Reply
 
ESXi Server
(10 items)
 
HTPC
(13 items)
 
CPUMotherboardGraphicsRAM
i7-4790K Asus Sabertooth Mark 2 Radeon HD 6950 GSkill Sniper Series 
Hard DriveCoolingOSMonitor
Samsung XP941 Noctura NH-D14 8.1 Pro QNIX 27", Dell 24", Samsung 22", Sony 24" 
KeyboardPowerCaseMouse
Corsair K95 PC&P 750 NZXT H2 Logitech G500s 
CPUHard DriveHard DriveHard Drive
Phenom II x4 965 WE Red Seagate Intel SSD 
Hard DriveCoolingOSOS
WR Raptor  Silenx Fans x2 120mm Win 7 x2 Unraid 
OSCase
pfsense Rackmount 16 bay hot-swap 
CPUMotherboardGraphicsRAM
i3 2100t Gigabyte integrated 2x 8GB GSkill 
Hard DriveCoolingOSMonitor
OCZ SSD Stock Win7 - Plex Shell Sony XBR2 
PowerCase
Generic 350 watt Repurposed Vidabox 
  hide details  
Reply
post #2 of 11
There is a few things wrong here:

You are delacring your class as a
public partial class EnterData : System.Web.UI.Page

But it isn't, it is just a class definition (form what I am seeing), now your page
public partial class DisplayData : System.Web.UI.Page

IS a page - and a page can't refernce another page, so, you must make your class definition JUST a class...

Change:
public partial class EnterData : System.Web.UI.Page

To:
public class EnterData
and lose the page_load event

You have no constructors on what a EnterData object is. You need to define your objects first. Once you have your object constructors built, you have to then initialize them in your page.

Let me know if I am making any sense and I will try to help you along.
post #3 of 11
Thread Starter 
Quote:
Originally Posted by stupid View Post
There is a few things wrong here:

You are delacring your class as a
public partial class EnterData : System.Web.UI.Page

But it isn't, it is just a class definition (form what I am seeing), now your page
public partial class DisplayData : System.Web.UI.Page

IS a page - and a page can't refernce another page, so, you must make your class definition JUST a class...

Change:
public partial class EnterData : System.Web.UI.Page

To:
public class EnterData
and lose the page_load event

You have no constructors on what a EnterData object is. You need to define your objects first. Once you have your object constructors built, you have to then initialize them in your page.

Let me know if I am making any sense and I will try to help you along.
the "public partial class EnterData : System.Web.UI.Page" and the page_load event were automatically there from when I added a page in VS. I fifgured they should be left alone?

Ill PM you in a sec. . . thanks for the help . . other are welcome to add
 
ESXi Server
(10 items)
 
HTPC
(13 items)
 
CPUMotherboardGraphicsRAM
i7-4790K Asus Sabertooth Mark 2 Radeon HD 6950 GSkill Sniper Series 
Hard DriveCoolingOSMonitor
Samsung XP941 Noctura NH-D14 8.1 Pro QNIX 27", Dell 24", Samsung 22", Sony 24" 
KeyboardPowerCaseMouse
Corsair K95 PC&P 750 NZXT H2 Logitech G500s 
CPUHard DriveHard DriveHard Drive
Phenom II x4 965 WE Red Seagate Intel SSD 
Hard DriveCoolingOSOS
WR Raptor  Silenx Fans x2 120mm Win 7 x2 Unraid 
OSCase
pfsense Rackmount 16 bay hot-swap 
CPUMotherboardGraphicsRAM
i3 2100t Gigabyte integrated 2x 8GB GSkill 
Hard DriveCoolingOSMonitor
OCZ SSD Stock Win7 - Plex Shell Sony XBR2 
PowerCase
Generic 350 watt Repurposed Vidabox 
  hide details  
Reply
 
ESXi Server
(10 items)
 
HTPC
(13 items)
 
CPUMotherboardGraphicsRAM
i7-4790K Asus Sabertooth Mark 2 Radeon HD 6950 GSkill Sniper Series 
Hard DriveCoolingOSMonitor
Samsung XP941 Noctura NH-D14 8.1 Pro QNIX 27", Dell 24", Samsung 22", Sony 24" 
KeyboardPowerCaseMouse
Corsair K95 PC&P 750 NZXT H2 Logitech G500s 
CPUHard DriveHard DriveHard Drive
Phenom II x4 965 WE Red Seagate Intel SSD 
Hard DriveCoolingOSOS
WR Raptor  Silenx Fans x2 120mm Win 7 x2 Unraid 
OSCase
pfsense Rackmount 16 bay hot-swap 
CPUMotherboardGraphicsRAM
i3 2100t Gigabyte integrated 2x 8GB GSkill 
Hard DriveCoolingOSMonitor
OCZ SSD Stock Win7 - Plex Shell Sony XBR2 
PowerCase
Generic 350 watt Repurposed Vidabox 
  hide details  
Reply
post #4 of 11
when you are in VS, and you are making a class, when you click 'add new item' you want to chose 'class' not 'web form' that is why all that stuff was populated fr you.
post #5 of 11
Thread Starter 
Quote:
Originally Posted by stupid View Post
when you are in VS, and you are making a class, when you click 'add new item' you want to chose 'class' not 'web form' that is why all that stuff was populated fr you.
oh, his instructions said to add a new web form. These are two separate pages so I figured I would do form and leave everything as default. . . . yor saying I should add a class then? . . how would it be referenced to the second page?
 
ESXi Server
(10 items)
 
HTPC
(13 items)
 
CPUMotherboardGraphicsRAM
i7-4790K Asus Sabertooth Mark 2 Radeon HD 6950 GSkill Sniper Series 
Hard DriveCoolingOSMonitor
Samsung XP941 Noctura NH-D14 8.1 Pro QNIX 27", Dell 24", Samsung 22", Sony 24" 
KeyboardPowerCaseMouse
Corsair K95 PC&P 750 NZXT H2 Logitech G500s 
CPUHard DriveHard DriveHard Drive
Phenom II x4 965 WE Red Seagate Intel SSD 
Hard DriveCoolingOSOS
WR Raptor  Silenx Fans x2 120mm Win 7 x2 Unraid 
OSCase
pfsense Rackmount 16 bay hot-swap 
CPUMotherboardGraphicsRAM
i3 2100t Gigabyte integrated 2x 8GB GSkill 
Hard DriveCoolingOSMonitor
OCZ SSD Stock Win7 - Plex Shell Sony XBR2 
PowerCase
Generic 350 watt Repurposed Vidabox 
  hide details  
Reply
 
ESXi Server
(10 items)
 
HTPC
(13 items)
 
CPUMotherboardGraphicsRAM
i7-4790K Asus Sabertooth Mark 2 Radeon HD 6950 GSkill Sniper Series 
Hard DriveCoolingOSMonitor
Samsung XP941 Noctura NH-D14 8.1 Pro QNIX 27", Dell 24", Samsung 22", Sony 24" 
KeyboardPowerCaseMouse
Corsair K95 PC&P 750 NZXT H2 Logitech G500s 
CPUHard DriveHard DriveHard Drive
Phenom II x4 965 WE Red Seagate Intel SSD 
Hard DriveCoolingOSOS
WR Raptor  Silenx Fans x2 120mm Win 7 x2 Unraid 
OSCase
pfsense Rackmount 16 bay hot-swap 
CPUMotherboardGraphicsRAM
i3 2100t Gigabyte integrated 2x 8GB GSkill 
Hard DriveCoolingOSMonitor
OCZ SSD Stock Win7 - Plex Shell Sony XBR2 
PowerCase
Generic 350 watt Repurposed Vidabox 
  hide details  
Reply
post #6 of 11
I must not be compltely understanding what you are looking for. It appears to me that 'enterData' is not a page at all, as it displays no data - it just seems to have an object and declare methods for it. It looks like all the work is done in 'DisplayData' - am I understanding correctly?

If you put your classes into the App_Code folder of your site, all of the files can reference them without any explicit declaration. You will even notice it shows up in your intelli-type in VS.

I uploaded a super-basic example of how to use a class with constructors and methods so you can look at it.

Please let me know what you don't understand.
post #7 of 11
Ok, now that i have had time ot look at the code I noticed quite a few things.

First off, your pages are not referenccing your codebehind pages at all. If you are using VS, and you choose 'add new item' , choose 'web form' and before you make the page, click 'place code in a seperate file' that will automatically link the two. If not, you will have to add a line like this:

Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EnterDataaspx.cs" Inherits="EnterData" %>
Ok, now we need to think of how we are going to pass the information. Since we are going to another page, we have to store it somewhere as html is stateless, and you are using .net controls and not a regular POST. So, we have 2 options:
1. store variables for each value
2. construct a single object and store that

I vote for 2, as it is cleaner, and you will learn more. So, next you need to make an object by right clicking in your solution explorer, clicking 'add new item' and choose 'class' - name it what you will, I chose 'employee'. (its going to ask you to put it in the app_code folder, you want to do this).

Now you need to define what the object has for properties, and how you are going to set and access them (set/get, but I don't like to use those terms on multi-property objects as I feel that can get confusing).

This is what I came up with:
Code:
//here you declare all of the properties that each 'employee' object will have
string name;
string id;
//constructor
public employee() { }
// this one actually builds an employee object - requires 2 strings as of now, but you can add more
public employee(string nameInput, string idInput) 
{ 
 name = nameInput; 
 id = idInput; 
}
// methods - returns what will be pulled from the employee obect here
public string showName() { return name; }
public string showId() { return id; }
With that you can see that an employee has both an ID and a Name property, they are both strings and are set by calling employee(name, id)

So now we have our object class... let's use it.

We have 2 textboxes, txtName and txtId, that will hold, you guessed it, name and id. I also added RequiredFieldValidators to try and make sure we get no errors, requiring the user to enter values before they submit.

Add a submit button and double click on it in design mode, that will auto-populate a Click event in your code behind to tell your page what to do when it is clicked. here you will make an empoyee object, and then set it to a session variable - then kick it to the display page:
Code:
protected void btnSubmit_Click(object sender, EventArgs e)
{//builds new employee object
  employee newEmployee = newemployee(txtName.Text, txtId.Text);  Session["employee"] = newEmployee;//puts it in session state memory
 Response.Redirect("DisplayData_Class.aspx");//sends you to the next page
}
Ok, that sent us to the next page, all we have to do is to pull the object out of memory, and push the values to the labels we have already made (lblName and lblId)

In the page_load event, we can run a quick check to see if there is a session variable set up, if not, it would error out otherwise:
Code:
protected void Page_Load(object sender, EventArgs e)
{
 //checks to make sure we have input, otherwise does nothing
 if (Session["employee"] != null)
 showEmployeeInfo();
}
And then we make the showEmployee() method by calling the methods we made in teh employee class we made earlier:
Code:
protected void showEmployeeInfo()
{
 employee passedEmployee = newemployee();//makes the new object
 //copies the one you just made
 passedEmployee = (employee)Session["employee"];//display info
 lblName.Text = passedEmployee.showName();
 lblId.Text = passedEmployee.showId();
}
And boom, you are all done. Now just go through and add the properties to the object you need and ways to access and set them and you will be done in no time.

Let me know if there is anything I need to make clearer - I enjoy this stuff... I want to be a professor in a few years.

EDIT: Keep in mind, this is far from the only way to do this, you can use viewstate, previouspage, querystrings or others. This is just the cleanest and best for this situation IMO.
post #8 of 11
Thread Starter 
Thank you sooo much, Im attaching my completed project to show what I was trying for . . well It's not as pretty as I want but I am sooo over it.

>> uploads are failing for some reason . . . Ill try again later
 
ESXi Server
(10 items)
 
HTPC
(13 items)
 
CPUMotherboardGraphicsRAM
i7-4790K Asus Sabertooth Mark 2 Radeon HD 6950 GSkill Sniper Series 
Hard DriveCoolingOSMonitor
Samsung XP941 Noctura NH-D14 8.1 Pro QNIX 27", Dell 24", Samsung 22", Sony 24" 
KeyboardPowerCaseMouse
Corsair K95 PC&P 750 NZXT H2 Logitech G500s 
CPUHard DriveHard DriveHard Drive
Phenom II x4 965 WE Red Seagate Intel SSD 
Hard DriveCoolingOSOS
WR Raptor  Silenx Fans x2 120mm Win 7 x2 Unraid 
OSCase
pfsense Rackmount 16 bay hot-swap 
CPUMotherboardGraphicsRAM
i3 2100t Gigabyte integrated 2x 8GB GSkill 
Hard DriveCoolingOSMonitor
OCZ SSD Stock Win7 - Plex Shell Sony XBR2 
PowerCase
Generic 350 watt Repurposed Vidabox 
  hide details  
Reply
 
ESXi Server
(10 items)
 
HTPC
(13 items)
 
CPUMotherboardGraphicsRAM
i7-4790K Asus Sabertooth Mark 2 Radeon HD 6950 GSkill Sniper Series 
Hard DriveCoolingOSMonitor
Samsung XP941 Noctura NH-D14 8.1 Pro QNIX 27", Dell 24", Samsung 22", Sony 24" 
KeyboardPowerCaseMouse
Corsair K95 PC&P 750 NZXT H2 Logitech G500s 
CPUHard DriveHard DriveHard Drive
Phenom II x4 965 WE Red Seagate Intel SSD 
Hard DriveCoolingOSOS
WR Raptor  Silenx Fans x2 120mm Win 7 x2 Unraid 
OSCase
pfsense Rackmount 16 bay hot-swap 
CPUMotherboardGraphicsRAM
i3 2100t Gigabyte integrated 2x 8GB GSkill 
Hard DriveCoolingOSMonitor
OCZ SSD Stock Win7 - Plex Shell Sony XBR2 
PowerCase
Generic 350 watt Repurposed Vidabox 
  hide details  
Reply
post #9 of 11
Glad to help, thanks for the REPs!
post #10 of 11
Thread Starter 
Quote:
Originally Posted by stupid View Post
Glad to help, thanks for the REPs!
anytime. . . Ill probably be seeking your help in the future so . . . lol

I'm doing a page now that has to have multi-views, panels, and a calender . . hope it'll go a little easier than the last. I stocked up on reference books so it shouldn't be as bad
 
ESXi Server
(10 items)
 
HTPC
(13 items)
 
CPUMotherboardGraphicsRAM
i7-4790K Asus Sabertooth Mark 2 Radeon HD 6950 GSkill Sniper Series 
Hard DriveCoolingOSMonitor
Samsung XP941 Noctura NH-D14 8.1 Pro QNIX 27", Dell 24", Samsung 22", Sony 24" 
KeyboardPowerCaseMouse
Corsair K95 PC&P 750 NZXT H2 Logitech G500s 
CPUHard DriveHard DriveHard Drive
Phenom II x4 965 WE Red Seagate Intel SSD 
Hard DriveCoolingOSOS
WR Raptor  Silenx Fans x2 120mm Win 7 x2 Unraid 
OSCase
pfsense Rackmount 16 bay hot-swap 
CPUMotherboardGraphicsRAM
i3 2100t Gigabyte integrated 2x 8GB GSkill 
Hard DriveCoolingOSMonitor
OCZ SSD Stock Win7 - Plex Shell Sony XBR2 
PowerCase
Generic 350 watt Repurposed Vidabox 
  hide details  
Reply
 
ESXi Server
(10 items)
 
HTPC
(13 items)
 
CPUMotherboardGraphicsRAM
i7-4790K Asus Sabertooth Mark 2 Radeon HD 6950 GSkill Sniper Series 
Hard DriveCoolingOSMonitor
Samsung XP941 Noctura NH-D14 8.1 Pro QNIX 27", Dell 24", Samsung 22", Sony 24" 
KeyboardPowerCaseMouse
Corsair K95 PC&P 750 NZXT H2 Logitech G500s 
CPUHard DriveHard DriveHard Drive
Phenom II x4 965 WE Red Seagate Intel SSD 
Hard DriveCoolingOSOS
WR Raptor  Silenx Fans x2 120mm Win 7 x2 Unraid 
OSCase
pfsense Rackmount 16 bay hot-swap 
CPUMotherboardGraphicsRAM
i3 2100t Gigabyte integrated 2x 8GB GSkill 
Hard DriveCoolingOSMonitor
OCZ SSD Stock Win7 - Plex Shell Sony XBR2 
PowerCase
Generic 350 watt Repurposed Vidabox 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Web Coding
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › ASP.Net cross-page referencing