Hi there, I really need your help on this. I'm trying to learn to using the
VS.2003 to create a User Control. In my aspx code has no problem to use the
property "grossWaye" that has "register" in the aspx code if I don't use
"Codebehind " directive. If I try to move the <script></script> coding the
Codebehind of the VS2003. I don't know how to get the property "grossWaye" . I
post both User Control and .aspx codes for your help. Thanks in advance!
User Control(UserCon trolPayroll.asc x):
aspx code:
namespace MyDotNet.UserCo ntrol
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
/// <summary>
/// Summary description for UserContriolPay roll.
/// </summary>
public class UserContriolPay roll : System.Web.UI.U serControl
{
protected System.Web.UI.W ebControls.Text Box txtH;
protected System.Web.UI.W ebControls.Text Box txtR;
protected System.Web.UI.W ebControls.Butt on btnCompute;
protected System.Web.UI.W ebControls.Labe l lblPayMsg;
protected System.Web.UI.W ebControls.Labe l lblPay;
protected System.Web.UI.W ebControls.Labe l lblTitle;
private void Page_Load(objec t sender, System.EventArg s e)
{
// Put user code to initialize the page here
}
public string Title
{
get
{
return lblTitle.Text;
}
set
{
lblTitle.Text = value;
}
}
private double grWage;
public double grossWage
{
get
{
return double.Parse(lb lPay.Text);
}
}
protected void companyPay(obje ct obj, EventArgs e)
{
double h, r, g;
h = double.Parse(tx tH.Text);
r =double.Parse(t xtR.Text);
lblPayMsg.Text = "Your Gross Wag is:";
g = h * r;
lblPay.Text = g.ToString();
grWage = g;
}
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeCompo nent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
}
}
=============== =========
aspx file to use abvoe Control(UsingUs erControlPayrol l.aspx):
1) aspx code: %@ Page language="c#"
Codebehind="Usi ngUserControlPa yroll.aspx.cs" AutoEventWireup ="false"
Inherits="MyDot Net.UserControl .UserControlPay roll" %>
<%@ Register TagPrefix="user CtrlPayroll" TagName="payrol l"
Src="UserContro lPayroll.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Using User Control Payroll</title>
<meta content="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
<meta content="C#" name="CODE_LANG UAGE">
<meta content="JavaSc ript" name="vs_defaul tClientScript">
<meta content="http://schemas.microso ft.com/intellisense/ie5"
name="vs_target Schema">
</HEAD>
<body MS_POSITIONING= "GridLayout ">
<form id="Form1" method="post" runat="server">
Hello there, here we are in our main page. Now, let us instantaite the
payroll
user control<br>
<userctrlpayrol l:payroll id="usrPayCtrl " title="Concord Idea"
runat="server"> </userctrlpayroll :payroll><br>
<asp:button id="btnShowTax " style="Z-INDEX: 101; LEFT: 16px; POSITION:
absolute; TOP: 288px"
onclick="comput eTax" runat="server" Text="Show
Tax"></asp:button><asp :label id="lblTaxMsg" style="Z-INDEX: 102; LEFT: 16px;
POSITION: absolute; TOP: 328px"
runat="server" Width="160px"></asp:label><asp: label id="lblTax"
style="Z-INDEX: 103; LEFT: 200px; POSITION: absolute; TOP: 328px"
runat="server"
Width="200px"></asp:label></form>
</body>
</HTML>
2) Codebehind C# code:
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
namespace MyDotNet.UserCo ntrol
{
/// <summary>
/// Summary description for UserControlPayr oll.
/// </summary>
public class UserControlPayr oll : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Labe l lblTaxMsg;
protected System.Web.UI.W ebControls.Labe l lblTax;
protected System.Web.UI.W ebControls.Butt on btnShowTax;
private void Page_Load(objec t obj, System.EventArg s e)
{
// Put user code to initialize the page here
}
protected void computeTax(obje ct obj, System.EventArg s e)
{
double t, gWage;
gWage = usrPayCtrl.gros sWage;
t = gWage * 0.10;
lblTaxMsg.Text = "Your Tax is :";
lblTax.Text = t.ToString("c") ;
}
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeCompo nent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
}
}
--
Colin
VS.2003 to create a User Control. In my aspx code has no problem to use the
property "grossWaye" that has "register" in the aspx code if I don't use
"Codebehind " directive. If I try to move the <script></script> coding the
Codebehind of the VS2003. I don't know how to get the property "grossWaye" . I
post both User Control and .aspx codes for your help. Thanks in advance!
User Control(UserCon trolPayroll.asc x):
aspx code:
namespace MyDotNet.UserCo ntrol
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
/// <summary>
/// Summary description for UserContriolPay roll.
/// </summary>
public class UserContriolPay roll : System.Web.UI.U serControl
{
protected System.Web.UI.W ebControls.Text Box txtH;
protected System.Web.UI.W ebControls.Text Box txtR;
protected System.Web.UI.W ebControls.Butt on btnCompute;
protected System.Web.UI.W ebControls.Labe l lblPayMsg;
protected System.Web.UI.W ebControls.Labe l lblPay;
protected System.Web.UI.W ebControls.Labe l lblTitle;
private void Page_Load(objec t sender, System.EventArg s e)
{
// Put user code to initialize the page here
}
public string Title
{
get
{
return lblTitle.Text;
}
set
{
lblTitle.Text = value;
}
}
private double grWage;
public double grossWage
{
get
{
return double.Parse(lb lPay.Text);
}
}
protected void companyPay(obje ct obj, EventArgs e)
{
double h, r, g;
h = double.Parse(tx tH.Text);
r =double.Parse(t xtR.Text);
lblPayMsg.Text = "Your Gross Wag is:";
g = h * r;
lblPay.Text = g.ToString();
grWage = g;
}
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeCompo nent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
}
}
=============== =========
aspx file to use abvoe Control(UsingUs erControlPayrol l.aspx):
1) aspx code: %@ Page language="c#"
Codebehind="Usi ngUserControlPa yroll.aspx.cs" AutoEventWireup ="false"
Inherits="MyDot Net.UserControl .UserControlPay roll" %>
<%@ Register TagPrefix="user CtrlPayroll" TagName="payrol l"
Src="UserContro lPayroll.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Using User Control Payroll</title>
<meta content="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
<meta content="C#" name="CODE_LANG UAGE">
<meta content="JavaSc ript" name="vs_defaul tClientScript">
<meta content="http://schemas.microso ft.com/intellisense/ie5"
name="vs_target Schema">
</HEAD>
<body MS_POSITIONING= "GridLayout ">
<form id="Form1" method="post" runat="server">
Hello there, here we are in our main page. Now, let us instantaite the
payroll
user control<br>
<userctrlpayrol l:payroll id="usrPayCtrl " title="Concord Idea"
runat="server"> </userctrlpayroll :payroll><br>
<asp:button id="btnShowTax " style="Z-INDEX: 101; LEFT: 16px; POSITION:
absolute; TOP: 288px"
onclick="comput eTax" runat="server" Text="Show
Tax"></asp:button><asp :label id="lblTaxMsg" style="Z-INDEX: 102; LEFT: 16px;
POSITION: absolute; TOP: 328px"
runat="server" Width="160px"></asp:label><asp: label id="lblTax"
style="Z-INDEX: 103; LEFT: 200px; POSITION: absolute; TOP: 328px"
runat="server"
Width="200px"></asp:label></form>
</body>
</HTML>
2) Codebehind C# code:
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
namespace MyDotNet.UserCo ntrol
{
/// <summary>
/// Summary description for UserControlPayr oll.
/// </summary>
public class UserControlPayr oll : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Labe l lblTaxMsg;
protected System.Web.UI.W ebControls.Labe l lblTax;
protected System.Web.UI.W ebControls.Butt on btnShowTax;
private void Page_Load(objec t obj, System.EventArg s e)
{
// Put user code to initialize the page here
}
protected void computeTax(obje ct obj, System.EventArg s e)
{
double t, gWage;
gWage = usrPayCtrl.gros sWage;
t = gWage * 0.10;
lblTaxMsg.Text = "Your Tax is :";
lblTax.Text = t.ToString("c") ;
}
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeCompo nent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
}
}
--
Colin