<%...@ Page Language="C#" AutoEventWireup="true" StylesheetTheme="Default" Theme="Default"
CodeFile="Dictionary.aspx.cs" Inherits="Admin_Dictionary" %>
<!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>数据字典配置</title>
</head>
<body>
<form id="form1" runat="server">
<fieldset>
<legend>菜单</legend>
<div id="tabsF">
<ul>
<li><a href="OfficeType.aspx" title="办公用品类型"><span>办公用品</span></a></li>
<li><a href="AddTax.aspx" title="增值税税率数据字典"><span>增值税税率</span></a></li>
<li><a href="MachineName.aspx" title="整机机种,收款计划数据字典"><span>整机机种|收款计划</span></a></li>
<li><a href="Default.aspx" title="返回"><span>返回</span></a></li>
</ul>
</div>
</fieldset>
<div>
<fieldset>
<legend>数据字典信息</legend>
<table width="100%">
<tr>
<td rowspan="2" valign="top" style="width: 174px">
<fieldset style="text-align: left">
<legend>数据字典项</legend>
<asp:BulletedList ID="blDataDict" runat="server" DisplayMode="LinkButton" OnClick="blDataDict_Click"
BulletStyle="Square">
<asp:ListItem Selected="True" Value="Dict_PayType">付款类型</asp:ListItem>
<asp:ListItem Value="Dict_PayModel">付款方式</asp:ListItem>
<asp:ListItem Value="Dict_IncType">收款类型</asp:ListItem>
<asp:ListItem Value="Dict_DeliveryModel">交货方式</asp:ListItem>
<asp:ListItem Value="Customer_Type">客户类型</asp:ListItem>
<asp:ListItem Value="Dict_TransModel">运输方式</asp:ListItem>
<asp:ListItem Value="Dict_MachineName">整机名称</asp:ListItem>
<asp:ListItem Value="Provider_Type">供应商类型</asp:ListItem>
<asp:ListItem Value="Dict_IncPlan_Kind">收款性质类型</asp:ListItem>
</asp:BulletedList>
</fieldset>
</td>
<td>
<fieldset>
<legend>相关信息</legend>
<asp:GridView ID="gvDictionary" SkinID="Default_GridView" runat="server" CaptionAlign="Left"
OnRowCancelingEdit="gvDictionary_RowCancelingEdit" OnRowDataBound="gvDictionary_RowDataBound"
OnRowEditing="gvDictionary_RowEditing" OnRowUpdating="gvDictionary_RowUpdating">
<Columns>
<asp:BoundField DataField="DictName" HeaderText="字典名称"></asp:BoundField>
<asp:BoundField DataField="DictCode" HeaderText="字典编号" ReadOnly="True"></asp:BoundField>
<asp:CheckBoxField DataField="IfUsing" HeaderText="是否启用" Text="启用"></asp:CheckBoxField>
<asp:BoundField DataField="Note" HeaderText="备注" />
<asp:CommandField ShowEditButton="True" CausesValidation="False"></asp:CommandField>
</Columns>
</asp:GridView>
</fieldset>
</td>
</tr>
<tr>
<td>
<fieldset>
<legend>添加数据字典信息</legend>
<br />
<table cellpadding="4" id="tableInfo" runat="server">
<tr>
<td align="center" colspan="2">
<asp:Label ID="lblTitle" runat="server" Font-Bold="True"></asp:Label></td>
</tr>
<tr>
<td class="tdbg">
<span style="color: #ff0000">*</span>数据字典编号:</td>
<td align="left">
<asp:TextBox ID="txtDictCode" runat="server" Width="241px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtDictCode"
ErrorMessage="数据字典编号不能为空">*</asp:RequiredFieldValidator></td>
</tr>
<tr>
<td class="tdbg">
<span style="color: #ff0000">*</span>数据字典名称:</td>
<td align="left">
<asp:TextBox ID="txtDictName" runat="server" Width="241px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtDictName"
ErrorMessage="数据字典名称不能为空">*</asp:RequiredFieldValidator></td>
</tr>
<tr>
<td class="tdbg">
是否启用:</td>
<td align="left">
<asp:CheckBox ID="chkUsing" runat="server" Text="启用" Checked="True" />
<asp:Label ID="lblTable" runat="server" Visible="False"></asp:Label></td>
</tr>
<tr>
<td class="tdbg">
备注:<br />
</td>
<td align="left">
<asp:TextBox ID="txtNote" runat="server" Height="60px" TextMode="MultiLine" Width="241px"></asp:TextBox></td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Button ID="btnSave" runat="server" Text="保存" OnClick="btnSave_Click" />
<asp:Button ID="btnCancel" runat="server" Text="取消" CausesValidation="False" OnClick="btnCancel_Click" />
<br />
<asp:Label ID="lblInfo" runat="server"></asp:Label>
</td>
</tr>
</table>
</fieldset>
</td>
</tr>
</table>
</fieldset>
<fieldset>
<legend>业务逻辑</legend>
<ul>
<li>带<span style="color: red">*</span>项不能为空</li>
</ul>
</fieldset>
</div>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True"
ShowSummary="False" />
</form>
</body>
</html>
以上HTML,代码和普通的页面代码很相似,本页面完成的功能是,通过点击左边的链接,动态的调用,不同的表中的数据,但是,表中的字段名称,有的是不同的,因此需要我根据实际情况,进行动态绑定,以下页面代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Admin_Dictionary : System.Web.UI.Page
...{
//define class
WebUtility.DictionaryBase dict=new WebUtility.DictionaryBase();
protected void Page_Load(object sender, EventArgs e)
...{
if (!Page.IsPostBack)
...{
//instantiation class
dict.DictionaryTableName = WebUtility.DictionaryBase.DictType.Dict_PayType.ToString();
this.lblTable.Text = WebUtility.DictionaryBase.DictType.Dict_PayType.ToString();
//set datasource
this.gvDictionary.Caption = "付款类型"+" 数据字典列表";
this.lblTitle.Text = "添加付款类型 数据字典";
this.gvDictionary.DataSource = dict.DataSetDictionary();
this.gvDictionary.DataBind();
}
}
protected void blDataDict_Click(object sender, BulletedListEventArgs e)
...{
//set caption and title
this.gvDictionary.Caption = this.GetCaption(this.blDataDict.Items[e.Index].Text);
this.lblTitle.Text = this.GetTitle(this.blDataDict.Items[e.Index].Text);
//clear add item information
this.ClearInfo();
//set tablename to label
this.lblTable.Text = this.blDataDict.Items[e.Index].Value;
this.gvBind();
//modify add info area
}
private string GetCaption(string strItem)
...{
return strItem+" 数据字典列表";
}
private string GetTitle(string strItem)
...{
return " 添加" + strItem + " 数据字典";
}
private void ClearInfo()
...{
this.txtDictCode.Text = "";
this.txtDictName.Text = "";
this.txtNote.Text = "";
this.lblInfo.Text = "";
}
private int GetValue(bool bol)
...{
if (bol)
return 1;
else
return 0;
}
protected void btnCancel_Click(object sender, EventArgs e)
...{
this.ClearInfo();
}
protected void btnSave_Click(object sender, EventArgs e)
...{
dict.DictionaryTableName = this.lblTable.Text;
if (dict.IsDictionary(this.txtDictCode.Text))
...{
this.lblInfo.ForeColor = System.Drawing.Color.Red;
this.lblInfo.Text = this.txtDictCode.Text + " 编号已经存在,请重新填写";
return;
}
//set value to member
dict.DictionCode = this.txtDictCode.Text;
dict.DictionName = this.txtDictName.Text;
dict.Note = this.txtNote.Text;
dict.IfUsing = this.GetValue(this.chkUsing.Checked);
//transfer member to add operate
int i = this.dict.AddDictionary();
if (i > 0)
...{
this.ClearInfo();
this.lblInfo.ForeColor = System.Drawing.Color.Green;
this.lblInfo.Text = "数据字典添加操作成功";
this.gvBind();
}
else
...{
this.lblInfo.ForeColor = System.Drawing.Color.Red;
this.lblInfo.Text = "数据字典添加操作失败!";
}
}
private void gvBind()
...{
dict.DictionaryTableName = this.lblTable.Text;
// when table is dict_machineName,clear all gridview columns
// and add new column
if (this.lblTable.Text == WebUtility.DictionaryBase.DictType.Dict_MachineName.ToString())
...{
this.gvDictionary.Columns.Clear();
//define class
System.Web.UI.WebControls.BoundField field;
//instantiation class
field = new BoundField();
field.DataField = "TypeName";
field.HeaderText = "整机类型";
this.gvDictionary.Columns.Add(field);
field = new BoundField();
field.DataField = "Type";
field.HeaderText = "整机编号";
field.ReadOnly = true;
this.gvDictionary.Columns.Add(field);
System.Web.UI.WebControls.CheckBoxField chkField = new CheckBoxField();
chkField.DataField = "IfUsing";
chkField.Text = "启用";
chkField.HeaderText = "是否启用";
this.gvDictionary.Columns.Add(chkField);
field = new BoundField();
field.DataField = "Note";
field.HeaderText = "备注";
this.gvDictionary.Columns.Add(field);
System.Web.UI.WebControls.CommandField cmdField = new CommandField();
cmdField.ShowEditButton = true;
cmdField.CausesValidation = false;
this.gvDictionary.Columns.Add(cmdField);
}
//if table is Customer_Type or Provider_Type
else if (this.lblTable.Text == WebUtility.DictionaryBase.DictType.Customer_Type.ToString() ||
this.lblTable.Text == WebUtility.DictionaryBase.DictType.Provider_Type.ToString())
...{
this.gvDictionary.Columns.Clear();
//define class
System.Web.UI.WebControls.BoundField field;
//instantiation class
field = new BoundField();
field.DataField = "TypeName";
field.HeaderText = "类型名称";
this.gvDictionary.Columns.Add(field);
field = new BoundField();
field.DataField = "TypeCode";
field.HeaderText = "类型编号";
field.ReadOnly = true;
this.gvDictionary.Columns.Add(field);
System.Web.UI.WebControls.CheckBoxField chkField = new CheckBoxField();
chkField.DataField = "IfUsing";
chkField.Text = "启用";
chkField.HeaderText = "是否启用";
this.gvDictionary.Columns.Add(chkField);
field = new BoundField();
field.DataField = "Note";
field.HeaderText = "备注";
this.gvDictionary.Columns.Add(field);
System.Web.UI.WebControls.CommandField cmdField = new CommandField();
cmdField.ShowEditButton = true;
cmdField.CausesValidation = false;
this.gvDictionary.Columns.Add(cmdField);
}
else
...{
this.gvDictionary.Columns.Clear();
//define class
System.Web.UI.WebControls.BoundField field;
//instantiation class
field = new BoundField();
field.DataField = "DictName";
field.HeaderText = "字典名称";
this.gvDictionary.Columns.Add(field);
field = new BoundField();
field.DataField = "DictCode";
field.HeaderText = "字典编号";
field.ReadOnly = true;
this.gvDictionary.Columns.Add(field);
System.Web.UI.WebControls.CheckBoxField chkField = new CheckBoxField();
chkField.DataField = "IfUsing";
chkField.Text = "启用";
chkField.HeaderText = "是否启用";
this.gvDictionary.Columns.Add(chkField);
field = new BoundField();
field.DataField = "Note";
field.HeaderText = "备注";
this.gvDictionary.Columns.Add(field);
System.Web.UI.WebControls.CommandField cmdField = new CommandField();
cmdField.ShowEditButton = true;
cmdField.CausesValidation = false;
this.gvDictionary.Columns.Add(cmdField);
}
this.gvDictionary.DataSource = dict.DataSetDictionary();
this.gvDictionary.DataBind();
}
protected void gvDictionary_RowDataBound(object sender, GridViewRowEventArgs e)
...{
if (e.Row.RowType == DataControlRowType.DataRow)
...{
e.Row.Attributes.Add("onmouseover", "e=this.style.backgroundColor; this.style.backgroundColor='linen'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=e");
}
}
protected void gvDictionary_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
...{
this.gvDictionary.EditIndex = -1;
this.gvBind();
}
protected void gvDictionary_RowEditing(object sender, GridViewEditEventArgs e)
...{
this.gvDictionary.EditIndex = e.NewEditIndex;
this.gvBind();
}
protected void gvDictionary_RowUpdating(object sender, GridViewUpdateEventArgs e)
...{
TextBox txtName = (TextBox)this.gvDictionary.Rows[e.RowIndex].Cells[0].Controls[0];
TextBox txtNote = (TextBox)this.gvDictionary.Rows[e.RowIndex].Cells[3].Controls[0];
CheckBox chkIfUsing = (CheckBox)this.gvDictionary.Rows[e.RowIndex].Cells[2].Controls[0];
//get value to member;
dict.DictionaryTableName = this.lblTable.Text;
dict.DictionName = txtName.Text;
dict.DictionCode = this.gvDictionary.Rows[e.RowIndex].Cells[1].Text;
dict.IfUsing = this.GetValue(chkIfUsing.Checked);
dict.Note = txtNote.Text;
//transfer method begin update
dict.UpdateDictionary();
//exit edit mode;
this.gvDictionary.EditIndex = -1;
//begin databind
this.gvBind();
}
}
其中, 最重要的是,gvBind()函数,首先将GridView的所有列进行清除,之后根据需要,进行添加操作
private void gvBind()
{
dict.DictionaryTableName = this.lblTable.Text;
// when table is dict_machineName,clear all gridview columns
// and add new column
if (this.lblTable.Text == WebUtility.DictionaryBase.DictType.Dict_MachineName.ToString())
{
this.gvDictionary.Columns.Clear();
//define class
System.Web.UI.WebControls.BoundField field;
//instantiation class
field = new BoundField();
field.DataField = "TypeName";
field.HeaderText = "整机类型";
this.gvDictionary.Columns.Add(field);
field = new BoundField();
field.DataField = "Type";
field.HeaderText = "整机编号";
field.ReadOnly = true;
this.gvDictionary.Columns.Add(field);
System.Web.UI.WebControls.CheckBoxField chkField = new CheckBoxField();
chkField.DataField = "IfUsing";
chkField.Text = "启用";
chkField.HeaderText = "是否启用";
this.gvDictionary.Columns.Add(chkField);
field = new BoundField();
field.DataField = "Note";
field.HeaderText = "备注";
this.gvDictionary.Columns.Add(field);
System.Web.UI.WebControls.CommandField cmdField = new CommandField();
cmdField.ShowEditButton = true;
cmdField.CausesValidation = false;
this.gvDictionary.Columns.Add(cmdField);
}
//if table is Customer_Type or Provider_Type
else if (this.lblTable.Text == WebUtility.DictionaryBase.DictType.Customer_Type.ToString() ||
this.lblTable.Text == WebUtility.DictionaryBase.DictType.Provider_Type.ToString())
{
this.gvDictionary.Columns.Clear();
//define class
System.Web.UI.WebControls.BoundField field;
//instantiation class
field = new BoundField();
field.DataField = "TypeName";
field.HeaderText = "类型名称";
this.gvDictionary.Columns.Add(field);
field = new BoundField();
field.DataField = "TypeCode";
field.HeaderText = "类型编号";
field.ReadOnly = true;
this.gvDictionary.Columns.Add(field);
System.Web.UI.WebControls.CheckBoxField chkField = new CheckBoxField();
chkField.DataField = "IfUsing";
chkField.Text = "启用";
chkField.HeaderText = "是否启用";
this.gvDictionary.Columns.Add(chkField);
field = new BoundField();
field.DataField = "Note";
field.HeaderText = "备注";
this.gvDictionary.Columns.Add(field);
System.Web.UI.WebControls.CommandField cmdField = new CommandField();
cmdField.ShowEditButton = true;
cmdField.CausesValidation = false;
this.gvDictionary.Columns.Add(cmdField);
}
else
{
this.gvDictionary.Columns.Clear();
//define class
System.Web.UI.WebControls.BoundField field;
//instantiation class
field = new BoundField();
field.DataField = "DictName";
field.HeaderText = "字典名称";
this.gvDictionary.Columns.Add(field);
field = new BoundField();
field.DataField = "DictCode";
field.HeaderText = "字典编号";
field.ReadOnly = true;
this.gvDictionary.Columns.Add(field);
System.Web.UI.WebControls.CheckBoxField chkField = new CheckBoxField();
chkField.DataField = "IfUsing";
chkField.Text = "启用";
chkField.HeaderText = "是否启用";
this.gvDictionary.Columns.Add(chkField);
field = new BoundField();
field.DataField = "Note";
field.HeaderText = "备注";
this.gvDictionary.Columns.Add(field);
System.Web.UI.WebControls.CommandField cmdField = new CommandField();
cmdField.ShowEditButton = true;
cmdField.CausesValidation = false;
this.gvDictionary.Columns.Add(cmdField);
}
this.gvDictionary.DataSource = dict.DataSetDictionary();
this.gvDictionary.DataBind();
}