ASP.NET如何批量保存动态生成的文本框?
对于OA系统,表单签核功能必不可少。而根据公司的情况,表单自然又五花八门,所以就要求能够让用户自己建立表单并设定表单的流程、填写内容等等。我之前写过一篇文章【地址:pivot的用法(SQL SERVER 2005 以上)】,对于OA系统这些填写内容的数据表结构作过一定的说明,而今天,我会给大家说明一下,用户在新建表单时,填表填到一半时,怎么暂存所填写的内容(此原理适用于表单提交时的保存操作)。
1、首先,以下面这张table为例子说明,其中【colValue】为用户填写的内容:
图一、表格详情
2、其次,我们需要把这些内容输出到页面,输出的话,比较简单,新建一个WebBaseSetup.aspx文件,以下为源码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebBaseSetup.aspx.cs" Inherits="Admin_WebBaseSetup" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>網址導覽設定</title>
<link href="../css/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div style="width:600px;">
<input type="button" id="btnSave" value="保存" style="width:50px" />
<asp:Literal ID="ltList" runat="server"></asp:Literal>
</div>
</form>
</body>
</html>
3、至于WebBaseSetup.aspx.cs文件的源码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text; public partial class Admin_WebBaseSetup : clsCheckRole
{
clsBaseSetup oBaseSetup = new clsBaseSetup(); private readonly string _colValue = "colValue_";
private readonly string _colDesction = "colDesction_"; protected void Page_Load(object sender, EventArgs e)
{
ckSession();
if (!IsPostBack)
{
BindData();
}
} private void BindData()
{
var baseList = oBaseSetup.GetBaseSetupList();
if (baseList.Any())
{
StringBuilder sb = new StringBuilder();
sb.Append("<table class='tbinfo' border='0' cellpadding='0' cellspacing='1' style='background-color:#D9E6FF;'>\n");
sb.Append("<tr><th style='width:130px;'>變量名</th><th style='width:70px;'>數值</th><th>備注</th></tr>");
foreach (var item in baseList)
{
sb.AppendFormat("<tr>\n<td>{0}</td>\n", item.colName);
sb.AppendFormat("<td style='text-align:center;'><input type='input' name='{0}' class='colControl' value='{1}' style='width:40px;' /></td>\n", _colValue + item.id, item.colValue);
sb.AppendFormat("<td><input type='input' name='{0}' class='colControl' value='{1}' style='width:98%;' /></td>\n<tr>\n", _colDesction + item.id, item.colDesction);
}
sb.Append("</table>");
ltList.Text = sb.ToString();
}
}
}
4、需要引用的clsBaseSetup.cs类源码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient; /// <summary>
/// clsBaseSetup 的摘要说明
/// </summary>
public class clsBaseSetup
{ #region model
/// <summary>
/// 主鍵
/// </summary>
public string id { get; set; } /// <summary>
/// 系統標識名稱
/// </summary>
public string colName { get; set; } /// <summary>
/// 設定值
/// </summary>
public string colValue { get; set; } /// <summary>
/// 說明
/// </summary>
public string colDesction { get; set; }
#endregion
public clsBaseSetup()
{
//
// TODO: 在此处添加构造函数逻辑
//
} #region method /// <summary>
/// 返回基礎設定表中的所有設定項
/// </summary>
/// <returns></returns>
public List<clsBaseSetup> GetBaseSetupList()
{
List<clsBaseSetup> list = new List<clsBaseSetup>();
string sql = "SELECT id,colName,colValue,colDesction FROM CWNS_BaseSetUp_Tab ORDER BY colName";
using (SqlDataReader dr = DbHelperSQL.ExecuteReader(sql))
{
while (dr.Read())
{
list.Add(GetModel(dr));
}
}
return list;
} /// <summary>
/// 獲取基礎設定表中的設定項
/// </summary>
/// <param name="colName">字段名</param>
/// <param name="defaultValue">如果抓不到值則取默認值</param>
/// <returns></returns>
public Object GetColValueByColName(string colName, string defaultValue)
{
string sql = string.Format("SELECT ISNULL(colValue,'{0}') FROM CWNS_BaseSetUp_Tab WHERE colName='{1}'", defaultValue, colName);
Object obj = DbHelperSQL.GetSingle(sql);
if (obj == null)
{
obj = defaultValue;
}
return obj;
}
#endregion #region 私有方法
/// <summary>
/// 獲取實體
/// </summary>
/// <param name="dr"></param>
/// <returns></returns>
private clsBaseSetup GetModel(SqlDataReader dr)
{
return new clsBaseSetup
{
id = dr["id"].ToString(),
colName = dr["colName"].ToString(),
colValue = dr["colValue"].ToString(),
colDesction = dr["colDesction"].ToString()
};
}
#endregion
}
5、好了,到了这步以后,运行网站,看到的界面应该会类似于下面这张图的,当然,CSS文件没提供,所以样式可能不一样,可以自己调:
图二、预览效果图
6、右键浏览器查看它生成的那个table的代码如下:
<table class='tbinfo' border='0' cellpadding='0' cellspacing='1' style='background-color:#D9E6FF;'>
<tr>
<th style='width:130px;'>變量名</th>
<th style='width:70px;'>數值</th>
<th>備注</th>
</tr>
<tr>
<td>Navigate_maxCount</td>
<td style='text-align:center;'><input type='input' name='colValue_2' class='colControl' value='100' style='width:40px;' /></td>
<td><input type='input' name='colDesction_2' class='colControl' value='設置網址導覽首頁每分類下顯示的最大項目數量' style='width:98%;' /></td>
<tr>
<tr>
<td>Navigate_rowNum</td>
<td style='text-align:center;'><input type='input' name='colValue_1' class='colControl' value='6' style='width:40px;' /></td>
<td><input type='input' name='colDesction_1' class='colControl' value='設置綱址導覽首頁每行顯示多少項' style='width:98%;' /></td>
<tr>
</table>
7、下面接下来的动作,就是编写【保存】按钮的动作了,目标是把input的表单打包传输到后台的一个处理程序处理,可以用jquery操作:
在【WebBaseSetup.aspx】的head标签中加入以下代码:
<script src="../js/jquery-1.6.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#btnSave").click(function () {
SaveColValue();
});
}); function SaveColValue() {//保存欄位值
var param = $(".tbinfo").find('.colControl').serializeArray();
param.push({ name: 'Action', value: 'Save' });
jQuery.ajax({
url: '../ajax/UpdateBaseSetupColValue.ashx',
type: 'POST',
data: param,
async: false,
success: function (data) {
alert(data);
}
});
}
</script>
8、由于【数值】跟【备注】两栏均要保存,所以调用.find('.colControl')全部传过去,接着,看一下【UpdateBaseSetupColValue.ashx】的代码吧:
<%@ WebHandler Language="C#" Class="UpdateBaseSetupColValue" %> using System;
using System.Web;
using System.Collections.Generic; public class UpdateBaseSetupColValue : IHttpHandler
{ public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World"); var req = context.Request;
context.Response.Write(SaveColValue(req));
} public bool IsReusable {
get {
return false;
}
} /// <summary>
/// 保存欄位值
/// </summary>
/// <returns></returns>
private string SaveColValue(HttpRequest reqForm)
{
var req = reqForm.Form;
var action = req["Action"];
var _colValue = "colValue_";
var _colDesction = "colDesction_";
if (!string.IsNullOrEmpty(action) && action == "Save")
{
List<String> listSql = new List<string>();
foreach (var item in req.AllKeys)
{
if (item.IndexOf(_colValue) > -)
{
listSql.Add(string.Format("UPDATE [CWNS_BaseSetUp_Tab] SET [colvalue] =N'{0}' WHERE ID='{1}'",
req[item], item.Replace(_colValue, "")));
}
else if (item.IndexOf(_colDesction) > -)
{
listSql.Add(string.Format("UPDATE [CWNS_BaseSetUp_Tab] SET [colDesction] =N'{0}' WHERE ID='{1}'",
req[item], item.Replace(_colDesction, "")));
}
}
try
{
DbHelperSQL.ExecuteSqlTran(listSql);
return "保存成功!";
}
catch (Exception)
{
return "保存失敗!";
} //Response.Redirect(Request.Url.ToString());
}
return "參數缺失,請聯系管理員!";
}
}
这样子,就实现了动态保存用户所作的修改了。当然,就算用户没修改,也会重新update一次所有的数据,这点需要注意,嘻嘻。
虽然过程没有细说,但该说的全都有写了,如果感兴趣的话,不妨花点时间仔细阅读一下。
补充一下css文件吧,也挺不错的,后面我还会单独写一篇出来分享一下:
/*-- 表格樣式 --*/
.tbinfo{background: #d6e0ef; line-height: 18px;
width: 100%;
}
.tbinfo th{font-size:12px;background-color:#97CBFF;border-bottom:1px solid #a5ceef; text-align:center;color: #003399;}
.tbinfo td{background: #fff;padding:2px 0;}
.tbinfo .lt{background: #fafafa;text-align: right; padding:0 4px; width:15%; white-space:nowrap;}
.tbinfo .rt{background: #fff; text-align:left; padding:0 2px;}
.tbinfo .rt:hover{background: #fafafa;}
.hidden { display:none;} /*-- 頁碼樣式 --*/
.page
{
text-align:center;
height:26px;
} .page a
{
padding:2px 6px 2px 6px;
width:24px;
border:1px solid blue;
text-align:center;
font-size:12px;
line-height:20px;
} .page a:hover
{
padding:2px 6px 2px 6px;
width:24px;
border:1px solid red;
text-align:center;
font-size:12px;
line-height:20px;
} .page span
{
padding:2px 6px 2px 6px;
width:24px;
border:1px solid silver;
text-align:center;
font-size:12px;
line-height:20px;
color:silver;
}
ASP.NET如何批量保存动态生成的文本框?的更多相关文章
- 【转】ASP.NET中服务器控件Table动态生成表格及其属性介绍
下文所有内容转自开源中国:http://www.oschina.net/question/565065_86453#tags_nav ================================= ...
- ExtJs动态生成复选框
var old_value = Ext.get("fgzr_select").getValue() if(old_value == ""){ document. ...
- web前端 ajax加载动态生成复选框demo
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- asp.net小技巧:保留password模式文本框textbox内的数据不丢失。
在asp.net 2.0环境下,使用textbox,提交到服务器再传回,如果textbox是password模式的,那么textbox内的密码(星号.圆点),就没有了! 一个可行的做法是 : prot ...
- 【js】批量判断表单中的文本框非空
方法一: <script type=”text/javascript”> /* * 批量验证表单非空 * 需要非空验证控件的样式class=”mustadd” */ $(".mu ...
- 动态创建的文本框想要加上jQuery的datepicker功能变成日期选择控件该怎么办?
通常页面输入控件想得到日期选择功能,借助jQuery是这样实现的: 1.载入css和js <script src="jqueryui/jquery-ui.js" type=& ...
- 利用StringList对象来管理这些动态生成的对象
如果程序需要动态创建大量的对象,那么我们可以利用StringList对象来管理这些动态生成的对象.1.创建StringList对象:OBJ := TStringList.Create; 2.保存动态生 ...
- jQuery动态生成<select>下拉框
前一阵在项目里需要动态生成下拉框,找了一下用jQuery实现比较方便,这里整理一下. 下文所述方法只是本人在项目中遇到问题的解决方法,场景较为简单,也希望能帮助有需要的朋友 1.动态生成下拉框的两种方 ...
- 通过jquery来实现文本框和下拉框动态添加效果,能根据自己的需求来自定义最多允许添加数量,实用的jquery动态添加文本框特效
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
随机推荐
- Mac PHPStorm快捷键总结
全局搜索(command + shift + F) 显示类中的方法 (command + 7) 函数追踪 (command +鼠标点击) 单行注释/取消(command + /) 输入行号跳到某一行( ...
- ASP.NET MVC与ASP.NET WebForm
ASP.NET MVC是微软公司的一款WEB开发框架,整合了“模型-视图-控制器”架构的高效与整洁,是敏捷开发最现代的思想与技术.它是传统ASP.NET WebForm的一个完善的替代品. 1.当今的 ...
- arguments 参数
下面要写的是知识梳理的一个案例: 写一个求和的方法sumFn,不管传递的参数有什么,都能将最终的和算出来,并且返回给函数外部使用.(要求:一个参数都不传默认结果为0,对于传递的非正常数字的参数不与累加 ...
- 关于AJAX的一些事
在JQ中运用AJAX的操作是很舒服的一件事,一直以来我对他都有个错误的认识,直到遇见了问题才把他研究个透彻. 下面贴出两种AJAX的写法,当然都是正确的. 其一: $.ajax({ type: 'po ...
- Android adb命令查看sharedpreferences
adb shell run-as com.example.android //对应包名 ls查看当前目录下的所有文件,找到shared_prefs cd shared_prefs ls 查看所有的 s ...
- mysql8.0.11 在windows64安装 步骤
MySQL8.0 Windows zip包下载地址:https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.11-winx64.zip 环境:Wind ...
- maven(13)-安装nexus私服
环境 nexus最新3.x版需要java1.8,2.x版需要1.7以上.我之前一直用2.x,现在偿试在centos7和window10上分别安装nexus3.x,首先确保系统中已经配好了JDK1 ...
- asp.net mvc4 小问题
最近在学习mvc4中间出现一些问题.留作记录.. 1.新建立的项目在vs2013中运行后会出现一个长轮询..这个叫browserLink 是vs2013中新加入的东西.至于更多解释.直接百度.. 关闭 ...
- python之路——进程
操作系统背景知识 顾名思义,进程即正在执行的一个过程.进程是对正在运行程序的一个抽象. 进程的概念起源于操作系统,是操作系统最核心的概念,也是操作系统提供的最古老也是最重要的抽象概念之一.操作系统的其 ...
- 对于当下国产CPU如火如荼有感
国家在国家战略层面去做国产CPU这个事情,从初衷来说是好的.国产CPU战略如果能够实现,则会大大加强我国在计算机产业领域从头到尾的话语权与技术竞争力.但是个人觉得,事情不是那么简单.我将从下面几个方面 ...