ASP.NET 动态写入服务器端控件第1/2页
程序员文章站
2024-03-09 15:28:53
关于动态写入html标签控件,大家都熟悉,这里就不再表述。本文讨论的重点是:如何动态写入服务器端控件,并且在页面postback到server端时,在server端来获取被...
关于动态写入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中。
这里,我来通过一个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中。
1