469,578 Members | 1,471 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,578 developers. It's quick & easy.

picking up value in code behind

Hi there, does anyone know if it is possible to set a value in a user
control and pick it up in the code behind.

I'm trying to reuse the same user control and code behind for multiple
pages but I don't want to use a querystring param. Thanks for
listening...Scott
Nov 17 '05 #1
3 1341
"Scott Collens" <sc**********@nospam.hotmail.com> wrote in message news:Xn**********************************@207.46.2 48.16...
Hi there, does anyone know if it is possible to set a value in a user
control and pick it up in the code behind.


Scott,
Create a public Property for the User Control. Then in the WebForm's code-behind,
call the User Control's Property to get the value.

--

Thanks,
Carl Prothman
Microsoft ASP.NET MVP
http://www.able-consulting.com

Nov 17 '05 #2
"Scott Collens" <sc**********@nospam.hotmail.com> wrote
"Carl Prothman [MVP]" <ca****@spamcop.net> wrote
"Scott Collens" <sc**********@nospam.hotmail.com> wrote
Hi there, does anyone know if it is possible to set a value in a user
control and pick it up in the code behind.


Create a public Property for the User Control. Then in the WebForm's
code-behind, call the User Control's Property to get the value.


Thanks for the response, but I'm a little confused as to how this is picked
up in the code behind. Can you provide an example? Thanks again....Scott


Scott,
In the User Control, create a public property

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

Then in the code behind of the WebForm (which contains the User Control)
create a protected member variable for the User Control. In this case,
the C# project is called "myProject" and the User Control is called
"UserControlPropertyDemo"

protected myProject.UserControlPropertyDemo UserControlPropertyDemo1;

Then in some event handler, get or set the User Control's public property value

// Set the property value
UserControlPropertyDemo1.Username = "CarlProthman";

// Get the property value
string username = UserControlPropertyDemo1.Username;

--

Thanks,
Carl Prothman
Microsoft ASP.NET MVP
http://www.able-consulting.com


Nov 17 '05 #3
Thanks Carl, I'll give it a try.
"Carl Prothman [MVP]" <ca****@spamcop.net> wrote in
news:OH**************@TK2MSFTNGP10.phx.gbl:
"Scott Collens" <sc**********@nospam.hotmail.com> wrote
"Carl Prothman [MVP]" <ca****@spamcop.net> wrote
> "Scott Collens" <sc**********@nospam.hotmail.com> wrote
> > Hi there, does anyone know if it is possible to set a value in a
> > user control and pick it up in the code behind.
>
> Create a public Property for the User Control. Then in the
> WebForm's code-behind, call the User Control's Property to get the
> value.


Thanks for the response, but I'm a little confused as to how this is
picked up in the code behind. Can you provide an example? Thanks
again....Scott


Scott,
In the User Control, create a public property

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

Then in the code behind of the WebForm (which contains the User
Control) create a protected member variable for the User Control. In
this case, the C# project is called "myProject" and the User Control
is called "UserControlPropertyDemo"

protected myProject.UserControlPropertyDemo
UserControlPropertyDemo1;

Then in some event handler, get or set the User Control's public
property value

// Set the property value
UserControlPropertyDemo1.Username = "CarlProthman";

// Get the property value
string username = UserControlPropertyDemo1.Username;

--

Thanks,
Carl Prothman
Microsoft ASP.NET MVP
http://www.able-consulting.com



Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Erik Max Francis | last post: by
2 posts views Thread by Steven K | last post: by
1 post views Thread by xenophon | last post: by
8 posts views Thread by Candace | last post: by
6 posts views Thread by Igor | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.