473,385 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to get a User Control's Property from a .aspx (Codebehind code

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
Nov 22 '05 #1
0 1913

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Amir Eshterayeh | last post by:
Dear Friends Hope things goes well to you. I have this problem. Would you please give me your solution? I want to change a property that I define on my user control in my web form that contains...
0
by: Colin | last post by:
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...
2
by: adiel | last post by:
Hello, I am trying to access the properties and methods from a user control within the code-behind file for a webform but I am receiving the message: Name 'MenuBar1' is not declared It does...
6
by: Nehal Shah | last post by:
I've read that in the Page Directive of an aspx page, changing the CodeBehind attribute to Src saves you from having to compile the page before refreshing. This is preferable in a large...
1
by: Kris van der Mast | last post by:
Hi, been a while since I posted a question myself instead of trying to help others out. I'm refactoring an existing web app that uses dynamic loading of user controls and a lot of...
12
by: Phil Certain | last post by:
Hi, I'm trying to do something very simple...or at least it should be. I have created a host page (gen.aspx) and a very simple user control (us.ascx). The corresponding code-behind files are...
4
by: louise raisbeck | last post by:
Resending this as own topic as didnt get answer from original. Would be grateful for a response from anyone that knows. Thanks. Hi there, I found your post really helpful..but i wondered if, once...
5
by: c676228 | last post by:
Hi, I guess I am confused. In aspx script, I mean (you won't use Codebehind="enrollinfo.aspx.vb", but mix code with html and code together) You can access user control's property directly. Since I...
2
by: | last post by:
I want to use codebehind to pass property values to a control that I've embedded INSIDE of a user control. In other words, let's say I have the following: MyPage.aspx ....with the following...
4
by: archana | last post by:
Hi all, i am having one user control. what i want is to add javascript which will gets called on button click of user control. but user control is not working if i add javascript in user...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.