How to pass data to user controls?

F

Frank Rizzo

I've got a number of user controls on the web page. How can I pass some
data to it? I don't see where the user control is instantiated in the
page code-behind page.

Thanks.
 
D

darrel

I've got a number of user controls on the web page. How can I pass some
data to it? I don't see where the user control is instantiated in the
page code-behind page.

I'm a complete newbie to .NET, but are you asking how to pass data via a
Query string of via it's parent aspx page? If so, I can maybe help with
that...

-Darrel
 
P

Peter Rilling

Do you mean passing information in the constructor? You cannot since
ASP.NET instantiates the control for you and binds the tag with some
code-behind variable.

You can however, use properties and methods to pass information in after it
is created, maybe in the Page_Load event. Or you can set attributes on the
page that get translated into property calls that ASP.NET sets.

You should at least have a protected variable in your code-behind that is
named that same as the tag on the page.
 
F

Frank Rizzo

Peter said:
Do you mean passing information in the constructor? You cannot since
ASP.NET instantiates the control for you and binds the tag with some
code-behind variable.

Either via the constructor or via properties. The problem is that the
UserControl that I placed on the page is not instantiated in the
code-behind file like a button control would be. So I am not sure about
how to even reference it.

For instance, the UserControl is called AddComments and my web page is
main.aspx.cs

I can't find anywhere in main.aspx.cs where AddComments is instantiated.
I can instantiate it manually like

AddComments oComment = new AddComments(...)
But ASP.NET doesn't seem to take into account anything I do with the
UserControl.

I guess, I need to have a reference to the instance of the UC that's in
use by ASP.NET.
You can however, use properties and methods to pass information in after it
is created, maybe in the Page_Load event. Or you can set attributes on the
page that get translated into property calls that ASP.NET sets.

Could you give me an example of this?
 
K

Kunal

You can manipulate your User Controls if you add them programatically. (Note
the code is for demo purposes only)

Lets say you create a User Control, lets call it "sc.ascx".
This control has a Label called Label1
Set the text to "SomeLabel".
Save the Control.
Don't drag it on to your web form.

Now open up a WebForm, and on the Page_Load , add the following code:

Control c = LoadControl("sc.ascx");
this.Controls.Add(c);
Label lbl = new Label();
lbl = (Label)c.FindControl("Label1");
lbl.Text = "SomeOtherLabel";

This will show your user Control on your web form when you run your web
application, with the text modified.

I don't know if this is your intent, however it is possible.
 
G

Guest

If I'm understanding you correctly, you need to do as suggested in Peter's note: declare a protected variable--e.g. protected withevents MyInstance as MyUserControlClass. Once you do that, from the page itself you can get at the properties, including the contained controls, etc

----- Frank Rizzo wrote: ----

I've got a number of user controls on the web page. How can I pass some
data to it? I don't see where the user control is instantiated in the
page code-behind page

Thanks
 
J

John Amick

If I understand you correctly, I believe all you need to do is declare the
control in your code-behind page. You will then be able to reference it in
code. Even though you've already added the control in design mode, you
should still be able to declare and reference it like this:

public class MyWebForm : System.Web.UI.Page
{
protected AddComments oAddComments;
...
oAddComments.MyProperty = "Something";
...
}

HTH,

John
 
F

Frank Rizzo

John said:
If I understand you correctly, I believe all you need to do is declare the
control in your code-behind page. You will then be able to reference it in
code. Even though you've already added the control in design mode, you
should still be able to declare and reference it like this:

public class MyWebForm : System.Web.UI.Page
{
protected AddComments oAddComments;
...
oAddComments.MyProperty = "Something";
...
}


Thanks, I managed to figure it out yesterday. I have to say that's
quite unintuitive, given that other controls on the form have code
generated for them.

Regardless, I am not quite understanding exactly how this works behind
the scenes. I mean, how does ASP.NET figure that this is the object
that it needs to work with?
 
P

Peter Rilling

There are two things required for the web page and the code-behind to
interact with each other.

1) The tag on the web page must have the runat="server" defined.
2) A variable in the code-behind must be defined, as a protected instance
member, with the same name as the ID in the web page. This is how ASP.NET
knows what variable to map to what tag.

Once the tag is mapped to a variable, then you can operate on the variable
with all actions affecting the tag when rendered.

If you add something using the designer, it should place the variable
reference for you in the code-behind. From my experience, the designer is a
bit flaky when it comes to this code generation. More often then not, I
have to define the variables myself, so knowing the interaction between the
page and the code-behind is more then academic.
 
F

Frank Rizzo

Peter said:
There are two things required for the web page and the code-behind to
interact with each other.

1) The tag on the web page must have the runat="server" defined.
2) A variable in the code-behind must be defined, as a protected instance
member, with the same name as the ID in the web page. This is how ASP.NET
knows what variable to map to what tag.

Once the tag is mapped to a variable, then you can operate on the variable
with all actions affecting the tag when rendered.

If you add something using the designer, it should place the variable
reference for you in the code-behind. From my experience, the designer is a
bit flaky when it comes to this code generation. More often then not, I
have to define the variables myself, so knowing the interaction between the
page and the code-behind is more then academic.

Thanks, I get it now.
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top