Webform动态创建删除行及后台取值
开发过程中经常碰到许多不确定事项,所以有时需要动态生成新的记录,如图所示,点击新增时新增一条参考记录,点击删除时则删除该记录:
第一步,创建一个表格,用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动态创建删除行及后台取值的更多相关文章
- js从后台取值并绑定到元素上
用ajax从后台取值不是什么有技术含量的活计,把后台取来的值绑定到Vue对象上也不算难,但每一次向后台拿数据的时候都得写上这么一段代码就十分痛苦了. 于是我写了这么一小段js代码,能够自己根据url去 ...
- 前台改变asp button控件的值,后台取值没有改变的问题
前台: <asp:Button ID="btnEdit" Style="margin-left: 600px;" runat="server&q ...
- chrome 和IE 上传的文件,在net 后台取值Request.Form.Files[0].FileName 的不同
chrome 和IE 上传的文件,在net 后台取值Request.Form.Files[0].FileName 的不同 chrome 获得的是不含路径的纯文件名 IE获得的是含路径的文件名
- JQuery动态添加控件并取值
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- Ext.form.ComboBox 后台取值 动态加载 ext5.0.0
我用的extjs是5.0.0版本的. 请注意:如果这里没有的combobox相关内容,这里一定有. 开始的时候keyup事件取到的数据就是放不到ComboBox中,放全局变量也不好用.最后大神出手帮忙 ...
- omDialog设计造成控件无法后台取值
http://ui.operamasks.org/website/homepage.html 使用服务端控件,前台进行赋值,但后台确无法取值. 不仅如此,如果里面放置了一个ASp:Button同样无法 ...
- .net TxetBox控件设置ReadOnly=True后台取值问题
1.为TxetBox添加onfocus=this.blur()进行模拟 2.通过 Request.From["TextBox"].Trim()取值; 3.后台CS文件设置TextB ...
- asp.net textbox控件readonly为true时,后台取值的问题
如题,在后台通过textbox.Text方式取值为空,不论你默认值是否是空,如想要获得,需通过request.Form[""]的方式.
- ASP.NET页面使用JQuery EasyUI生成Dialog后台取值为空
原因: JQuery EasyUI生成Dialog后原来的文档结构发生了变化,原本在form里的内容被移动form外面,提交到后台后就没有办法取值了. 解决办法: 在生成Dialog后将它append ...
随机推荐
- [置顶] 使用Android OpenGL ES 2.0绘图之六:响应触摸事件
传送门 ☞ 系统架构设计 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229 传送门 ☞ GoF23种设计模式 ☞ 转载请注明 ☞ http://blog.csd ...
- 使用 IDEA 创建 Maven Web 项目 (一)- 使用IEAD创建Maven项目
创建IDEA项目 单击 Create New Project 按钮,弹出 New Project 对话框. 选择 Maven 选项,单击 Next 按钮. 输入 GroupId.ArtifactId. ...
- 开箱即用 - Memcache
废话少说,先上代码C# memcache Demo memcache 是服务器缓存系统,以键值对方式保存数据到内存中,把对象序列化后,理论上可支持所有的数据类型. 使用情景:怎么用都可以,注意的是它只 ...
- error C2448 函数样式初始值设定项类似函数定义
类似这种的 int grow_expansion(elen, e, b, h) int elen; REAL *e; REAL b; REAL *h; { // function definition ...
- Android学习笔记(一)Git相关配置及使用
一.配置 打开Git Bash, git config --global user.name "username" git config --global user.email & ...
- jQuery的css
1.css(name|pro|[,val|fn]) 访问匹配元素的样式属性. 参数name 描述: 取得第一个段落的color样式属性的值. $("p").css("co ...
- Android高级开发专题晋升班
Android高级开发专题晋升班 适用人群:1-3年以上经验的开发者丨学员平均薪酬20K/月
- 使用Stardict命令行版本sdcv
sdcv命令的常用选项如下: -l:列出安装的词典 -u:指定查词所用的词典 在我的电脑上列出的词典有: Dictionary's name Word count Merrian Webster 10 ...
- ECMAScript6之Set结构和Map结构
set数据结构 ES6提供了一个新的数据结构,Set,Set和Array数组相似,但是Set里没有重复的数据,可以说是一个值的集合. 同时,Set数据结构有以下属性和方法: size:返回成员总数 a ...
- java操作mongodb——更新数据
Java中可以通过updateOne,updateMany,replaceOne方法进行集合的文档更新.但是 _id 是不能更新的 updateOne只会更新一条数据,即使通过Filters.lt(& ...