您的位置:首页 > 数据库

第七章 业务数据库的管理(八)-- 打印表格行

2012-09-19 09:35 351 查看
7.6 定义要打印的表格

7.6.2 选择打印表格要用到的业务数据字段

在上一小节的定义打印表格页点击“设置表格行”按钮后将以打印表格编号为参数转到选择业务数据字段页面PrintedFields.aspx页面,选择业务数据字段,保存到PrintedTable表中。如图7.10所示:



图7.10 选择业务数据表字段
PrintedFields.aspx页面代码比较简单,主要是一个空的服务器端表格控件,表格的行和列由后台代码动态生成:
......
<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>

<tr><td><asp:table id="Tbl" style="BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid;
BORDER-LEFT: 1px solid; BORDER-BOTTOM: 1px solid; FONT-FAMILY: 宋体;
BORDER-COLLAPSE: collapse" runat="server" border="1"
cellPadding="3" cellSpacing="0" Width="100%">
</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="Button2" runat="server" Text="继续"></asp:Button>  
<asp:button id="Button1" runat="server" Text="暂时保存"></asp:button> 

<asp:Button id="Button3" runat="server" Text="放弃"></asp:Button>

<asp:Label id="Label1" runat="server" ForeColor="Red"></asp:Label>
</td>

</tr>

</table>

</form>
......
PrintedFields.aspx.cs代码如下
......
using DataAccess;

using CommonTools;
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)

Response.Redirect("../Message.aspx");
//使用页面状态变量保存接收的打印表格编号参数
ViewState["printedtableid"]=Request.QueryString["id"].ToString();

}

//根据printedtableid获得关联的数据表名称,因为表名为"ower.tablename",所以要分割出
//表的所有者和表名.

string printedtableid=ViewState["printedtableid"].ToString();

string strSql="select RelatedTable from PrintedTable where
PrintedTableID="+printedtableid;

Base basecode=new Base();

DataSet ds=basecode.SQLExeDataSet(strSql);

if(ds.Tables[0].Rows[0]["RelatedTable"]==DBNull.Value
|| ds.Tables[0].Rows[0]["RelatedTable"].ToString()=="")

{

Response.Write("You have encounter an error,please contact to the developer.");

return;

}

string[] owertblname=new Tools()
.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();

chk.ID=ds.Tables[0].Rows[i]["FieldName"].ToString();//为避免出现ID重复错误
//如果是标识字段则默认不钩选并不可编辑.

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;

tr.Cells.Add(tc);
Tbl.Rows.Add(tr);
//如果PrintedTable已有打印字段的内容(即编辑已有PrintedTable记录的情形),要分割以逗
//号隔开的字段集字符串,得到字段名并设置对应控件的钩选.

strSql="select RelatedFields from PrintedTable where
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;

}

}

}

}

}//Page_Load()
//保存按钮

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++)

{

CheckBox chk=(CheckBox)this.FindControl(ds.Tables[0]
.Rows[i]["FieldName"].ToString());

if(chk.Checked)

chkedfields=chkedfields+ds.Tables[0].Rows[i]["FieldName"].ToString()+",";

}

string strSql="update PrintedTable set RelatedFields='"+chkedfields+"' where
PrintedTableID="+printedtableid;

Base basecode=new Base();

if(! basecode.SQLExeNonQuery(strSql))

{

Label1.Text=basecode.BaseSqlErrDes;

return;

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: