关于动态写入html标签控件,大家都熟悉,这里就不再表述。本文讨论的重点是:如何动态写入服务器端控件,并且在页面PostBack到Server端时,在Server端来获取被动态写入的服务器端控件的各种属性。
这里,我来通过一个Demo来说明这个应用。
需求:
1. 用户在UI上输入一个数值(比如:5),系统动态为用户加载这个数值的Url Address输入域;
2. 用户输入的Url Address内容需要通过Url格式验证;
3. 用户提交输入内容后,系统给出提交的结果
设计如下:
1. Css样式设置:
以下为引用的内容:
<style type="text/css">
.item
{
margin:10px;
border-bottom:solid 1px #CCC;
}
.item2
{
margin:5px;
}
.input
{
width:200px;
}
</style>
2.前台页面代码:
<div>
<div class="item">
Please input a number:
<asp:TextBox runat="server" CssClass="item" ID="txtTextCount"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtTextCount" ValidationGroup="CreateTextBox" Display="Dynamic"
ErrorMessage="Required to input content!"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtTextCount" ValidationGroup="CreateTextBox" Display="Dynamic"
runat="server" ErrorMessage="Only number is valid!" ValidationExpression="^\d+$"></asp:RegularExpressionValidator>
<asp:Button runat="server" ID="btnCreate" Text="Create TextBox List" ValidationGroup="CreateTextBox"
onclick="btnCreate_Click" />
<asp:Button runat="server" ID="btnOK" Text="Get TextBox Content" ValidationGroup="ShowListContent"
onclick="btnOK_Click" />
</div>
<div runat="server" id="divControls" class="item"></div>
<div runat="server" id="divMessage">
</div>
</div>
2. 前台页面代码:
以下为引用的内容:
<div>
<div class="item">
Please input a number:
<asp:TextBox runat="server" CssClass="item" ID="txtTextCount"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtTextCount" ValidationGroup="CreateTextBox" Display="Dynamic"
ErrorMessage="Required to input content!"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtTextCount" ValidationGroup="CreateTextBox" Display="Dynamic"
runat="server" ErrorMessage="Only number is valid!" ValidationExpression="^\d+$"></asp:RegularExpressionValidator>
<asp:Button runat="server" ID="btnCreate" Text="Create TextBox List" ValidationGroup="CreateTextBox"
onclick="btnCreate_Click" />
<asp:Button runat="server" ID="btnOK" Text="Get TextBox Content" ValidationGroup="ShowListContent"
onclick="btnOK_Click" />
</div>
<div runat="server" id="divControls" class="item"></div>
<div runat="server" id="divMessage">
</div>
</div>
说明, 动态创建的TextBox们将装载到divControls中。
ASP.NET 动态写入服务器端控件
原文:http://www.jb51.net/article/17798.htm