473,406 Members | 2,273 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,406 software developers and data experts.

ASP.NET - C# How to customize user control?

Hello,

I work in a ASP.NET (C#) project. I would like to create an user control
with a label and a textBox.

My component myTextBox.ascx is present in my page (aspx) with
<toto:TextBoxPerso ID="TextBoxPerso1" runat="server" /><br />

So I would like to manage the component of my component for example in
the code of my aspx page.

I find I can do that with
((Label)(this.TextBoxPerso1.FindControl("label"))) .Text = "Label 2";

But is there a way to "publish" several properties of my component in
order to access to this like
this.TextBoxPerso1.getLabel().Text = "label 2";

Thanks a lot.

Mike.
Oct 25 '06 #1
3 1257
The easiest way to change controls values within your user controls is
set a public field within the usercontrol that modifies that field

i.e.:

public MyUserControl : Control {

public string LabelText {
get { return mylabel.Text; }
set { myLabel.Text = value; }
}
}

This is the accepted way of modifying values outside the class, this is
called encapsulation.

Hope that helps!

Sean

Mikaël PLOUHINEC wrote:
Hello,

I work in a ASP.NET (C#) project. I would like to create an user control
with a label and a textBox.

My component myTextBox.ascx is present in my page (aspx) with
<toto:TextBoxPerso ID="TextBoxPerso1" runat="server" /><br />

So I would like to manage the component of my component for example in
the code of my aspx page.

I find I can do that with
((Label)(this.TextBoxPerso1.FindControl("label"))) .Text = "Label 2";

But is there a way to "publish" several properties of my component in
order to access to this like
this.TextBoxPerso1.getLabel().Text = "label 2";

Thanks a lot.

Mike.
Oct 25 '06 #2
Mike,

You can add a Text property to the user control and set it as

this.TextBoxPerso1.Text = "label 2";

or

<toto:TextBoxPerso ID="TextBoxPerso1" runat="server" Text = "label 2" />

Just add the following code to the .ascx file:

public string Text
{
set {this.label.Text=value;}
}

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
"Mikaël PLOUHINEC" <mi**@cerealog.frwrote in message
news:OH****************@TK2MSFTNGP03.phx.gbl...
Hello,

I work in a ASP.NET (C#) project. I would like to create an user control
with a label and a textBox.

My component myTextBox.ascx is present in my page (aspx) with
<toto:TextBoxPerso ID="TextBoxPerso1" runat="server" /><br />

So I would like to manage the component of my component for example in the
code of my aspx page.

I find I can do that with
((Label)(this.TextBoxPerso1.FindControl("label"))) .Text = "Label 2";

But is there a way to "publish" several properties of my component in
order to access to this like
this.TextBoxPerso1.getLabel().Text = "label 2";

Thanks a lot.

Mike.

Oct 25 '06 #3
Thanks a lot.

It was a problem with Visual Studio which doesn't refresh the properties
available.

Although thanks for everybody.

Mike
Sean Chambers a écrit :
The easiest way to change controls values within your user controls is
set a public field within the usercontrol that modifies that field

i.e.:

public MyUserControl : Control {

public string LabelText {
get { return mylabel.Text; }
set { myLabel.Text = value; }
}
}

This is the accepted way of modifying values outside the class, this is
called encapsulation.

Hope that helps!

Sean

Mikaël PLOUHINEC wrote:
>Hello,

I work in a ASP.NET (C#) project. I would like to create an user control
with a label and a textBox.

My component myTextBox.ascx is present in my page (aspx) with
<toto:TextBoxPerso ID="TextBoxPerso1" runat="server" /><br />

So I would like to manage the component of my component for example in
the code of my aspx page.

I find I can do that with
((Label)(this.TextBoxPerso1.FindControl("label")) ).Text = "Label 2";

But is there a way to "publish" several properties of my component in
order to access to this like
this.TextBoxPerso1.getLabel().Text = "label 2";

Thanks a lot.

Mike.
Oct 26 '06 #4

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

Similar topics

4
by: balachandar | last post by:
hi, How to Customize Internet Explorer Menus Using JS and Any other Scripting Language? I am in need to protect my website from saving files from the file menu in IE and Printing too.Can...
14
by: deko | last post by:
The below code dials a phone number when the subform datasheet cell containing the number is double clicked. The problem is that the dialer application (c:\windows\dialer.exe) pops up windows on...
3
by: sachin | last post by:
How to customize PrintPreviewDialog, such that, addition of a new button, change the functionality of existing button should be possible programatically. Sample code will help a lot. Moreover, what...
0
by: Seko | last post by:
Hi! I have an application written in VC++ that uses wizards to get input from the user. ( the wizards are implemented by using CPropertySheet.SetWizardMode() ) My question is, how can I...
5
by: Sun Jian | last post by:
Hi, I am trying to customize the asp.net validation to achieve the following: Upon submitting the form, client side validation will run, and it will stop at the first error detected. For...
0
by: Mitkip | last post by:
Does someone know how can I make a user control which allow me to customize it like a datagrid or a repeater ... For example, I would like to call my control in an aspx page like this : <%@...
6
by: serge calderara | last post by:
Dear all, I was wondering is there is a way to customise the treView control in order to get any Control as a Node. What I try to do is having a Progress bar as a treeview node object. Is...
3
by: bala | last post by:
Hi Gurus The scenario A MS Access frontend application with Oracle Backend (Linked Tables). The Database UserID and password is not stored and each user has a unique UserID and password. There...
4
by: Michael | last post by:
I am considering building my WebForm page using Web Parts, so that my users can customize Web application's user interface. Is there any good information on it? or step by step demo?
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: 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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.