How to pass a string of data to a programmatically loaded custom control in C #

I wrote a custom control that returns some custom data. I am using the following line of code to load the custom control:

UserControl myUC = (UserControl).Load("~/customUserControl.ascx");

      

But how can I access the string user

inside of User Control myUC

?

0


source to share


4 answers


Call your user control "Bob"

If you inherit UserControl in Bob, then I'm sure it's safe:

Bob b = (Bob).Load("~/customUserControl.ascx");

      

For the user part, I can't follow what you want to do, it is "user" in the class, you created a custom control "Bob" and you want to set a property in the custom control "Bob" or is it the other way around?



For the former, you have to create a property in your usercontrol.

class Bob : UserControl{
public string User { get; set;}
}

      

and then install it when after instantiating "Bob".

b.User = theuser;

      

+1


source


Let's assume your custom user control name is "MyUserControl"

Try this code:



MyUserControl myUC = (UserControl).Load("~/customUserControl.ascx") as MyUserControl;
string result = myUC.user;

      

+1


source


You will need to cast the loaded control down to the actual type and use its public property:

MyUserControl myUCTyped = (MyUserControl)myUC;
myUCTyped.ThePublicProperty = "some value";

      

0


source


Thank. My code looks the same as above:

public partial class todo : System.Web.UI.UserControl
{
    private string mysecondteststring;
    public string setValue
    {
        get
        {
            return mysecondteststring;
        }
        set
        {
            mysecondteststring = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        // my code
    }
}

      

But I am unable to instantiate the poll in my web service code. Did I forget something?

-> Forgot to add Reference

to the .aspx page where the custom control is loaded dynamically. It now works with regular .aspx Pages. Sample code can be found here

Does anyone of you know how custom controls can be added to web services since the reference attribute is not available there?

0


source







All Articles