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

How to pass data to user controls?

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.
Nov 18 '05 #1
9 2311
> 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
Nov 18 '05 #2
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.

"Frank Rizzo" <no****@nospam.com> wrote in message
news:#s**************@TK2MSFTNGP11.phx.gbl...
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.

Nov 18 '05 #3
Peter Rilling wrote:
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?

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

"Frank Rizzo" <no****@nospam.com> wrote in message
news:#s**************@TK2MSFTNGP11.phx.gbl...
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.


Nov 18 '05 #4
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.

--
-------------------------------------------------------
http://www.qnal.net/inkabletype

"Frank Rizzo" <no****@nospam.com> wrote in message
news:uB**************@TK2MSFTNGP12.phx.gbl...
Peter Rilling wrote:
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?

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

"Frank Rizzo" <no****@nospam.com> wrote in message
news:#s**************@TK2MSFTNGP11.phx.gbl...
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.


Nov 18 '05 #5
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

Nov 18 '05 #6
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
"Frank Rizzo" <no****@nospam.com> wrote in message
news:uB**************@TK2MSFTNGP12.phx.gbl...
Peter Rilling wrote:
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?

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

"Frank Rizzo" <no****@nospam.com> wrote in message
news:#s**************@TK2MSFTNGP11.phx.gbl...
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.


Nov 18 '05 #7
John Amick wrote:
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?


HTH,

John
"Frank Rizzo" <no****@nospam.com> wrote in message
news:uB**************@TK2MSFTNGP12.phx.gbl...
Peter Rilling wrote:
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?

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

"Frank Rizzo" <no****@nospam.com> wrote in message
news:#s**************@TK2MSFTNGP11.phx.gbl...
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.


Nov 18 '05 #8
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.
"Frank Rizzo" <no****@nospam.com> wrote in message
news:On**************@TK2MSFTNGP09.phx.gbl...
John Amick wrote:
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?


HTH,

John
"Frank Rizzo" <no****@nospam.com> wrote in message
news:uB**************@TK2MSFTNGP12.phx.gbl...
Peter Rilling wrote:

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?
You should at least have a protected variable in your code-behind that


is
named that same as the tag on the page.

"Frank Rizzo" <no****@nospam.com> wrote in message
news:#s**************@TK2MSFTNGP11.phx.gbl...
>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.


Nov 18 '05 #9
Peter Rilling wrote:
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.
Nov 18 '05 #10

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

Similar topics

2
by: Quarantine | last post by:
I am still a noob at the whole .NET, but I have a question and hope someone can help me out. I am taking user input, then want to query a database and return the results to either a datagrid, or a...
8
by: darrel | last post by:
I'm still trying to fully understand how best to pass variables between pages/usercontrols/each other. On a current site I've done, I've had one userControl do the logic and set the variable,...
6
by: DMUM via AccessMonster.com | last post by:
Hello I am trying to pass the name of my subform to a function/sub but I can't seem to get it to work. I am using an autokey function (ctrl E) to unlock text boxes on a subform. I have a few...
3
by: Phillip Vong | last post by:
I'm new and trying to learn. This is in VB.NET 2. I have a simple page with one Textbox1, one Label1 and one Button1. The on_click Button one goes and get's a simple data from the SQL db with...
2
by: simon | last post by:
Hello, relatively new .net programmer. using vb.net (v2.0) I have an aspx page that contains 2 user controls (ascx) on it. the left control is basically navigation hyperlinks the right control...
14
by: =?Utf-8?B?Umljaw==?= | last post by:
I have seen examples of passing data from FormB to FormA (Parent To Child) but I need an example of passind data from FormA to FormB. My main form is FormA; I will enter some data in textboxes and...
24
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
3
Frinavale
by: Frinavale | last post by:
I've created a few ASP.NET Ajax Enable Server controls. There are 2 components to these controls: a server side Object that deals with the server side stuffs, and a client side Object that deals...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.