此处把table中的内容放在上部分的form,是因为如果txtHeadDes和txtBodyDes中的内容设计到敏感字符或者空格、换行等问题,我们把它转换之后放在form。这样通过form加密encodeURIComponent传入后台,然后后台C#解析@HttpUtility.UrlDecode,可以得到完整的前端保存的内容。
cs代码:
public int SaveEncoding(FormCollection form)
{
string head = @HttpUtility.UrlDecode(form["txtHeadDes1"]);
string body = @HttpUtility.UrlDecode(form["txtBodyDes1"]);
string EchartType = form["txtchartType1"];
string Version = form["txtVersion1"];
string VersionDes = form["txtVersionDes1"];
string CkNew = form["ckNew1"];
string Html = head + body;
int flag = 0;
if (EchartType != null && head != null && body != null)
{
string userID = GetUserID.GetUserIDAndPassword();
if (!string.IsNullOrEmpty(userID))
{
if (CkNew.ToLower() == "true")
{
string sqlHtml = "insert into HtmlEncoding values('" + Guid.NewGuid().ToString() + "','" + userID + "','" + EchartType.ToLower() + "','" + Transcoding.Encode64(Html) + "','" + DateTime.Now.ToString("yyyy-MM-dd") + "','" + VersionDes + "') ";
return flag = DBHelper.GetExecuteNonQuery(sqlHtml);
}
else
{
//string sqlHtml = "update HtmlEncoding set Encoding = '" + Convert.ToBase64String(System.Text.UTF8Encoding.Unicode.GetBytes(Html)) + "', VersionDes = '" + VersionDes + "' where 1=1 and EchartType = '" + EchartType.ToLower() + "' and Version = '" + Version + "' and UserID = '" + userID + "'";
string sqlHtml = "update HtmlEncoding set Encoding = '" + Transcoding.Encode64(Html) + "', VersionDes = '" + VersionDes + "' where 1=1 and EchartType = '" + EchartType.ToLower() + "' and Version = '" + Version + "' and UserID = '" + userID + "'";
return flag = DBHelper.GetExecuteNonQuery(sqlHtml);
}
}
}
else
{
flag = 2;
}
return flag;
}
前端js代码:
<script type="text/javascript" src="~/Scripts/jquery-1.7.1.js"></script>
<script type="text/javascript" src="~/Scripts/jquery-form.js"></script>
<script type="text/javascript">
function SaveFun() {
$("#txtHeadDes1").val(encodeURIComponent($("#txtHeadDes").val()));
$("#txtBodyDes1").val(encodeURIComponent($("#txtBodyDes").val()));
$("#txtchartType1").val($("#chartType").val());
$("#txtVersion1").val($("#txtVersion").val());
if ($("input[name='ckNew']").is("checked") == true) {
$("#ckNew1").val(true);
}
else {
$("#ckNew1").val(false);
}
$("#txtVersionDes1").val($("#txtVersionDes").val());
var options = {
success: resultFunction,
type: 'post',
dataType: 'json',
clearForm: true,
async: false
};
$('#TemplateForm').ajaxSubmit(options);
}
//提交
function resultFunction(responseText, statusText) {
if (statusText == 'success') {
if (parseInt(responseText) == 1) {
alert("保存成功!");
}
else {
alert("不能为空!");
}
}
else {
alert('服务器错误!');
}
}
</script>
html代码:
<div>
<form id="TemplateForm" action="../HtmlEncoding/SaveEncoding" method="post"><input id="txtHeadDes1" name="txtHeadDes1" type="text" hidden="hidden" />
<input id="txtBodyDes1" name="txtBodyDes1" type="text" hidden="hidden" />
<input id="txtchartType1" name="txtchartType1" type="text" hidden="hidden" />
<input id="txtVersion1" name="txtVersion1" type="text" hidden="hidden" />
<input id="ckNew1" name="ckNew1" type="text" hidden="hidden" />
<input id="txtVersionDes1" name="txtVersionDes1" hidden="hidden" />
</form>
<table>
<thead>
<tr>
<td>图形类型:</td>
<td colspan="3">
<select id="chartType" name="chartType" style="width: 150px;" οnchange="SelectEchartType()">
<option value="line" selected="selected">折线</option>
<option value="bar">柱状</option>
<option value="pie">饼图</option>
<option value="map">地图</option>
</select>
</td>
</tr>
<tr>
<td style="width: 100px;">版本号:
</td>
<td>
<select id="txtVersion" name="txtVersion" οnchange="selectEncoding()"></select>
</td>
<td>是否为新版本:
</td>
<td>
<input type="checkbox" id="ckNew" name="ckNew" />
</td>
</tr>
<tr>
<td>版本说明:</td>
<td colspan="3">
<textarea id="txtVersionDes" name="txtVersionDes" style="width: 500px; min-height: 20px;"></textarea></td>
</tr>
</thead>
<tbody>
<tr>
<td style="width: 100px;">head部分:</td>
<td colspan="3">
<textarea id="txtHeadDes" name="txtHeadDes" style="width: 500px; min-height: 200px;"></textarea></td>
</tr>
<tr>
<td>body部分:</td>
<td colspan="3">
<textarea id="txtBodyDes" name="txtBodyDes" style="width: 500px; min-height: 200px;"></textarea></td>
</tr>
</tbody>
<tfoot>
<tr style="text-align: center; height: 80px;">
<td>
<input id="btnSubmit" type="button" οnclick="SaveFun();" value="提交" /></td>
<td>
<input id="btnGo" type="button" value="返回" οnclick="SetEchartsConfig()" /></td>
<td>
<input id="btnEcharts" type="button" value="查看图表" οnclick="GoEcharts()" /></td>
<td>
<input id="btnDelete" type="button" value="删除" οnclick="DeleteEncoding()" /></td>
</tr>
</tfoot>
</table>
</div>