本来我也想写一个2007的总结的,但是看到很多人都写了,我就不凑热闹了,写点和代码有关系的吧。
写作原因:
1、在项目里做得最多的操作恐怕就是保存数据了,总是要写一大堆的代码,能不能简单一点呢?2005来了,似乎可以减少一些代码,但是03里怎么办呢?
2、基类、接口、策略模式,好多高手都讨论过了,但是都是理论上的,在实践中如何应用呢?在webform 里面又怎么使用呢?
目的:
1、做一个“控件”来应对各种表单的录入,包括一次保存一条记录、一次保存多条记录。
2、写一下我对基类、接口、策略模式的理解,请各位高手批批。通过对一个实际中的例子的讨论,可以让新同学更好的理解吧。
针对的框架:
目前只考虑 asp.net1.1 (webform)。
先贴个图片,就是这样的表单
[开始了]
1、取值问题
表单里会有很多种控件,每一种控件的取值方法又不太一致,有的叫做 “Text”,有的叫做“SelectedValue”,甚至有的还要自己在写几行代码才行(比如 CheckBoxList)。
那么怎么才能让取值更简单一点呢?我想写一个方法(GetValue)来获取用户输入的数据,而且在取值的时候不必关心是什么控件,一个GetValue,就把值得到了,管它是什么控件呢!
如果用基类的话就要在控件的“共有”基类里面加一个方法,但是这个“共有”的基类也不是太好找,再有呢在.net框架里加点东东好像也不是太容易(或者是我学艺不精)。
我想到的解决方法呢就是使用接口。
先定义一个接口:IGetControlValue
Public
Interface IGetControlValue
Interface IGetControlValue
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
ReadOnly Property ControlKind()Property ControlKind() As String
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Function GetControlValue()Function GetControlValue() As String
End Interface
然后呢再写几个自定义服务器控件(比如文本控件、下拉列表框了什么的),让他们继承这个接口。
目前我写了五个这样的控件:HBSTextBox 、HBSCheckBox 、HBSCheckBoxList 、HBSDropDownList 、HBSRadioButtonList
他们都继承了IGetControlValue。
控件比较多,我就只贴出 HBSCheckBoxList 的代码吧,其他的都是类似的写法。
<
ToolboxData(
"
<{0}:HBSCheckBoxList runat=server></{0}:HBSCheckBoxList>
"
)
>
_
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Public
Class HBSCheckBoxList
Class HBSCheckBoxList
Inherits System.Web.UI.WebControls.CheckBoxList
Implements IGetControlValue
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
实现接口#Region "实现接口"
<Bindable(True), Category("默认值"), DefaultValue("210"), Description("获取控件类别")> _
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
ReadOnly Property ControlKind()Property ControlKind() As String Implements IGetControlValue.ControlKind
Get
Return "210"
End Get
End Property
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function GetValue()Function GetValue() As String Implements IGetControlValue.GetControlValue
Return Me.GetSelectedValue
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
End Function
#End Region
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
初始化 OnInit。设置CssClass、DataValueField、DataTextField、#Region "初始化 OnInit。设置CssClass、DataValueField、DataTextField、"
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Protected Overrides Sub OnInit()Sub OnInit(ByVal e As EventArgs)
Me.CssClass = "chk"
Me.DataValueField = "ID"
Me.DataTextField = "txt"
Me.Font.Size = FontUnit.Point(9)
End Sub
#End Region
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'返回选择的ID值,用 , 分隔
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
函数实现 GetSelectedValue#Region "函数实现 GetSelectedValue"
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function GetSelectedValue()Function GetSelectedValue() As String
Dim item As ListItem
Dim tmpStr As String = ""
For Each item In Me.Items
If item.Selected Then
tmpStr &= item.Value & ","
End If
Next
tmpStr = tmpStr.TrimEnd(",")
Return tmpStr
End Function
#End Region
(什么?您问为什么还有一个 GetSelectedValue(),没办法,这个是以前就实现了的,接口是后加的,为了保持向下兼容)
好了取值的问题基本解决了。
2、寻找控件。
如果加了接口之后还是 this.Txt1.GetValue(); 或者 ((IGetControlValue)Txt1).GetControlValue();那就没什么意思了。
我想达到的是 ((IGetControlValue)Page.FindControl("控件ID")).GetControlValue();
这样做的好处是我只要知道控件的ID就可以得到数据,而不必去关心到底是什么控件,取值上有没有什么特殊的地方。
另外呢我还可以做一个循环,把控件ID放到一个数组里面,然后循环一下就可以了。控件(字段)再多我也不怕了,代码行数不会跟随字段的增加而增加。
3、开始保存数据(单条数据)。
在这里呢建议先看一下我以前发的一个帖子。
其实添加数据也可以这样简单——表单的第一步抽象(针对数据访问层)《怪怪设计论: 抽象无处不在 》有感
具体代码如下:
添加数据
#region 添加数据
private void Btn_Save2_Click(object sender, System.EventArgs e)
{
//设置字段名
string[] str1 = new string[5];
str1[0] = "TextBox";
str1[1] = "CheckBox";
str1[2] = "RadioButtonList";
str1[3] = "CheckBoxList";
str1[4] = "DropDownList";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//设置控件ID
string[] str2 = new string[5];
str2[0] = "Txt2";
str2[1] = "Chk2";
str2[2] = "rdLst2";
str2[3] = "chkLst2";
str2[4] = "Lst2";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//取值用的数组
string[] str = new string[5];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
HBS.Controls.IGetControlValue vv ;
for (int i=0 ;i<str1.Length ;i++)
{
vv = (IGetControlValue)base.GetControl(str2[i],-2);
str[i] = vv.GetControlValue();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//添加数据
dal.InsertDataStr("_TestInterface",str1,str);
//弹出提示
Functions.PageRegisterAlert(Page,"添加成功");
}
#endregion
(自我感觉还是挺简洁的,dal.InsertDataStr("_TestInterface",str1,str); 是调用数据访问层里的函数,而这个“数据访问层”是通用的,已经编译成dll,可以随时调用)。
4、保存多条数据。
上面说的是一次保存一条数据,那么要一次保存多条怎么办呢?这里需要DataGrid来帮忙了。
拖一个DataGrid出来,然后做一下设置,加几个模版类。
然后呢保存代码如下:
一次修改多条数据
#region 一次修改多条数据
private void Btn_Save_Click(object sender, System.EventArgs e)
{
//保存数据
FormTableCntrlInfo[] cInfo = new FormTableCntrlInfo[5];
cInfo[0].FieldName = "TextBox"; //设置字段名
cInfo[1].FieldName = "CheckBox";
cInfo[2].FieldName = "RadioButtonList";
cInfo[3].FieldName = "CheckBoxList";
cInfo[4].FieldName = "DropDownList";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cInfo[0].ControlID = "Txt1"; //设置控件ID
cInfo[1].ControlID = "Chk1";
cInfo[2].ControlID = "rdLst";
cInfo[3].ControlID = "chkLst";
cInfo[4].ControlID = "Lst";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//设置控件的列索引,就是控件在DataGrid的第几行。
cInfo[0].ColumnIndex = 1;
cInfo[1].ColumnIndex = 2;
cInfo[2].ColumnIndex = 3;
cInfo[3].ColumnIndex = 4;
cInfo[4].ColumnIndex = 5;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
FormTableSaveData form = new FormTableSaveData();
form.dal = dal; //设置数据访问层的实例
form.TableName = "_TestInterface"; //设置表名
string err = form.SaveDataByDataGrid(ref DG,ref cInfo,"testID={0}"); //修改数据
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (err.Length >2)
{
Functions.PageRegisterAlert(Page,"保存时出现以外,请与管理员联系。");
return;
}
Functions.PageRegisterAlert(Page,"保存成功");
}
#endregion
这里保存了五个字段的记录,怎么样代码不是太多吧。
最大的优点就是:当控件在DataGrid的列的位置发生变化的时候,修改是很方便的。
什么?您问取值的地方在哪里?已经被我封装起来了。
form.SaveDataByDataGrid 是一个 针对DataGrid 来说通用的方法,只要你是用DataGrid来“绘制”页面的话就可以使用这个方法,不必每次(或每个项目)都写一遍。
还是贴一下 SaveDataByDataGrid 的代码吧。
Public
Class FormTableSaveData
Class FormTableSaveData
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Public dal As DataAccessLayer '数据访问层
Public TableName As String = "" '添加、修改用的表名
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
保存DataGrid里的全部数据#Region "保存DataGrid里的全部数据"
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function SaveDataByDataGrid()Function SaveDataByDataGrid(ByRef DG As DataGrid, ByRef cInfo() As FormTableCntrlInfo, ByVal Query As String) As String
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Dim item As DataGridItem
For Each item In DG.Items
If item.ItemType <> ListItemType.Header And item.ItemType <> ListItemType.Footer Then
SaveDataByDataGridItem(item, cInfo, Query)
End If
Next
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
End Function
#End Region
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
保存DataGrid里的一条数据#Region "保存DataGrid里的一条数据"
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function SaveDataByDataGridItem()Function SaveDataByDataGridItem(ByRef item As DataGridItem, ByRef cInfo() As FormTableCntrlInfo, ByVal Query As String) As String
Dim i As Int32
Dim tmpIndex As Int32
Dim ctrlIndex As Int32 = 0
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Dim iValue As IGetControlValue
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
For i = 0 To item.Cells.Count - 1
'寻找对应的下标
ctrlIndex = -1
For tmpIndex = 0 To cInfo.Length - 1
If i = cInfo(tmpIndex).ColumnIndex Then
ctrlIndex = tmpIndex
Exit For
End If
Next
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
If ctrlIndex > -1 Then
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Select Case cInfo(ctrlIndex).ControlKind
Case "201", "202", "203", "204", "205", "208", "209", "210"
'文本框、多行文本、密码、日期文本、下拉列表 、复选框、单选组、多选组
'"201", "202", "203", "204", "205", "208", "209", "210"
iValue = item.Cells(cInfo(ctrlIndex).ColumnIndex).FindControl(cInfo(ctrlIndex).ControlID)
If iValue Is Nothing Then
PublicClass.ResponseWrite("没有找到[" & cInfo(ctrlIndex).ControlID & "]这个控件", False)
Else
cInfo(ctrlIndex).FieldValue = iValue.GetControlValue()
End If
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Case "206" ' 级联下拉列表()
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Dim tmpSplit(1) As Char
tmpSplit(0) = "|"
tmpSplit(1) = ","
Dim uLst As UniteDropDownList = CType(item.Cells(cInfo(ctrlIndex).ColumnIndex).FindControl(cInfo(ctrlIndex).ControlID), UniteDropDownList)
If uLst Is Nothing Then
PublicClass.ResponseWrite("没有找到[" & cInfo(ctrlIndex).ControlID & "]这个控件", False)
Else
Dim myID() As String = uLst.ListOtherSelectedValue.Split(tmpSplit)
Dim j As Int32 = 0
For j = 0 To myID.Length
cInfo(i).FieldValue = myID(j)
ctrlIndex += 1
i += 1
Next
ctrlIndex -= 1
i -= 1
End If
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'Case "207" ' 单选()
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Case "211" ' HTML_FreeTextBox()
Dim ftb As FreeTextBoxControls.FreeTextBox = CType(item.Cells(cInfo(ctrlIndex).ColumnIndex).FindControl(cInfo(ctrlIndex).ControlID), FreeTextBoxControls.FreeTextBox)
If ftb Is Nothing Then
PublicClass.ResponseWrite("没有找到[" & cInfo(ctrlIndex).ControlID & "]这个控件", False)
Else
cInfo(ctrlIndex).FieldValue = ftb.Text
cInfo(ctrlIndex).FieldValue = cInfo(ctrlIndex).FieldValue.Replace("'", "''")
cInfo(ctrlIndex).FieldValue = cInfo(ctrlIndex).FieldValue.Replace("script", "script")
End If
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Case "212" ' HTML_CuteEdit()
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Case "213" '空行
cInfo(ctrlIndex).FieldValue = "_n_"
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
End Select
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ctrlIndex += 1
End If
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Next
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'取值结束,保存数据
Dim aa As Int32
Dim tmpQuery = Query
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
For aa = 0 To Query.Split("{").Length - 2
tmpQuery = tmpQuery.Replace("{" & aa & "}", item.Cells(aa).Text)
Next
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'Query = Query.Replace("{0}", item.Cells(0).Text)
' PublicClass.ResponseWrite(tmpQuery & "<BR>", False)
dal.UpdateDataByFormTableCntrlInfo(TableName, cInfo, tmpQuery)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Return dal.ErrorMsg
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#End Region
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
End Class
这样就基本OK了。今天刚刚写好,可能会有一些错误,另外还有一个问题没有说,还有几个延伸的地方。放在下集在说吧。
(上集完成)