Change the textbox value of a usercontrol from the principal page

N

nail

Hi.
So I have a default.aspx page and 3 WebUserControls
In the WebUserControl1, I have on TexBox, and in the default.aspx page I
have a Button control.
On the click event of the default.aspx page I need to change the
TextBox.Text value, but not user the FindControl Method because I think this
methos very slow.

So, I want this, but not with findcontrol:

void Button_Click(object sender, EventArgs e)
{
((TextBox)this.WebUserControl2_1.FindControl("TextBox1")).Text = "Principal
page button clicked";
}

Anybody has any idea how can I do that?
My boss said the findcontrol method use reflection and is too slow.
Understand now why I can't use findcontrol? :)

Thanks ind advance.

nail.
 
M

Marina

Add a public method to the usercontrol that sets its textbox's text to a
given value. Then call this method from your page and pass it the new text.
 
M

Mark Fitzpatrick

You can expose the textbox, or the Text value of the textbox as a property.

Try something like the following in the web user control

public string TheText
{
get{return TextBox1.Text;}
set{TextBox1.Text = value;}
}

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
N

nail

Ok Mark.
But if I have a lot of controls, I need to create a lot of properties.
Very hard to maintenance this, don't think?

It's an idea, but I guess my boss will discard too.
But thanks.
 
N

nail

Marina, I can't understand very well.
How did you imagine the method?

Thanks.

PS: Don't have any way like WebUserControl.Control??
 
M

Marina

Pretty much as the other user suggested with a property - since a property
is syntactic sugar for 2 methods.

If you want a generic solution, where you need to refer to each textbox by a
string - then I don't see any way other then to use reflection.

If you don't need to refer to the textbox by a string held in a variable,
you could make all the textboxes in the user control public, and then you
could access them directly.
 
N

nail

Marina, now I understant.
But is the same problem of Mark solution.

I need to do a public method for any control I put in the usercontrol.

Thanks.
 
M

Marina

The normal implications of being public - access to them is not restricted
to just within the user control.
 
N

nail

Thanks Marina.
I will see what my boss think about it.

PS: Do you know anything about a way to do it using ::, like
WebForm::WebUserControl.Control
Is it exists?
 

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

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top