Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What about ASCX controls 1

Status
Not open for further replies.

mooniron

Programmer
Dec 15, 2002
26
TR
Hi all. I have some problems with using jscript asp.net (c#) and ascx files. Firstly, I have created several ascx files and included them in my page. For example I created an ascx file to hold logged in session information. But I couldn't send information from this .ASCX file instance to the container ASPX file or other include .ASCX instances. How can I communicate theese controls.
Second, I want to use java script code but I don't know how to use that on a server side control. For example a Button control.
Thanks.
 
moon: haven't gotten around to using ascx files (outside of Visual Studio) - so can't offer much advice on that. Insofar as adding javascript to server side controls there must be a 100 examples here at Tek-Tips - the topic has been discussed in detail many times (do a quick Search here at Tek-Tips and see what you can find). Someone may drop in and help you out on your ascx problem.
 
Hi,

Since a user control (ascx) is a class and self-contained, the only way to communicate is to use properties.

If, for instance, you have a Textbox (txtLastName) on your form, write a property to access it.

[tt]
public string LastName
{
get
{
return txtLastName.Text;
}
set
{
txtLsatName.Text = value;
}
}
[/tt]

In your ASPX container, you need to add a declaration at class level in order to use it. If you user control is called MyControl and the id is uc1, you need to add the following:

[tt]
protected MyControl uc1;
[/tt]

Make sure it is "protected", not "private".

You can then use the following code:
[tt]
uc1.LastName = "Brown";
Label1.Text = uc1.LastName;
[/tt]

You can also write events and set up delegates in order to pass events througvh to your calling form (i.e. if you need to pass a DropDownList.SelectedIndex event into the containing form.

As long as you consider the user control as a seperate class, you'll be OK.

Hope this helps.
 
I tried this property way for several times but every time it returns the error something like:
Object reference not set for an instance etc...
although I defined the user control like

protected MyControl uc1 = new MyControl();

What do you think?
Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top