Link to home
Start Free TrialLog in
Avatar of 66chawger
66chawgerFlag for United States of America

asked on

Access UserControl set() method from code behind

I have form1.aspx and code behind for this form (form1.aspx.cs). I have a UserControl (UserControl1.cs) that is built in a package dll that is included in the project. The DLL has been added as a reference.

What I am trying to do is access UserControl1's Set() method from within my code behind (form1.aspx.cs).

Here is an example of the set method in UserControl1.cs. FYI, I am not using the get() method at this time.

public string psField1
{
get { return __Field1; }
set { _Field1 = value; }
}

So, in form1's code behind, I assume I will have to instantiate UserControl1.cs first.

PackageControl.UserControl2.......

now I need the rest to access the set() method in UserControl1.cs to set

Avatar of McExp
McExp
Flag of United Kingdom of Great Britain and Northern Ireland image

Is the UserContol added to a page or is it Dynamically created.

If it's on a page you should already have a refernce to the control so you can call UserControl1.psField where UserControl1 is the ID of the control

If it has not been added to a page then yes you would have to create an instance of the control but accessing the property will be the same as above
ASKER CERTIFIED SOLUTION
Avatar of renjurdevan
renjurdevan
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 66chawger

ASKER

renjurdevan, thanks for the example.  I do have one more question.

My UserControl2  public stringw/ set method is below.  Now, looking at your example for .MyMethod(); as you indicated previously, how am I actually coding to set serverName? I know this sounds lame, please bare with me!

public string psServerName
{
get { return serverName; }
set { serverName = value; }
}
psServerName is a property of the Control so all you need do is call

//Setting the property
yourControl.psServerName = "ServerName";
//Getting the property
string serverName = yourControl.psServerName;
where yourControl is the refrence to the obj instatiated in the other posters answer
Thanks alot!  I feel really lame, this seems to obvious..LOL.  

McExp, I accepted the other posters response, but I want to acknowledge you also.  Can you only accept one response?
You can do somthing about awarding points for an assist, I've never asked a question so can't tell you how this is done.
Ok, receiving a build error:
 Cannot convert type 'System.Web.UI.Control' to 'PackageControl.UserControl2'  on the following:

obj = (UserControl2) LoadControl("UserControl2.cs")

I think this has something to do with UserControl2 being defined as a public class using System.Windows.Forms.UserControl