如何从代码隐藏(C#)初始化Web用户控件的属性? [英] How to initialize a Web User Control's property from code behind (C#) ?
本文介绍了如何从代码隐藏(C#)初始化Web用户控件的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好......
为什么代码没有在Page_Load中执行?
或
如何初始化Web用户控件的属性来自运行时的代码?
似乎当ASP.Net编译器看到public enum Status {Disable,Enable};时它一次初始化禁用引擎属性值。
任何想法?
Hi guys...
Why the code not execute in Page_Load ?
OR
How can i initialize a Web User Control's property from code behind at runtime ?
It seems that when ASP.Net compiler saw "public enum Status { Disable, Enable };" it initialize Disable value to Engine property at once .
any idea ?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
public partial class WebUserControl : System.Web.UI.UserControl
{
public enum Status { Disable, Enable };
protected void Page_Load(object sender, EventArgs e)
{
Engine = Status.Enable;
}
[Category("Vehicle"),DefaultValue(typeof(Status),"Disable")]
public Status Engine { set; get; }
}
推荐答案
嘿那里,
你可以从你使用此控件的地方将其设置为启用
,例如,
Hey there,
You can set it toEnable
from the place where you used this control, e.g,
<uc1:webusercontrol1 id="WebUserControl11" engine="Enable" runat="server" xmlns:uc1="#unknown" />
如果这不是你想要的,请告诉我。
希望它有所帮助。
Azee
Please let me know if this is not what you were looking for.
Hope it helps.
Azee
嗨...
我已经解决了这个问题。我们必须使用Page_Init事件。
Hi...
I've solved this problem. We have to use Page_Init event .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
public partial class WebUserControl : System.Web.UI.UserControl
{
public enum Status { Disable, Enable };
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_Init(object sender, EventArgs e)
{
Engine = Status.Enable;
}
[Category("Vehicle"),DefaultValue(typeof(Status),"Disable")]
public Status Engine { set; get; }
}
这篇关于如何从代码隐藏(C#)初始化Web用户控件的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文