您的位置:首页 > 数据库

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

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

打印表格是用户在完成任务实例时要用到的。用户处理过程任务时除了审核和编辑业务数据,可能还需要打印与业务有关的表格予以存档。打印表格实际上是以特定的表格格式显示相关业务数据,一项业务可能需要打印多种表格。定义打印表格的程序就是要定义表格形式,设置表格要打印的业务数据字段等。

7.6.1 为业务数据表定义打印表格



图7.8 为业务数据表定义打印表格



图7.9 PrintedTable.aspx页面设计

需要指出的是,如图7.9所示,与通常编辑按钮使用“编辑、更新、取消”按钮组不同,这里编辑按钮使用普通按钮列,不过命令名使用了编辑命令Edit;设置表格行按钮与此类似,也是普通按钮列,命令名借用了更新命令名(对应DataGrid控件的更新事件处理函数)。
1、页面代码
<form id="Form1" method="post" runat="server">

<table cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">

<tr height="60">

<td style="FONT-SIZE: 16px" align="center">与业务表关联的打印表格</td>

</tr>

</table>

<table cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">

<tr>

<td><asp:datagrid id="DataGrid1" runat="server"

HeaderStyle-HorizontalAlign="Center" DataKeyField="PrintedTableID"
Width="100%" AllowPaging="True" PageSize="6">

<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:BoundColumn DataField="PrintedTableName" HeaderText="打印表格名称">
</asp:BoundColumn>

<asp:BoundColumn DataField="RelatedTable" HeaderText="关联业务表">
</asp:BoundColumn>

<asp:BoundColumn DataField="ColumnsCount" HeaderText="最大列数">
</asp:BoundColumn>

<asp:BoundColumn DataField="RelatedFields" HeaderText="要打印的字段">

<ItemStyle HorizontalAlign="Left" Width="46%"></ItemStyle>

</asp:BoundColumn>

<asp:ButtonColumn Text="编辑" CommandName="Edit"></asp:ButtonColumn>

<asp:TemplateColumn>

<ItemTemplate>

<asp:LinkButton id="LinkButton1" runat="server" CommandName="Delete">删除
</asp:LinkButton>

</ItemTemplate>

</asp:TemplateColumn>

<asp:ButtonColumn Text="设置表格行" CommandName="Update"></asp:ButtonColumn>

</Columns>

<PagerStyle Font-Size="9pt" HorizontalAlign="Right" ForeColor="#3366FF"
Mode="NumericPages"></PagerStyle>

</asp:datagrid></td>

</tr>

</table>

......
<table cellSpacing="2" cellPadding="2" width="100%" align="right" border="0">

<tr>

<td align="right" width="10%"><FONT face="宋体">选择关联业务表:</FONT></td>

<td><asp:dropdownlist id="DropDownList1" runat="server"
DataValueField="newtablename" DataTextField="Description">
</asp:dropdownlist>
</td>

<td width="55%"></td>

</tr>

<tr>

<td align="right"><FONT face="宋体">要打印的表格名称:</FONT></td>

<td><asp:textbox id="TextBox1" runat="server" Columns="50"></asp:textbox></td>

<td></td>

</tr>

<tr>

<td align="right"><FONT face="宋体"><FONT face="宋体">一行最多打印几项:</FONT>
</FONT></td>

<td><asp:textbox id="TextBox2" runat="server" Columns="1"></asp:textbox></td>

<td></td>

</tr>

<tr>

<td align="right"> </td>

<td align="center">   
<asp:button id="Button2" 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>

<td></td>

</tr>

</table>

</form>
......
2、PrintedTable.aspx.cs文件代码
......
using System.Text.RegularExpressions;
using DataAccess;
namespace workflow.admin.PrintedTable

