开发过程中经常碰到许多不确定事项,所以有时需要动态生成新的记录,如图所示,点击新增时新增一条参考记录,点击删除时则删除该记录:第一步,创建一个表格,用hidden记录当前最大行数,添加时则只需复制模板并修改ID(由于lable最终生成的html是span标签,不方便后台取值,所以换成了textbox)

<table class="table-bordered" style="width:100%;text-align:center">
<thead class="form-group">
<tr class="bg-primary">
<td>参考房源
</td>
<td>评估单价
</td>
<td>建筑面积
</td>
<td>评估总价=评估单价*建筑面积
</td>
<td>
<input type="button" id="btnAdd" class="btn-info" value="新增" onclick="addRow()" />
<asp:HiddenField ID="hidRows" runat="server" ClientIDMode="Static" Value="" />
</td>
</tr>
</thead>
<tbody id="tbBody" class="form-group">
<tr id="tr_0" style="display: none" class="bg-warning">
<td>
<asp:DropDownList runat="server" ID="drpHouseSource_0" ClientIDMode="Static" AppendDataBoundItems="true">
<asp:ListItem Value="">请选择</asp:ListItem>
<asp:ListItem Value="">搜房</asp:ListItem>
<asp:ListItem Value="">安居客</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox runat="server" ID="txtAvgPrice_0" ClientIDMode="Static" onchange="calaTotalPrice(this)"></asp:TextBox>
</td>
<td>
<asp:TextBox runat="server" ID="txtBuildingArea_0" ClientIDMode="Static" ReadOnly="true" BorderStyle="None" Text=""></asp:TextBox>
</td>
<td>
<asp:TextBox runat="server" ID="txtTotalPrice_0" ClientIDMode="Static" ReadOnly="true" BorderStyle="None"></asp:TextBox>
</td>
<td>
<input type="button" id="btnDelete_0" class="btn-danger" value="删除" onclick="deleteRow(this)" />
</td>
</tr>
</tbody>
<tfoot class="form-group">
<tr class="bg-danger">
<td>评估人员
</td>
<td>评估单价(最终)
</td>
<td>建筑面积(最终)
</td>
<td>评估总价(最终) = 评估单价(最终) * 建筑面积(最终)
</td>
<td></td>
</tr>
<tr class="bg-info">
<td>
<asp:Label runat="server" ID="lblAssessPerson" ClientIDMode="Static"></asp:Label>
</td>
<td>
<asp:TextBox runat="server" ID="txtFinalAvgPrice" ClientIDMode="Static" onchange="calaFinalTotalPrice(this)"></asp:TextBox>
</td>
<td>
<asp:Label runat="server" ID="lblFinalBuildingArea" ClientIDMode="Static" Text=""></asp:Label>
</td>
<td>
<asp:Label runat="server" ID="lblFinalTotalPrice" ClientIDMode="Static"></asp:Label>
</td>
<td></td>
</tr>
<tr class="bg-success">
<td>评估备注:
</td>
<td colspan="">
<asp:TextBox runat="server" ID="txtAssessRemark" ClientIDMode="Static" Width=""></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="">
<asp:Button runat="server" ID="btnSubmit" CssClass="btn-success" OnClick="btnSubmit_Click" Text="提交" />
</td>
</tr>
</tfoot>
</table>

第二步,复制模板行替换里面的编号并添加到末尾

     //添加一条新记录
function addRow() {
var currentRows = parseInt($("#hidRows").val()); //当前最大行数
var tempTr = $("#tr_0").html(); //模板行的html
var newTr = "<tr id=\"tr_" + (currentRows + ) + "\" class=\"bg-warning\"> " //需新增行的html
+ tempTr.replace(/_0/g, "_" + (currentRows + )) + "</tr>"; var tbody = $("#tbBody");
$(newTr).appendTo(tbody); //把需新增的行放到最后面
$("#hidRows").val(currentRows + ); //当前最大行加1
}

第三步,删除指定行

     //删除一条记录
