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

  • Thread starter =?ISO-8859-1?Q?Mika=EBl_PLOUHINEC?=
  • Start date
?

=?ISO-8859-1?Q?Mika=EBl_PLOUHINEC?=

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.
 
E

Eliyahu Goldin

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;}
}
 
S

Sean Chambers

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
 
?

=?ISO-8859-1?Q?Mika=EBl_PLOUHINEC?=

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 :
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top