7.6 定义要打印的表格
7.6.2 选择打印表格要用到的业务数据字段
图7.10 选择业务数据表字段
<form id="Form1" method="post" runat="server">
<table height="80" cellSpacing="0" cellPadding="0" width="100%" border="0">
<tr>
<td><a href="javascript:history.back()">返回上一页</a></td>
</tr>
<tr height="60"><td></td></tr>
</table>
<table cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
<tr><td style="FONT-SIZE: 16px" align="center">选择要打印的字段</td></tr>
<tr height="50"><td align="right"> </td></tr>
BORDER-COLLAPSE: collapse" runat="server" border="1"
</asp:table></td>
</tr>
</table>
<table height="100" cellSpacing="0" cellPadding="0" width="100%" border="0">
<tr><td vAlign="middle" align="right"> </td>
<td align="center">
<asp:button id="Button1" runat="server" Text="暂时保存"></asp:button>
<asp:Button id="Button3" runat="server" Text="放弃"></asp:Button>
</td>
</tr>
</table>
......
PrintedFields.aspx.cs代码如下:
using DataAccess;
namespace workflow.admin.PrintedTable
{
public class PrintedFields : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Table Tbl;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Button Button3;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
if(! IsPostBack)
{
//验证用户是否登录
if(Session["userid"] == null)
ViewState["printedtableid"]=Request.QueryString["id"].ToString();
}
//表的所有者和表名.
string printedtableid=ViewState["printedtableid"].ToString();
PrintedTableID="+printedtableid;
Base basecode=new Base();
DataSet ds=basecode.SQLExeDataSet(strSql);
|| ds.Tables[0].Rows[0]["RelatedTable"].ToString()=="")
{
Response.Write("You have encounter an error,please contact to the developer.");
return;
}
.StringSplit(ds.Tables[0].Rows[0]["RelatedTable"].ToString(),".");
string ower=owertblname[0];
string tablename=owertblname[1];
//根据关联表获得关联表的字段名及其中文描述.
strSql="select * from RelatedTablesFields where TableName='"+tablename+"' and TableOwer='"+ower+"'";
ds=basecode.SQLExeDataSet(strSql);
ViewState["relatedfields"]=ds;
TableRow tr=new TableRow();
TableCell tc=new TableCell();
tc.Text="请选择要打印的字段:";
tc.Font.Size=FontUnit.Parse("14px");
tc.ColumnSpan=9;//一行9列
tc.HorizontalAlign=HorizontalAlign.Left;
tr.Cells.Add(tc);
Tbl.Rows.Add(tr); tr=new TableRow();//新行
tc=new TableCell();//空白列
tr.Cells.Add(tc);
//为每一个关联字段设置单选框,并将字段名作为它的ID.
CheckBox chk=null;
Label lb=null;
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{
//一行9列,两边空白列,中间包括7个字段选择控件
if((i% 7) == 0 & i>0)
{
//一行尾部加一列
tc=new TableCell();
tc.Text=" ";
tr.Cells.Add(tc);
//另起一行并加一列空白列
Tbl.Rows.Add(tr);
tr=new TableRow();
tc=new TableCell();
tr.Cells.Add(tc);
tc=new TableCell();
tc.HorizontalAlign=HorizontalAlign.Right;
lb=new Label();
lb.Text=ds.Tables[0].Rows[i]["FieldAlias"].ToString();
tc.Controls.Add(lb);
chk=new CheckBox();
//如果是标识字段则默认不钩选并不可编辑.
if(ds.Tables[0].Rows[i]["IsIdentity"].ToString() =="Y")
{
chk.Checked=false;
chk.Enabled=false;
}
tc.Controls.Add(chk);
tr.Cells.Add(tc);
}
//最后一行最后一个空白列的跨度设置为最大值.
tc=new TableCell();
tc.ColumnSpan=9;
//号隔开的字段集字符串,得到字段名并设置对应控件的钩选.
PrintedTableID="+printedtableid;
ds=basecode.SQLExeDataSet(strSql);
if(ds !=null & ds.Tables[0].Rows[0]["RelatedFields"] !=DBNull.Value)
{
string printedfieldsstr="";
printedfieldsstr=ds.Tables[0].Rows[0]["RelatedFields"].ToString().Trim();
if(printedfieldsstr != "")
{
string[] fieldsarray=new Tools().StringSplit(printedfieldsstr,",");
for(int j=0;j<fieldsarray.Length;j++)
{
//所以要判断.也可以将循环条件设置为j<fieldsarray.Length-1
if(fieldsarray[j] !="")
{
chk=(CheckBox)this.FindControl(fieldsarray[j]);
if(chk !=null)//避免找不到指定的控件
chk.Checked=true;
}
}
}
}
//保存按钮
private void Button1_Click(object sender, System.EventArgs e)
{
SaveFields();
Response.Redirect("PrintedTable.aspx");
//放弃按钮
private void Button3_Click(object sender, System.EventArgs e)
{
Response.Redirect("PrintedTable.aspx");
}
//继续按钮(转到设置打印表格行页面,设计表格行)
private void Button2_Click(object sender, System.EventArgs e)
{
SaveFields();
Response.Redirect("PrintedRows.aspx?id="+ViewState["printedtableid"].ToString());
}
//保存打印字段
private void SaveFields()
{
//获取字段单选框列表的值
string printedtableid=ViewState["printedtableid"].ToString();
string chkedfields="";
//CheckBox chk=null;
DataSet ds=(DataSet)ViewState["relatedfields"];
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{
.Rows[i]["FieldName"].ToString());
if(chk.Checked)
chkedfields=chkedfields+ds.Tables[0].Rows[i]["FieldName"].ToString()+",";
}
PrintedTableID="+printedtableid;
Base basecode=new Base();
if(! basecode.SQLExeNonQuery(strSql))
{
Label1.Text=basecode.BaseSqlErrDes;
return;
}
}
}