Access UserControl set() method from code behind

Posted on 2007-10-10
Medium Priority
Last Modified: 2012-05-05
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.


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

Question by:66chawger
  • 4
  • 3
LVL 16

Expert Comment

ID: 20049304
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
LVL 12

Accepted Solution

renjurdevan earned 2000 total points
ID: 20049322
Control obj = new Control();

obj = (UserControl1)  LoadControl("UserControl1.ascx");

obj .MyMethod();

where "UserControl1.ascx" as usercontrol  and MyMethod is a method defined inside UserControl1.ascx"



Author Comment

ID: 20049468
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; }
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 16

Expert Comment

ID: 20049507
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;
LVL 16

Expert Comment

ID: 20049512
where yourControl is the refrence to the obj instatiated in the other posters answer

Author Comment

ID: 20049566
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?
LVL 16

Expert Comment

ID: 20049576
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.

Author Comment

ID: 20050102
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


Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.
Suggested Courses
Course of the Month13 days, 18 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question