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(UserControlPayroll.ascx):
aspx code:
namespace MyDotNet.UserControl
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for UserContriolPayroll.
/// </summary>
public class UserContriolPayroll : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.TextBox txtH;
protected System.Web.UI.WebControls.TextBox txtR;
protected System.Web.UI.WebControls.Button btnCompute;
protected System.Web.UI.WebControls.Label lblPayMsg;
protected System.Web.UI.WebControls.Label lblPay;
protected System.Web.UI.WebControls.Label lblTitle;
private void Page_Load(object sender, System.EventArgs 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(lblPay.Text);
}
}
protected void companyPay(object obj, EventArgs e)
{
double h, r, g;
h = double.Parse(txtH.Text);
r =double.Parse(txtR.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(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
========================
aspx file to use abvoe Control(UsingUserControlPayroll.aspx):
1) aspx code: %@ Page language="c#"
Codebehind="UsingUserControlPayroll.aspx.cs" AutoEventWireup="false"
Inherits="MyDotNet.UserControl.UserControlPayroll" %>
<%@ Register TagPrefix="userCtrlPayroll" TagName="payroll"
Src="UserControlPayroll.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Using User Control Payroll</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</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>
<userctrlpayroll: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="computeTax" 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.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace MyDotNet.UserControl
{
/// <summary>
/// Summary description for UserControlPayroll.
/// </summary>
public class UserControlPayroll : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblTaxMsg;
protected System.Web.UI.WebControls.Label lblTax;
protected System.Web.UI.WebControls.Button btnShowTax;
private void Page_Load(object obj, System.EventArgs e)
{
// Put user code to initialize the page here
}
protected void computeTax(object obj, System.EventArgs e)
{
double t, gWage;
gWage = usrPayCtrl.grossWage;
t = gWage * 0.10;
lblTaxMsg.Text = "Your Tax is :";
lblTax.Text = t.ToString("c");
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
--
Colin