function deleteRow(obj) {
var objId = $(obj).attr("id");
var objIndex = getIndexById(objId); var maxRow = $("#hidRows").val();
if (objIndex != maxRow) { //判断删除行是否为最后一行
var tbody = $("#tbBody");
tbody.children("tr").each(function () { //循环当前所有行
var currentId = $(this).attr("id");
var currentIndex = getIndexById(currentId); if (currentIndex > objIndex) { //比较当前行和需删除行的位置,如在之后,则id和name需前移
$(this).attr("id", currentId.replace(currentIndex, (currentIndex - ))); $(this).find("input").each(function () { //循环当前行里面所有input标签并前移一个位置
if ($(this).attr("name") != undefined)
$(this).attr("name", $(this).attr("name").replace(currentIndex, (currentIndex - )));
if ($(this).attr("id") != undefined)
$(this).attr("id", $(this).attr("id").replace(currentIndex, (currentIndex - )));
});
$(this).find("select").each(function () { //循环当前行里面所有select标签并前移一个位置
if ($(this).attr("name") != undefined)
$(this).attr("name", $(this).attr("name").replace(currentIndex, (currentIndex - )));
if ($(this).attr("id") != undefined)
$(this).attr("id", $(this).attr("id").replace(currentIndex, (currentIndex - )));
});
$(this).find("span").each(function () { //循环当前行里面所有span标签并前移一个位置
if ($(this).attr("name") != undefined)
$(this).attr("name", $(this).attr("name").replace(currentIndex, (currentIndex - )));
if ($(this).attr("id") != undefined)
$(this).attr("id", $(this).attr("id").replace(currentIndex, (currentIndex - )));
});
}
});
}
$("#tr_" + objIndex).remove(); //移除该行
$("#hidRows").val(maxRow - ); //最大行减1
}

第四步,添加一些自定义方法(如单价改变时自动计算总价)

       //根据ID获取当前所在行的位置
function getIndexById(objId) {
return objId.substring(objId.indexOf("_") + );
}
//单价变化时计算总价
function calaTotalPrice(obj) {
var objId = $(obj).attr("id");
var objIndex = getIndexById(objId);
var avgVal = $(obj).val();
var areaVal = $("#txtBuildingArea_" + objIndex).val(); $("#txtTotalPrice_" + objIndex).val(avgVal * areaVal);
}
//最终单价变化时计算最终总价
function calaFinalTotalPrice(obj) {
var avgVal = $(obj).val();
var areaVal = $("#lblFinalBuildingArea").text(); $("#lblFinalTotalPrice").text(avgVal * areaVal);
}

最后后台取值并保存

   /// <summary>
/// 根据控件name获取值
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
private string GetValue(string name)
{
return Request[name];
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
List<HouseAssess> houseList = null;
HouseAssess house = null;
int maxRows = Convert.ToInt32(hidRows.Value); //当前最大行
if (maxRows > ) //判断当前是否有记录
{
houseList = new List<HouseAssess>();
for (int i = ; i <= maxRows; i++) //循环取值
{
house = new HouseAssess();
house.HouseSource = GetValue("drpHouseSource_" + i);
house.AvgPrice = Convert.ToDecimal(GetValue("txtAvgPrice_" + i));
house.BuildingArea = Convert.ToDecimal(GetValue("txtBuildingArea_" + i));
house.TotalPrice = Convert.ToDecimal(GetValue("txtTotalPrice_" + i)); houseList.Add(house);
}
}
}