{

public class PrintedTable : System.Web.UI.Page

{

protected System.Web.UI.WebControls.DataGrid DataGrid1;

protected System.Web.UI.WebControls.DropDownList DropDownList1;

protected System.Web.UI.WebControls.TextBox TextBox1;

protected System.Web.UI.WebControls.TextBox TextBox2;

protected System.Web.UI.WebControls.Button Button1;

protected System.Web.UI.WebControls.Button Button2;

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");
//定义页面状态变量保存要编辑的printedtableid.

ViewState["editedprintedtableid"]="";

string strSql="select *,(Ower+'.'+TableName) as newtablename from
RelatedTables";

//Response.Write(strSql);

DataView dv=new Base().SQLExeDataView(strSql);

DropDownList1.DataSource=dv;

DropDownList1.DataBind();
BindGrid();

}

Label1.Text="";

}
void BindGrid()

{

string strSql="select * from PrintedTable";

DataSet ds=new Base().SQLExeDataSet(strSql);

if(ds !=null & ds.Tables[0].Rows.Count>=1)

{

DataGrid1.DataSource=ds;

DataGrid1.DataBind();

}

}
//保存按钮

private void Button2_Click(object sender, System.EventArgs e)

{

if(! SavePrintedTable())

return;

BindGrid();

//重新初始化输入控件

TextBox1.Text="";

TextBox2.Text="";

//将ViewState["editedprintedtableid"]置空,否则编辑修改记录后无法添加新记录(仍然是
//修改记录).

ViewState["editedprintedtableid"]="";

}
bool SavePrintedTable()

{

string relatedtable=DropDownList1.SelectedValue;

string printedtablename=TextBox1.Text.Trim();

string columnsperrow=TextBox2.Text.Trim();

if(printedtablename == "" || columnsperrow == "")

{

Label1.Text="输入不能为空";

return false;

}

Regex reg=new Regex(@"^[1-9]\d*$");

if(! reg.IsMatch(columnsperrow))

{

Label1.Text="每行的最大项数应为大于(或等于)1的数字";

return false;

}

reg=new Regex(@"^[\u4e00-\u9fa5]*\w*$");//汉字、字母、下划线

if(! reg.IsMatch(printedtablename))

{

Label1.Text="请输入正确的打印表格名称";

return false;

}

//获得该流程关联的业务表的标识字段名

string strSql="Select name from syscolumns Where ID=OBJECT_ID
('"+relatedtable+"') and autoval is not null";

Base basecode=new Base();

DataSet ds=basecode.SQLExeDataSet(strSql);

if(ds == null)

{

Label1.Text=basecode.BaseSqlErrDes;

return false;

}

if(ds.Tables[0].Rows.Count<1 )

{

Label1.Text="数据表"+relatedtable+"没有定义标识字段,请先添加一个标识字段再定
义其要打印的表格.";

return false;

}

string identityfield=ds.Tables[0].Rows[0]["name"].ToString();
string printedtableid=ViewState["editedprintedtableid"].ToString();

//如果ViewState["editedprintedtableid"]为空则执行添加新记录

if(printedtableid == "")

{

//判断输入的打印表格名称是否重复

strSql="select PrintedTableID from PrintedTable where PrintedTableName
=@PrintedTableName and RelatedTable='"+relatedtable+"'";

SqlCommand cmd=new SqlCommand(strSql);

cmd.Parameters.Add("@PrintedTableName",SqlDbType.VarChar);

cmd.Parameters["@PrintedTableName"].Value=printedtablename;

ds=basecode.SQLExeDataSet(cmd);

if(ds !=null & ds.Tables[0].Rows.Count>=1)

{

Label1.Text="与所选业务表关联的打印表格名称已存在,请重新输入.";

return false;

}

//插入新记录

strSql="insert PrintedTable(ColumnsCount,RelatedTable,IdentityField,
PrintedTableName) values("+columnsperrow+",'"+relatedtable+"','"
+identityfield+"',@PrintedTableName)";

cmd=new SqlCommand(strSql);

cmd.Parameters.Add("@PrintedTableName",SqlDbType.VarChar);

cmd.Parameters["@PrintedTableName"].Value=printedtablename;

//Response.Write(strSql);

if(! basecode.SQLExeNonQuery_proc(cmd))

{

Label1.Text=basecode.BaseSqlErrDes;

return false;

}

//获取新记录的编号

strSql="select PrintedTableID from PrintedTable where PrintedTableName
=@PrintedTableName and RelatedTable='"+relatedtable+"'";

cmd=new SqlCommand(strSql);

cmd.Parameters.Add("@PrintedTableName",SqlDbType.VarChar);

cmd.Parameters["@PrintedTableName"].Value=printedtablename;

ds=basecode.SQLExeDataSet(cmd);

}

else//如果ViewState["editedprintedtableid"]不为空则执行修改记录

{

//判断输入的打印表格名称是否重复

strSql="select PrintedTableID from PrintedTable where PrintedTableID
!="+printedtableid+" and PrintedTableName=@PrintedTableName and
RelatedTable='"+relatedtable+"'";

SqlCommand cmd=new SqlCommand(strSql);

cmd.Parameters.Add("@PrintedTableName",SqlDbType.VarChar);

cmd.Parameters["@PrintedTableName"].Value=printedtablename;

ds=basecode.SQLExeDataSet(cmd);

if(ds !=null & ds.Tables[0].Rows.Count>=1)

{

Label1.Text="与所选业务表关联的打印表格名称已存在,请重新输入.";

return false;

}

//修改记录(如果选择了不同的业务表,这里应该要删除原来的打印行记录)

strSql="update PrintedTable set PrintedTableName=@PrintedTableName,
RelatedTable='"+relatedtable+"',IdentityField='"+identityfield+"',
ColumnsCount="+columnsperrow+" where PrintedTableID="+printedtableid;

cmd=new SqlCommand(strSql);

cmd.Parameters.Add("@PrintedTableName",SqlDbType.VarChar);

cmd.Parameters["@PrintedTableName"].Value=printedtablename;

//Response.Write(strSql);

if(! basecode.SQLExeNonQuery_proc(cmd))

{

Label1.Text=basecode.BaseSqlErrDes;

return false;

}

}

return true;

}
//删除
private void DataGrid1_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)

{

string printedtableid=DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
//删除打印表记录以及关联的多个打印行记录

string strSql="delete PrintedTable where PrintedTableID="+printedtableid
+";delete PrintedRows where PrintedTableID="+printedtableid;

Base basecode=new Base();

if(! basecode.ExeSQLNoResultTransaction(strSql))

{

Label1.Text=basecode.BaseSqlErrDes;

return;

}

BindGrid();

}
private void DataGrid1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)

{

if(e.Item.ItemType == ListItemType.Item
||e.Item.ItemType == ListItemType.AlternatingItem)

{

LinkButton btn = (LinkButton)(e.Item.Cells[5].Controls[1]);

btn.Attributes.Add("onClick","JavaScript:return confirm('确定删除吗?')");
//分行显示打印字段

e.Item.Cells[3].Attributes.Add("style","WORD-BREAK:break-all;
WORD-WRAP:break-word");

}

}
private void DataGrid1_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)

{

string printedtableid=DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
//设置页面状态变量,SavePrintedTable()据此判断是添加记录还是修改记录

ViewState["editedprintedtableid"]=printedtableid;

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

Base basecode=new Base();

DataSet ds=basecode.SQLExeDataSet(strSql);

DropDownList1.SelectedValue=ds.Tables[0].Rows[0]["RelatedTable"].ToString();

TextBox1.Text=ds.Tables[0].Rows[0]["PrintedTableName"].ToString();

TextBox2.Text=ds.Tables[0].Rows[0]["ColumnsCount"].ToString();

}
//放弃按钮
private void Button3_Click(object sender, System.EventArgs e)

{

TextBox1.Text="";

TextBox2.Text="";

return;

}
//设置打印行按钮
private void DifineTableRows(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)

{

Response.Redirect("PrintedFields.aspx?id="
+DataGrid1.DataKeys[e.Item.ItemIndex].ToString());

}

}

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