.net动态创建服务器端控件

public partial class 动态创建服务器端控件 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected override void OnInit(EventArgs e)
    {
        AddControl();
    }

    private void AddControl()
    {

        var label = new Label { ID = "myLabel", Text = "test_label" };
        label.EnableViewState = true;
        label.Style.Add("padding-left","320px");
        var button = new LinkButton { ID = "myButton", Text = "test_button" };
        button.EnableViewState = true;
        button.Click += button_Click;

        this.Form.Controls.Add(label);
        this.Form.Controls.Add(button);
    }

    protected void button_Click(object sender, EventArgs e)
    {
        var label = (Label)this.Form.FindControl("myLabel");
        label.Text = "test success!";
    }

}

你可能感兴趣的:(.net)