|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Web Coding | |
ASP.Net cross-page referencing
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | ||||||||||||
|
WaterCooler
|
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: PHP Code:
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;
}
}
}
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;
}
}
__________________
XPS M1330: T7500, 4GB, 13.3" LED WXGA, 128 8400 GS, 120GB 5400RPM, 802.11 AGN, 9 cell, Fingerprint, Ultimate x64, 4 Year Accidental Damage/lojack/warranty. __________________
|
||||||||||||
|
|
|
|
#2 (permalink) | ||||||||||||
|
Programmer
|
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.
__________________
"If there is a god, I hope he has a good excuse" Woody Allen
Last edited by stupid : 01-27-08 at 09:07 AM. |
||||||||||||
|
|
|
|
|
#3 (permalink) | |||||||||||||
|
WaterCooler
|
Quote:
Ill PM you in a sec. . . thanks for the help . . other are welcome to add ![]()
__________________
XPS M1330: T7500, 4GB, 13.3" LED WXGA, 128 8400 GS, 120GB 5400RPM, 802.11 AGN, 9 cell, Fingerprint, Ultimate x64, 4 Year Accidental Damage/lojack/warranty. __________________
|
|||||||||||||
|
|
|
|
#4 (permalink) | ||||||||||||
|
Programmer
|
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.
__________________
"If there is a god, I hope he has a good excuse" Woody Allen
|
||||||||||||
|
|
|
|
|
#5 (permalink) | ||||||||||||
|
WaterCooler
|
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?
__________________
XPS M1330: T7500, 4GB, 13.3" LED WXGA, 128 8400 GS, 120GB 5400RPM, 802.11 AGN, 9 cell, Fingerprint, Ultimate x64, 4 Year Accidental Damage/lojack/warranty. __________________
|
||||||||||||
|
|
|
|
#6 (permalink) | ||||||||||||
|
Programmer
|
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.
__________________
"If there is a god, I hope he has a good excuse" Woody Allen
|
||||||||||||
|
|
|
|
|
#7 (permalink) | ||||||||||||
|
Programmer
|
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" %> 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; }
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
}
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();
}
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();
}
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.
__________________
"If there is a god, I hope he has a good excuse" Woody Allen
Last edited by stupid : 01-28-08 at 11:23 AM. |
||||||||||||
|
|
|
|
|
#8 (permalink) | ||||||||||||
|
WaterCooler
|
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
__________________
XPS M1330: T7500, 4GB, 13.3" LED WXGA, 128 8400 GS, 120GB 5400RPM, 802.11 AGN, 9 cell, Fingerprint, Ultimate x64, 4 Year Accidental Damage/lojack/warranty. __________________
|
||||||||||||
|
|
|
|
#9 (permalink) | ||||||||||||
|
Programmer
|
Glad to help, thanks for the REPs!
__________________
"If there is a god, I hope he has a good excuse" Woody Allen
|
||||||||||||
|
|
|
|
|
#10 (permalink) | ||||||||||||
|
WaterCooler
|
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
__________________
XPS M1330: T7500, 4GB, 13.3" LED WXGA, 128 8400 GS, 120GB 5400RPM, 802.11 AGN, 9 cell, Fingerprint, Ultimate x64, 4 Year Accidental Damage/lojack/warranty. __________________
|
||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|