Access public property from my ascx control !

M

Mick

Hi,
Dragging & Dropping my ascx control in my aspx page, I just wanna know how I
can access its properties when validating the form ?
The control is composed of 4 controls (2 txtbox and 2 dropdown : Address,
ZipCode, Country & City).

In fact, when, in my code, I try "this.MyControlId".... it does not exist.

Thks for help.
 
D

Dusan Zupancic

Hi Mick!

This is known thing in Visual Studio .NET.
But first: If you want to access control properties on ASCX, the best way is
to publish them as "public property" of ASCX class (basicaly you write a
wrapper around control properties).

When you drag your ASCX on ASPX form, Visual Studio .NET "forgets" to
declare instance variables in codebehind pages, so you have to do it "by
hand". If your ASCX id is "MyControlID", you write at codebehind class
something like:
protected MyAscxControl MyControlID

runtime environment of ASPX will coretly instatiate object, and now you can
use it in your code.

Dusan
 
M

MWells

Hi Mick,

Check your code behind, where the controls are defined as protected members
of your class. You probably need to add a line that declares your control
in your source code.

In C# this would look something like;

protected System.Web.UI.WebControls.Table tblRequest;
protected MyControl MyControl1;

That will make it possible to access your control's properties and methods
from your source.

Also, I'm fairly certain that you will only be able to access properties and
methods that you've defined. I don't recall a way to directly access the
embedded controls, though there might be a Controls collection that serves
this purpose. Otherwise you'll need to define properties and methods in
your User Control that provide access to the specific properties and methods
you want to expose. E.g.

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

public string Text2
{
get { return TextBox2.Text; }
set { TextBox2.Text = value; }
}

And so on...

/// M
 
M

Mick

Thks for help ^^

MWells said:
Hi Mick,

Check your code behind, where the controls are defined as protected members
of your class. You probably need to add a line that declares your control
in your source code.

In C# this would look something like;

protected System.Web.UI.WebControls.Table tblRequest;
protected MyControl MyControl1;

That will make it possible to access your control's properties and methods
from your source.

Also, I'm fairly certain that you will only be able to access properties and
methods that you've defined. I don't recall a way to directly access the
embedded controls, though there might be a Controls collection that serves
this purpose. Otherwise you'll need to define properties and methods in
your User Control that provide access to the specific properties and methods
you want to expose. E.g.

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

public string Text2
{
get { return TextBox2.Text; }
set { TextBox2.Text = value; }
}

And so on...

/// M


how
 

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

Top