7.6 定义要打印的表格
7.6.3 设置打印表格行
在上一小节的程序页面中点击继续按钮将转到设置打印表格行页面。如图7.11所示:
图7.11 设置打印表格行
页面设计如图7.12所示:
图7.12 PrintedRows.aspx页面设计
1、PrintedRows.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">
定义《<%=ViewState["printedtablename"].ToString()%>》的表格行
</td></tr>
<tr height="50"><td align="right"> </td></tr>
</table>
<table cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
<tr><td><asp:datagrid id="DataGrid1" runat="server" AllowPaging="True" Width="100%"
DataKeyField="RowID" >
<ItemStyle Font-Size="13px" HorizontalAlign="Center" Height="28px"
VerticalAlign="Bottom"></ItemStyle>
<HeaderStyle Font-Size="16px" HorizontalAlign="Center" Height="30px"
VerticalAlign="Middle" BackColor="#EEEEEE"></HeaderStyle>
<Columns>
<asp:TemplateColumn Visible="False" HeaderText="DataGrid行号">
<ItemTemplate><%#(int)DataBinder.eval_r(Container, "itemindex") + 1%>
</ItemTemplate>
<EditItemTemplate><FONT face="宋体"></FONT></EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="RowSerialNumber" HeaderText="表格行行号">
</asp:BoundColumn>
<asp:BoundColumn DataField="IncludedFields" ReadOnly="True"
HeaderText="打印的字段"></asp:BoundColumn>
<asp:BoundColumn DataField="SelfDefinedRowName" ReadOnly="True"
HeaderText="自定义行标题"></asp:BoundColumn>
<asp:BoundColumn DataField="RowSpan" ReadOnly="True"
HeaderText="跨几个自然行"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新行编号"
CancelText="取消" EditText="调整行编号"></asp:EditCommandColumn>
<asp:ButtonColumn Text="修改行" CommandName="EditRow"></asp:ButtonColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton id="LinkButton1" runat="server" CommandName="Delete">删除
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle Font-Size="9pt" HorizontalAlign="Right" ForeColor="#3366FF"
Mode="NumericPages"></PagerStyle>
</asp:datagrid></td>
</tr>
</table>
<table width="100%" bgColor="#f5fafe">
<tr>
<td align="left" colSpan="2"> <FONT face="宋体">
<asp:label id="Label2" runat="server">是自定义行?</asp:label>
<asp:checkbox id="CheckBox1" runat="server" >
</asp:checkbox></FONT></td>
</tr>
<% if(CheckBox1.Checked==false)
{
%>
<tr>
<td align="right" width="20%"><FONT face="宋体">选择该行所包含的字段:
</FONT> </td>
<td align="left">
<asp:listbox id="ListBox1" runat="server" Rows="3"></asp:listbox></td>
</tr>
<tr>
<td align="right"><FONT face="宋体">跨几个自然行?</FONT> </td>
<td align="left">
<asp:textbox id="TextBox1" runat="server" Columns="2"></asp:textbox></td>
</tr>
<%
}
%>
<% if(CheckBox1.Checked==true)
{
%>
<tr>
<td align="right" width="20%"><FONT face="宋体">行标题(如“签字盖章”):
</FONT> </td>
<td align="left">
<asp:textbox id="TextBox2" runat="server" Columns="30"></asp:textbox></td>
</tr>
<tr>
<td align="right"><FONT face="宋体">跨几个自然行?</FONT> </td>
<td align="left">
<asp:textbox id="TextBox3" runat="server" Columns="2"></asp:textbox></td>
</tr>
<%
}
%>
</table>
<table height="100" cellSpacing="0" cellPadding="0" width="100%" border="0">
<tr><td vAlign="middle" align="right" width="20%"></td>
<td align="left">
<asp:button id="Button1" runat="server" Text="增加行"></asp:button>
<asp:button id="Button3" runat="server" Text="修改"></asp:button>
<asp:button id="Button2" runat="server" Text="取消"></asp:button>
<asp:label id="Label1" runat="server" ForeColor="Red"></asp:label></td>
</tr>
</table>
</form>
......
2、PrintedRows.aspx.cs文件代码:
......
using System.Text.RegularExpressions;
using DataAccess;
using CommonTools;
namespace workflow.admin.PrintedTable
{
public class PrintedRows : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.CheckBox CheckBox1;
protected System.Web.UI.WebControls.ListBox ListBox1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Button Button3;
protected System.Web.UI.WebControls.Label Label1;
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();
//定义用于页面标题使用的打印表格名ViewState变量.
Base basecode=new Base();
string strSql="select PrintedTableName from PrintedTable where
PrintedTableID="+ViewState["printedtableid"].ToString();
DataSet ds=basecode.SQLExeDataSet(strSql);
ViewState["printedtablename"]
=ds.Tables[0].Rows[0]["PrintedTableName"].ToString();
BindGrid();
//默认显示增加行按钮
Button1.Visible=true;
Button3.Visible=false;
//ViewState["editedrowid"]用于修改行按钮
ViewState["editedrowid"]="";
//获取打印表格名称等变量
strSql="select RelatedTable,RelatedFields,ColumnsCount from PrintedTable
where PrintedTableID="+ViewState["printedtableid"].ToString();
ds=basecode.SQLExeDataSet(strSql);
if(ds == null || (ds.Tables[0].Rows[0]["RelatedFields"]==DBNull.Value
|| ds.Tables[0].Rows[0]["RelatedFields"].ToString() == ""))
{
Label1.Text="打印表格不包含任何可打印字段,无法定义打印行.";
return;
}
string relatedtable=ds.Tables[0].Rows[0]["RelatedTable"].ToString();
string[] relatedtablearray=new Tools().StringSplit(relatedtable,".");
string tablename=relatedtablearray[1];
ViewState["tablename"]=tablename;//用于设置ListBox控件使用.
string tableower=relatedtablearray[0];
ViewState["tableower"]=tableower;//用于设置ListBox控件使用.
//获取打印表格包含的字段数组
string[] printedfieldsarray=new Tools().StringSplit(ds.Tables[0].Rows[0]
["RelatedFields"].ToString(),",");
ViewState["printedfieldsarray"]=printedfieldsarray;//用于设置ListBox控件使用.
//每一行的最大列数(可以控制每行最多可以打印的字段个数)
int maxcolumns=int.Parse(ds.Tables[0].Rows[0]["ColumnsCount"].ToString());
ViewState["maxcolumns"]=maxcolumns;//用于添加(修改)行时使用.
//初始化ListBox控件
InitialListBox();
}
Label1.Text="";
}
void BindGrid()
{
string strSql="select *,(select PrintedTableName from PrintedTable where
PrintedTableID=a.PrintedTableID) as tablename from PrintedRows a where
PrintedTableID="+ViewState["printedtableid"].ToString()
+" order by RowSerialNumber";
DataSet ds=new Base().SQLExeDataSet(strSql);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}
//初始化选择每一行要打印字段的ListBox控件
void InitialListBox()
{
string tablename=ViewState["tablename"].ToString();
string tableower=ViewState["tableower"].ToString();
//打印表格要打印的所有字段
string[] printedfieldsarray=(string[])ViewState["printedfieldsarray"];
//获取打印表格在PrintedRows表中已经包含的打印字段
Base basecode=new Base();
string strSql="select IncludedFields from PrintedRows where
PrintedTableID="+ViewState["printedtableid"].ToString();
DataSet includedfieldsds=basecode.SQLExeDataSet(strSql);
string includedfieldstr="";
for(int k=0;k<includedfieldsds.Tables[0].Rows.Count;k++)
{
if(includedfieldsds.Tables[0].Rows[k]["IncludedFields"] != DBNull.Value)
{
includedfieldstr=includedfieldstr+includedfieldsds.Tables[0]
.Rows[k]["IncludedFields"].ToString().Trim();
}
}
string[] includedfieldsarray=new Tools().StringSplit(includedfieldstr,",");
//获取打印表格所包含字段中除PrintedRows中已包含的打印字段之外的字段的中文名等
strSql="select FieldAlias,FieldName from RelatedTablesFields where
TableName='"+tablename+"' and TableOwer='"+tableower+"' and
(FieldName='"+printedfieldsarray[0]+"'";
//i<printedfieldsarray.Length-1因为最后一项为空格
for(int i=1;i<printedfieldsarray.Length-1;i++)
{
strSql=strSql+" or FieldName='"+printedfieldsarray[i]+"'";
}
strSql=strSql+") and (FieldName is not null";//FieldName isn't null作sql连接使用
for(int j=0;j<includedfieldsarray.Length-1;j++)
{
strSql=strSql+" and FieldName != '"+includedfieldsarray[j]+"'";
}
strSql=strSql+")";
DataSet ds=basecode.SQLExeDataSet(strSql);
ListBox1.Rows=3;
ListBox1.SelectionMode=ListSelectionMode.Multiple;
ListBox1.DataSource=ds;
ListBox1.DataTextField="FieldAlias";
ListBox1.DataValueField="FieldName";
ListBox1.DataBind();
//如果当前的ListBox所包含的项数<1,则不能再添加打印字段行,只能添加自定义行.
if(ListBox1.Items.Count<1)
{
CheckBox1.Checked=true;
CheckBox1.Enabled=false;
}
else
{
CheckBox1.Checked=false;
CheckBox1.Enabled=true;
}
}
private void CheckBox1_CheckedChanged(object sender, System.EventArgs e)
{
Page_Load(sender,e);
}
......(待续)