通过js根据后台数据动态生成一个页面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ExportSelField.aspx.cs" Inherits="GDAS.Web.DocManage.Library.ExportSelField" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>test</title>
<link href="../../CSS/SubPage.css" rel="Stylesheet" type="text/css" />
<script language="javascript" type="text/javascript">
function CloneTr() {
var newRow;
var table = document.getElementById("tbField");
if (table.rows[0]) {
newRow = table.rows[1].cloneNode(true);
}
else {
newRow = document.createElement("tr");
newRow.appendChild(document.getElementById("td"));
}
newRow.style.display = "block";
table.rows[0].parentElement.appendChild(newRow);
var maxLength = table.rows.length;
table.rows[maxLength - 1].cells[0].innerHTML = "<input type='checkbox' style='cursor:hand'/>";
table.rows[maxLength - 1].cells[1].firstChild.value = "";
table.rows[maxLength - 1].cells[2].innerHTML = " ";
table.rows[maxLength - 1].cells[3].innerHTML = " ";
}
//删除最后一行,如果是只有一行记录则清空行
function DeleteTr() {
var table = document.getElementById("tbField");
var index = event.srcElement.parentNode.parentNode.rowIndex;
var result = window.confirm("您确认删除此记录?");
if (result) {
table.deleteRow(index);
return;
}
}
function LoadData() {
var type = "FILE";
var DocTypeID = "<%=this.DocTypeID%>";
var response = ExportSelField.GetExportField(type,DocTypeID);
if (response.error != null) {
alert(response.error);
return;
}
var dt = response.value;
var table = document.getElementById("tbField");
CreateTable(dt, table);
}
function CreateTable(dt, table) {
for (var i = 0; i < dt.Rows.length; i++) {
var row_count;
CloneTr(table);
row_count = table.rows.length - 1;
table.rows[row_count].cells[0].innerHTML = "<input type='checkbox' style='cursor:hand' checked='true'/>";
table.rows[row_count].cells[1].firstChild.value = (dt.Rows[i].Name == "") ? " " : dt.Rows[i].Name;
table.rows[row_count].cells[2].innerHTML = (dt.Rows[i].DBColumnName == "") ? " " : dt.Rows[i].DBColumnName;
table.rows[row_count].cells[3].innerHTML = (dt.Rows[i].DBColumnType == "") ? " " : dt.Rows[i].DBColumnType;
}
}
function SelectAll(obj) {
var table = document.getElementById("tbField");
for (i = 1; i < table.rows.length; i++) {
table.rows[i].childNodes[0].childNodes[0].checked = obj.checked;
}
}
function Export() {
window.returnValue = GetValue();
window.close();
}
function GetValue() {
var result = "";
var iCount = 0;
var table = document.getElementById("tbField");
document.getElementById("hdfFieldString").value = "";
for (var i = 2; i < table.rows.length; i++) {
if (table.rows[i].childNodes[0].childNodes[0].checked == true) {
var field0 = field1 = field2 = "";
field0 = table.rows[i].cells[1].firstChild.value;
field1 = table.rows[i].cells[2].innerHTML;
field2 = table.rows[i].cells[3].innerHTML;
result += GetFieldString(field0, field1, field2) + ",";
iCount++;
}
}
if (iCount == 0)
result = "*";
return result;
}
function GetFieldString(cField, eField, FieldType) {
var result = "";
switch (FieldType) {
case "date":
case "datetime": {
result = "convert(varchar, " + eField + ", 23) as '" + cField + "'";
break;
}
default:
{
result = eField + " as '" + cField + "'";
break;
}
}
return result;
}
</script>
</head>
<body onload="LoadData()">
<form id="form1" runat="server">
<div id="main" style="width:95%">
<fieldset>
<legend>选择导出字段</legend>
<table id="tbField" class="grid">
<tr>
<th style="width:60px">
<input type='checkbox' id="chkall" style='cursor: hand' title="全选" onclick="SelectAll(this)"
checked="checked" />
</th>
<th>
字段名
</th>
<th style="display: none">
fieldname
</th>
<th style="display: none">
数据类型
</th>
</tr>
<tr style="display: none">
<td align="center">
<input type='checkbox' />
</td>
<td align="center">
<asp:TextBox ID="TextBox1" runat="server" Width="95%"></asp:TextBox>
</td>
<td style="display: none">
</td>
<td style="display: none">
</td>
</tr>
</table>
</fieldset>
<fieldset style="text-align: center; margin-top: 10px">
<input type="button" id="btnExport" class="btnS" value="导出" style="width: 60px;
height: 24px" onclick="Export();" />
<input type="button" class="btnS" style="width: 60px; height: 24px" value="关闭"
onclick="javascript:window.close();" />
</fieldset>
</div>
<asp:HiddenField ID="hdfFieldString" runat="server" />
</form>
</body>
</html>