C# 在动态添加的 UserControl 上设置属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/508826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 06:11:45  来源:igfitidea点击:

Set properties on dynamically added UserControl

c#.netasp.netuser-controls.net-2.0

提问by roman m

I'm dynamically adding a custom user control to the page as per this post.

我正在根据这篇文章动态地向页面添加一个自定义用户控件。

However, i need to set a property on the user control as i add it to the page. How do I do that?

但是,当我将它添加到页面时,我需要在用户控件上设置一个属性。我怎么做?

Code snippet would be nice :)

代码片段会很好:)

Details:

细节:

I have a custom user control with a public field (property ... e.g. public int someId;)

我有一个带有公共字段的自定义用户控件(属性...例如 public int someId;)

As I add a UserControl to a page, its type is UserControl. Do i just cast it into MyCustomUCType and set a property on a cast control?

当我向页面添加 UserControl 时,它的类型是 UserControl。我是否只是将其转换为 MyCustomUCType 并在转换控件上设置属性?

p.s. looks like I've answered my own question after all.

ps 看起来我毕竟已经回答了我自己的问题。

采纳答案by Jeromy Irvine

Ah, I answered before you added the additional clarification. The short answer is, yes, just cast it as your custom type.

啊,在你补充说明之前我已经回答了。简短的回答是,是的,只需将其转换为您的自定义类型即可。

I'll leave the rest of my answer here for reference, though it doesn't appear you'll need it.

我会将我的其余答案留在这里以供参考,尽管您似乎不需要它。



Borrowing from the code in the other question, and assuming that all your User Controls can be made to inherit from the same base class, you could do this.

借用另一个问题中的代码,并假设您的所有用户控件都可以从同一个基类继承,您可以这样做。

Create a new class to act as the base control:

创建一个新类作为基础控件:

public class MyBaseControl : System.Web.UI.UserControl
{
    public string MyProperty 
    {
        get { return ViewState["MyProp"] as string; }
        set { ViewState["MyProp"] = value; }
    }
}

Then update your user controls to inherit from your base class instead of UserControl:

然后更新您的用户控件以从您的基类而不是 UserControl 继承:

public partial class SampleControl2 : MyBaseControl
{
    ....

Then, in the place where you load the controls, change this:

然后,在加载控件的地方,更改以下内容:

UserControl uc = (UserControl)LoadControl(controlPath);
PlaceHolder1.Controls.Add(uc);

to:

到:

MyBaseControl uc = (MyBaseControl)LoadControl(controlPath);
uc.MyProperty = "foo";
PlaceHolder1.Controls.Add(uc);

回答by Jim Petkus

Yes, you just cast the control to the proper type. EX:

是的,您只需将控件转换为正确的类型。前任:

((MyControl)control).MyProperty = "blah";

回答by Tom Ritter

"As I add a UserControl to a page, its type is UserControl. Do i just cast it into MyCustomUCType and set a property on a cast control?"

“当我向页面添加 UserControl 时,它的类型是 UserControl。我是否只是将其转换为 MyCustomUCType 并在转换控件上设置属性?”

That's what I'd do. If you're actually using the example code where it loads different controls, I'd use an if(x is ControlType) as well.

这就是我要做的。如果您实际使用加载不同控件的示例代码,我也会使用 if(x is ControlType) 。

if(x is Label)
{    
     Label l = x as Label;
     l.Text = "Batman!";
}
else
     //...
if(x is Label)
{    
     Label l = x as Label;
     l.Text = "Batman!";
}
else
     //...

Edit: Now it's 2.0 compatible

编辑:现在它与 2.0 兼容