如何将数据字符串传递到 C# 中以编程方式加载的自定义用户控件

发布于 2024-07-09 09:39:56 字数 219 浏览 4 评论 0原文

我编写了一个自定义用户控件,它返回一些用户特定的数据。 要加载自定义用户控件,我使用以下代码行:

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

但是如何访问用户控件 myUC 内的 string user

I've written a Custom User Control which returns some user specific data.
To load the Custom User Control I use the following line of code:

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

But how can I access string user inside the User Control myUC?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光病人 2024-07-16 09:39:56

让我们称您的用户控件为“Bob”

如果您继承自Bob中的UserControl,那么我想这样做是安全的:

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

对于用户部分,我无法真正遵循您想要做的事情,类中的“用户”是您创建了“Bob”用户控件,并且想要在“Bob”用户控件中设置属性,还是相反?

对于第一个,您应该在用户控件中创建一个属性。

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

然后在创建“Bob”实例后设置它。

b.User = theuser;

Let's call your usercontrol "Bob"

If you Inherit from UserControl in Bob, then I guess it's safe to do this:

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

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

For the first one you should create a property in your usercontrol.

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

and then set it when after you create "Bob" instance.

b.User = theuser;
情绪操控生活 2024-07-16 09:39:56

假设您的自定义用户控件的名称是“MyUserControl”,

请尝试以下代码:

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

Suppose your custom user control's name is "MyUserControl"

Try this code:

MyUserControl myUC = (UserControl).Load("~/customUserControl.ascx") as MyUserControl;
string result = myUC.user;
临风闻羌笛 2024-07-16 09:39:56

您需要将加载的控件转换为实际类型并使用其公共属性:

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

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

MyUserControl myUCTyped = (MyUserControl)myUC;
myUCTyped.ThePublicProperty = "some value";
枕头说它不想醒 2024-07-16 09:39:56

到目前为止谢谢。 我的代码与上面编写的完全相同:

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
    }
}

但我无法在我的 Web 服务代码中创建 poll 实例。 我是不是忘记了什么?

--> 忘记将 Reference 添加到 .aspx 页面,其中用户控件是动态加载的。 现在可以使用普通的 .aspx 页面。
示例代码可以在 此处找到

你们中有人知道如何在 Web 服务中添加用户控件吗,因为那里没有引用属性?

Thanks so far. My code is excactly the same as written 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'm not able to create an Instance of poll, in my Web Service code. Do I forget something?

--> Forgot to add the Reference to the .aspx Page, where the user control is loaded dynamically. It's working now with normal .aspx Pages.
Example code can be found here

Does anyone of you know how user controls can be added in Web Services aswell, because the Reference Attribute isn't available there?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文