Webform动态创建删除行及后台取值的更多相关文章

  1. js从后台取值并绑定到元素上

    用ajax从后台取值不是什么有技术含量的活计,把后台取来的值绑定到Vue对象上也不算难,但每一次向后台拿数据的时候都得写上这么一段代码就十分痛苦了. 于是我写了这么一小段js代码,能够自己根据url去 ...

  2. 前台改变asp button控件的值,后台取值没有改变的问题

    前台: <asp:Button ID="btnEdit" Style="margin-left: 600px;" runat="server&q ...

  3. chrome 和IE 上传的文件,在net 后台取值Request.Form.Files[0].FileName 的不同

    chrome 和IE 上传的文件,在net 后台取值Request.Form.Files[0].FileName 的不同 chrome 获得的是不含路径的纯文件名 IE获得的是含路径的文件名

  4. JQuery动态添加控件并取值

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  5. Ext.form.ComboBox 后台取值 动态加载 ext5.0.0

    我用的extjs是5.0.0版本的. 请注意:如果这里没有的combobox相关内容,这里一定有. 开始的时候keyup事件取到的数据就是放不到ComboBox中,放全局变量也不好用.最后大神出手帮忙 ...

  6. omDialog设计造成控件无法后台取值

    http://ui.operamasks.org/website/homepage.html 使用服务端控件,前台进行赋值,但后台确无法取值. 不仅如此,如果里面放置了一个ASp:Button同样无法 ...

  7. .net TxetBox控件设置ReadOnly=True后台取值问题

    1.为TxetBox添加onfocus=this.blur()进行模拟 2.通过 Request.From["TextBox"].Trim()取值; 3.后台CS文件设置TextB ...

  8. asp.net textbox控件readonly为true时,后台取值的问题

    如题,在后台通过textbox.Text方式取值为空,不论你默认值是否是空,如想要获得,需通过request.Form[""]的方式.

  9. ASP.NET页面使用JQuery EasyUI生成Dialog后台取值为空

    原因: JQuery EasyUI生成Dialog后原来的文档结构发生了变化,原本在form里的内容被移动form外面,提交到后台后就没有办法取值了. 解决办法: 在生成Dialog后将它append ...

随机推荐

  1. Ubuntu12.10 下搭建基于KVM-QEMU的虚拟机环境(八)

    Libvirt 是用c写的一个管理虚拟机及其资源(如网络.存储和外设等)的工具库,它不仅支持KVM/QEMU,它还支持xen,Vmware,OpenVZ和VirtualBox等其他HyperVisor ...

  2. about flashback_transaction_query

    详见原文博客链接地址:about flashback_transaction_query

  3. Android与js交互实例

    Android 中可以通过webview来实现和js的交互,在程序中调用js代码,只需要将webview控件的支持js的属性设置为true Android(Java)与JavaScript(HTML) ...

  4. XAF-Domain Components 技术 使用接口来定义ORM业务对象

    一.简介 Domain Component组件技术,以下简称DC,是扩展自XPO的, 官方不建议新手使用DC. 如果你用过EF,XPO及类似的ORM,这是很容易理解的,DC是基于XPO的,只是原业定义 ...

  5. 百度地图API的自动定位和搜索功能(移动端)

    近期有个项目涉及到百度地图API,要求做到自动定位和搜索功能.煞费苦心的研究半天,终于能将两个功能合二为一,现将代码贴出来分享给大家,希望你们的砖搬得又快又好.注释不多,具体请参照:http://lb ...

  6. 使用gulp构建自动化工作流

    简单易用 高效构建 高质量的生态圈 可能很多人会说现在提gulp也太落后了吧,但我想说写点东西并不是为了讨论它是否过时,而是来帮助我们自己来记忆.整理和学习.任何工具,我需要,我才去使用它,正如此时我 ...

  7. H5与CS3权威下.18 and 19 选择器(1)

    18章.CSS3概述 1.从前端技术的角度把互联网的发展分为三个阶段: (1)web1.0:HTML和CSS. (2)web2.0:Ajax,Javascript/DOM/异步数据请求. (3)web ...

  8. [UWP小白日记-8]一些零碎的东西

    设置启动窗口大小 直接上代码了没什么好解释的了,既然能设置最小,那铁定就能设置最大 public MainPage() { //设定窗口启动显示大小 ApplicationView.Preferred ...

  9. 微信小程序前置课程:Flex 布局教程(一):语法篇

    原文:http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html?utm_source=tuicool 网页布局(layout)是CSS的一个重点 ...

  10. Token注解防止表单的重复提交

    注解的一些基础: 参见http://blog.csdn.net/duo2005duo/article/details/50505884和 http://blog.csdn.net/duo2005duo ...