如何从代码隐藏(C#)初始化Web用户控件的属性?

如何从代码隐藏(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 to Enable 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; }
}