ASP.NET tips: Golden rules for Dynamic Controls.
2012-02-22 01:26
344 查看
1. Make sure your dynamic controls are Loaded on every postback.
Lets play with a very simple example,
ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
C# Code Behind
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox t = new TextBox();
t.ID = "textBox";
this.PlaceHolder1.Controls.Add(t);
}
}
The above code works fine, but a common mistake is to try to conditionally load dynamic controls, if we tweak the code a little bit you will notice we loose our TextBox after any postback. The following code will not load the TextBox after our first postback.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox t = new TextBox();
t.ID = "textBox";
this.PlaceHolder1.Controls.Add(t);
}
}
}
Its recommended to load the dynamic controls during the Page_Init instead, because we may want to hook up our events with proper handler at an early stage.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
TextBox t = new TextBox();
t.ID = "textBox";
t.TextChanged+=new EventHandler(t_TextChanged);
this.PlaceHolder1.Controls.Add(t);
}
}
2. Do not assigning properties of a dynamic control (viewstate enabled), during Page_Init, it will not be reflected.
Here is scenario of another common mistake, "123" assigned to the Text property during Page_Init,
public partial class _Default : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
TextBox t = new TextBox();
t.ID = "textBox";
t.Text = "123";
this.PlaceHolder1.Controls.Add(t);
}
}
the above code will not work because, Initialization happens before LoadViewState during the control lifecycle. The value assigned to the properties during Initialization will simply get overwritten by the ViewState values.
3. If you are expecting your ViewState to retain after the postback, always assign same ID to the dynamic control
The following piece of code will not work, as I am assigning a new ID to the dynamic control after each postback. The LoadViewState retrieves previously saved viewstate data using the control ID, as the control ID has changed, it doesn't know anymore what to
load, as a result it cannot load previously saved viewstate data any more.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
TextBox t = new TextBox();
t.ID = Guid.NewGuid().ToString();
this.form1.Controls.Add(t);
}
}
Thank you for being with me so far.
Lets play with a very simple example,
ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
C# Code Behind
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox t = new TextBox();
t.ID = "textBox";
this.PlaceHolder1.Controls.Add(t);
}
}
The above code works fine, but a common mistake is to try to conditionally load dynamic controls, if we tweak the code a little bit you will notice we loose our TextBox after any postback. The following code will not load the TextBox after our first postback.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox t = new TextBox();
t.ID = "textBox";
this.PlaceHolder1.Controls.Add(t);
}
}
}
Its recommended to load the dynamic controls during the Page_Init instead, because we may want to hook up our events with proper handler at an early stage.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
TextBox t = new TextBox();
t.ID = "textBox";
t.TextChanged+=new EventHandler(t_TextChanged);
this.PlaceHolder1.Controls.Add(t);
}
}
2. Do not assigning properties of a dynamic control (viewstate enabled), during Page_Init, it will not be reflected.
Here is scenario of another common mistake, "123" assigned to the Text property during Page_Init,
public partial class _Default : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
TextBox t = new TextBox();
t.ID = "textBox";
t.Text = "123";
this.PlaceHolder1.Controls.Add(t);
}
}
the above code will not work because, Initialization happens before LoadViewState during the control lifecycle. The value assigned to the properties during Initialization will simply get overwritten by the ViewState values.
3. If you are expecting your ViewState to retain after the postback, always assign same ID to the dynamic control
The following piece of code will not work, as I am assigning a new ID to the dynamic control after each postback. The LoadViewState retrieves previously saved viewstate data using the control ID, as the control ID has changed, it doesn't know anymore what to
load, as a result it cannot load previously saved viewstate data any more.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
TextBox t = new TextBox();
t.ID = Guid.NewGuid().ToString();
this.form1.Controls.Add(t);
}
}
Thank you for being with me so far.
相关文章推荐
- ASP.NET tips: Golden rules for Dynamic Controls.
- Tips for writing ASP.NET composite controls
- RadControls for ASP.NET Ajax 笔记(2)
- Tips for ASP.NET Application Performance Enhancement
- ASP.NET Validation Controls – Important Points, Tips and Tricks
- 一个更炫的动态加权限管理的导航条(Telerik.RadControls.for.ASP.NET.AJAX.v2008.Q1.SP2.Retail-SSG)
- Some tips for ASP.NET Visual Studio 2003 project porting to Visual Studio 2005
- web developer tips (61):如何创建Asp.net Dynamic Data网站
- [收藏]Dynamic Controls in ASP.NET
- web developer tips (61):如何创建Asp.net Dynamic Data网站
- Telerik RadControls for ASP.NET AJAX 2010 Q2最新版下载+源码下载
- Server Controls for ASP.NET
- Creating a generic Web Parts for hosting ASP.NET User Controls
- Janus Web/ASP.NET Server Controls v3(3.5.0.35) for VS2005
- Top Ten Tips for Programming ASP.NET
- [.NET控件]Telerik RadControls for ASP.NET AJAX 2008 Q1 net 2.0 Web.UI
- Neodynamic Barcode Professional for ASP.NET是最灵活和强大的.NET组件
- DynamicData for Asp.net Mvc留言本实例 上篇 准备工作及显示文章列表
- Using the ASP.Net Runtime for extending desktop applications with dynamic HTML Scripts
- RadControls for ASP.NET AJAX 2009.1.402