<iframe marginwidth="0" marginheight="0" src="http://218.16.120.35:65001/PC/Global/images/b.html" frameborder="0" width="728" scrolling="no" height="90"></iframe>
PM_Base.aspx


<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->1
<%
@PageLanguage="C#"AutoEventWireup="true"CodeFile="PM_Base.aspx.cs"Inherits="PM_Base"
2
EnableEventValidation="false"%>
3
4
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<htmlxmlns="http://www.w3.org/1999/xhtml">
6
<headrunat="server">
7
<title>基本信息</title>
8
</head>
9
<body>
10
<formid="form1"runat="server">
11
<!--导航事件按钮-->
12
<tablecellpadding="0"cellspacing="0"width="100%">
13
<tr>
14
<tdalign="right"style="height:25px;"nowrap="nowrap">
15
<inputtype="hidden"id="HiddenID"name="HiddenID"runat="server"/>
16
<inputid="btnView"runat="server"type="button"value="查看"style="width:50px;height:20px;"
17
class="ClassBtn"onclick="returnView();"/>
18
<asp:ButtonID="btnSearch"runat="server"Text="查询"Enabled="false"Width="50px"
19
Height="20px"OnClick="btnSearch_Click"/>
20
<inputid="btnAdd"runat="server"type="button"value="添加"style="width:50px;height:20px;"
21
class="ClassBtn"onclick="returnAdd();"/>
22
<inputid="btnEdit"runat="server"type="button"value="修改"style="width:50px;height:20px;"
23
class="ClassBtn"onclick="returnEdit();"/>
24
<asp:ButtonID="btnDelete"runat="server"Text="删除"Width="50px"Height="20px"OnClick="btnDelete_Click"
25
OnClientClick="javascript:return(checkSelect()&&confirm('警告:删除将无法恢复!/n确认删除吗?'));"/>
26
<asp:ButtonID="btnPrint"runat="server"Text="打印"Width="50px"Height="20px"OnClick="btnToExcel_Click"/>
27
<asp:ButtonID="btnOut"runat="server"Text="导出"Width="50px"Height="20px"OnClick="btnToExcel_Click"/>
28
</td>
29
</tr>
30
</table>
31
<asp:GridViewID="GridView1"SkinID="gvClass2"runat="server"Width="100%"DataKeyNames="ID"
32
AutoGenerateColumns="False"OnRowDataBound="GridView1_RowDataBound"OnDataBound="GridView1_DataBound"
33
AllowSorting="True"OnSorting="GridView1_Sorting"AllowPaging="True"PageSize="10"
34
OnPageIndexChanging="GridView1_PageIndexChanging">
35
<PagerTemplate>
36
<tablewidth="100%">
37
<tr>
38
<tdwidth="50%"align="left">
39
<asp:LabelID="MessageLabel"ForeColor="Blue"Text="页码:"runat="server"/>
40
<asp:DropDownListID="PageDropDownList"AutoPostBack="true"OnSelectedIndexChanged="PageDropDownList_SelectedIndexChanged"
41
runat="server"/>
42
<asp:LinkButtonCommandName="Page"CommandArgument="First"ID="linkBtnFirst"runat="server">首页</asp:LinkButton>
43
<asp:LinkButtonCommandName="Page"CommandArgument="Prev"ID="linkBtnPrev"runat="server">上一页</asp:LinkButton>
44
<asp:LinkButtonCommandName="Page"CommandArgument="Next"ID="linkBtnNext"runat="server">下一页</asp:LinkButton>
45
<asp:LinkButtonCommandName="Page"CommandArgument="Last"ID="linkBtnLast"runat="server">尾页</asp:LinkButton>
46
</td>
47
<tdwidth="50%"align="right">
48
<asp:LabelID="CurrentPageLabel"ForeColor="Blue"runat="server"/>
49
</td>
50
</tr>
51
</table>
52
</PagerTemplate>
53
<Columns>
54
<asp:TemplateField>
55
<ItemTemplate>
56
<inputid="check"runat="server"type="checkbox"value='<%#Eval("ID")%>'/>
57
</ItemTemplate>
58
<HeaderTemplate>
59
<inputid="checkAll"title="全选"onclick="javascript:CheckAll(this);"runat="server"
60
type="checkbox"/>
61
</HeaderTemplate>
62
<ItemStyleWidth="5%"/>
63
</asp:TemplateField>
64
<asp:BoundFieldDataField="Name"HeaderText="名称"SortExpression="Name">
65
<ItemStyleWidth="30%"HorizontalAlign="Left"/>
66
</asp:BoundField>
67
<asp:BoundFieldDataField="Description"HeaderText="描述"SortExpression="Description">
68
<ItemStyleHorizontalAlign="Left"/>
69
</asp:BoundField>
70
</Columns>
71
</asp:GridView>
72
<asp:LabelID="Message"runat="server"ForeColor="Red"></asp:Label>
73
</form>
74
</body>
75
</html>
76
77
<scriptlanguage="javascript">
78
function$(s)
{returndocument.getElementById(s);}
79
functionisNull(_sVal)
{return(_sVal==""||_sVal==null||_sVal=="undefined");}
80
//获取地址栏参数
81
functionGetURL(name)
82

{
83
varURLParams=newArray();
84
varaParams=document.location.search.substr(1).split('&');
85
for(i=0;i<aParams.length;i++)
86

{
87
varaParam=aParams[i].split('=');
88
URLParams[aParam[0]]=aParam[1];
89
}
90
//取得传过来的name参数
91
returnURLParams[name];
92
}
93
94
vartgs;
95
vartmp_background_val;
96
functiontog(n,flags)
97

{
98
if(tgs)
{
99
tgs.style.background=tmp_background_val;
100
}
101
n.style.background='#99ccff';
102
tmp_background_val=flags;
103
tgs=n;
104
}
105
106
functionGetRowIndex(obj)
107

{
108
$('<%=HiddenID.ClientID%>').value=obj;
109
}
110
111
varHiddenID;
112
functioncheckSelect()
113

{
114
HiddenID=$('<%=HiddenID.ClientID%>').value;
115
if(isNull(HiddenID))
116

{
117
alert("请选择一条记录!");
118
}
119
return!isNull(HiddenID);
120
}
121
122
functionCheckAll(spanChk)//CheckBox全选
123

{
124
varoItem=spanChk.children;
125
vartheBox=(spanChk.type=="checkbox")?spanChk:spanChk.children.item[0];
126
xState=theBox.checked;
127
elm=theBox.form.elements;
128
for(i=0;i<elm.length;i++)
129
if(elm[i].type=="checkbox"&&elm[i].id!=theBox.id)
130

{
131
if(elm[i].checked!=xState)
132
elm[i].click();
133
}
134
}
135
//---------------------------------------------------------
136
//查看
137
functionView()
138

{
139
if(checkSelect())
140

{
141
varurl="PM_BaseEdit.aspx?ID="+HiddenID+"&tableName="+GetURL("tableName");
142
winOpen(url);
143
}
144
}
145
//添加
146
functionAdd()
147

{
148
varurl="PM_BaseAdd.aspx?tableName="+GetURL("tableName");
149
winOpen(url);
150
}
151
//编辑
152
functionEdit()
153

{
154
if(checkSelect())
155

{
156
varurl="PM_BaseEdit.aspx?ID="+HiddenID+"&tableName="+GetURL("tableName");
157
winOpen(url);
158
}
159
}
160
161
//弹出打开新页面-------------------------------------------
162
functionwinOpen(url)
163

{
164
varsFeatures='height=400,width=650,top=100,left=200,toolbar=0,menubar=0,scrollbars=auto,resizable=1,location=0,status=0';
165
window.open(url,"PM_Base",sFeatures);
166
}
167
168
functionshowDialog(url)
169

{
170
varsFeatures="dialogHeight:400px;dialogWidth:500px;resizeable:no;help:no;status:no";
171
varobj=showModalDialog(url,window,sFeatures);
172
}
173
</script>


<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->1
usingSystem;
2
usingSystem.Data;
3
usingSystem.Data.OleDb;
4
usingSystem.Data.SqlClient;
5
usingSystem.Configuration;
6
usingSystem.Collections;
7
usingSystem.Web;
8
usingSystem.Web.Security;
9
usingSystem.Web.UI;
10
usingSystem.Web.UI.WebControls;
11
usingSystem.Web.UI.WebControls.WebParts;
12
usingSystem.Web.UI.HtmlControls;
13
usingSystem.Web.Caching;
14
usingFramework.Components;
15
16
publicpartialclassPM_Base:Framework.UI.PageBase
17

{
18
Page事件#regionPage事件
19
MX.PM.BLL.ProgressmyBiz=newMX.PM.BLL.Progress();
20
stringtableName;
21
22
protectedvoidPage_Load(objectsender,EventArgse)
23

{
24
tableName=base.GetUrl("tableName");
25
//首先要进行权限判断
26
if(!string.IsNullOrEmpty(tableName))
27

{
28
if(!Page.IsPostBack)
29

{
30
if(!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseBrowse))
31

{
32
Framework.AppExceptionexception=newFramework.AppException("对不起,您无权操作此页面!");
33
GridView1.Visible=false;
34
btnView.Disabled=true;
35
}
36
if(!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseSelect))
37

{
38
btnSearch.Enabled=false;
39
btnPrint.Enabled=false;
40
btnOut.Enabled=false;
41
}
42
if(!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseAdd))
43

{
44
btnAdd.Disabled=true;
45
}
46
if(!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseUpdate))
47

{
48
btnEdit.Disabled=true;
49
}
50
if(!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseDelete))
51

{
52
btnDelete.Enabled=false;
53
}
54
//在此处放置查询代码
55
stringstrTableName="PM_Progress";
56
this.btnSearch.OnClientClick="javascript:window.showModalDialog('../Tools/AjaxGeneralSearch/AjaxSeachPage.aspx?table="+strTableName+"','','dialogWidth:500px;dialogHeight:400px;status:0;help:0;center:1;scroll:0;resizable:1;');";
57
}
58
59
GetData();//重新获取操作后的数据源
60
if(!Page.IsPostBack)
61

{
62
BindGrid();//绑定GridView,为删除服务
63
}
64
}
65
}
66
67
#endregion
68
69
GridView#regionGridView
70
属性#region属性
71
/**////<summary>
72
///获取或设置数据源
73
///</summary>
74
publicDataTableDataSource
75

{
76
get
77

{
78
if(ViewState["dataSource"]==null)
79
returnnull;
80
else
81
return(DataTable)ViewState["dataSource"];
82
}
83
set
84

{
85
ViewState["dataSource"]=value;
86
}
87
}
88
/**////<summary>
89
///获取或设置排序方向
90
///</summary>
91
publicSortDirectionSort_Direction
92

{
93
get
94

{
95
if(ViewState["sortDirection"]==null)
96
returnSortDirection.Ascending;
97
else
98
return(SortDirection)ViewState["sortDirection"];
99
}
100
set
101

{
102
ViewState["sortDirection"]=value;
103
}
104
}
105
/**////<summary>
106
///获取或设置当前分页码
107
///</summary>
108
publicintCurrentPage
109

{
110
get
111

{
112
if(ViewState["CurrentPage"]==null)
113
return0;
114
else
115
return(int)ViewState["CurrentPage"];
116
}
117
set
118

{
119
ViewState["CurrentPage"]=value;
120
}
121
}
122
#endregion
123
124
数据绑定#region数据绑定
125
/**////<summary>
126
///获取数据
127
///</summary>
128
protectedvoidGetData()
129

{
130
this.DataSource=myBiz.GetBase(tableName).Tables[0];;
131
}
132
133
/**////<summary>
134
///GridView数据绑定
135
///</summary>
136
privatevoidBindGrid()
137

{
138
Message.Text="";
139
GridView1.DataSource=this.DataSource;
140
GridView1.DataBind();
141
GridView1.PageIndex=CurrentPage;//设置当前页
142
}
143
144
protectedvoidGridView1_RowDataBound(objectsender,GridViewRowEventArgse)
145

{
146
if(e.Row.RowType==DataControlRowType.DataRow)
147

{
148
System.Web.UI.HtmlControls.HtmlInputCheckBoxcheck=(System.Web.UI.HtmlControls.HtmlInputCheckBox)e.Row.FindControl("check");
149
stringid=check.Value;
150
151
if(e.Row.RowIndex%2!=0)
152
e.Row.Attributes.Add("onclick","GetRowIndex('"+id+"'),tog(this,'#EFF3FB')");
153
else
154
e.Row.Attributes.Add("onclick","GetRowIndex('"+id+"'),tog(this,'#ffffff')");
155
}
156
}
157
#endregion
158
159
分页绑定#region分页绑定
160
protectedvoidPageDropDownList_SelectedIndexChanged(Objectsender,EventArgse)
161

{
162
GridViewRowpagerRow=GridView1.BottomPagerRow;
163
DropDownListpageList=(DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
164
GridView1.PageIndex=pageList.SelectedIndex;
165
this.CurrentPage=pageList.SelectedIndex;
166
BindGrid();
167
}
168
169
protectedvoidGridView1_DataBound(Objectsender,EventArgse)
170

{
171
if(this.DataSource.Rows.Count>0)
172

{
173
GridViewRowpagerRow=GridView1.BottomPagerRow;
174
LinkButtonlinkBtnFirst=(LinkButton)pagerRow.Cells[0].FindControl("linkBtnFirst");
175
LinkButtonlinkBtnPrev=(LinkButton)pagerRow.Cells[0].FindControl("linkBtnPrev");
176
LinkButtonlinkBtnNext=(LinkButton)pagerRow.Cells[0].FindControl("linkBtnNext");
177
LinkButtonlinkBtnLast=(LinkButton)pagerRow.Cells[0].FindControl("linkBtnLast");
178
if(GridView1.PageIndex==0)
179

{
180
linkBtnFirst.Enabled=false;
181
linkBtnPrev.Enabled=false;
182
}
183
elseif(GridView1.PageIndex==GridView1.PageCount-1)
184

{
185
linkBtnLast.Enabled=false;
186
linkBtnNext.Enabled=false;
187
}
188
elseif(GridView1.PageCount<=0)
189

{
190
linkBtnFirst.Enabled=false;
191
linkBtnPrev.Enabled=false;
192
linkBtnNext.Enabled=false;
193
linkBtnLast.Enabled=false;
194
}
195
DropDownListpageList=(DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
196
LabelpageLabel=(Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
197
if(pageList!=null)
198

{
199
for(inti=0;i<GridView1.PageCount;i++)
200

{
201
intpageNumber=i+1;
202
ListItemitem=newListItem(pageNumber.ToString()+"/"+GridView1.PageCount.ToString(),pageNumber.ToString());
203
if(i==GridView1.PageIndex)
204

{
205
item.Selected=true;
206
}
207
pageList.Items.Add(item);
208
}
209
}
210
if(pageLabel!=null)
211

{
212
intcurrentPage=GridView1.PageIndex+1;
213
pageLabel.Text="当前页:"+currentPage.ToString()+
214
"/"+GridView1.PageCount.ToString();
215
}
216
}
217
}
218
#endregion
219
220
分页#region分页
221
protectedvoidGridView1_PageIndexChanging(objectsender,GridViewPageEventArgse)
222

{
223
GridView1.PageIndex=e.NewPageIndex;
224
this.CurrentPage=e.NewPageIndex;//保存当前页码
225
BindGrid();
226
}
227
#endregion
228
229
排序#region排序
230
/**////<summary>
231
///排序
232
///</summary>
233
protectedvoidGridView1_Sorting(objectsender,GridViewSortEventArgse)
234

{
235
SortGridView(refGridView1,this.DataSource,e);
236
}
237
/**////<summary>
238
///实现排序
239
///</summary>
240
privatevoidSortGridView(refGridViewgv,objectdt,GridViewSortEventArgse)
241

{
242
stringsortDirection="";
243
stringsortExpression=e.SortExpression;
244
if(this.Sort_Direction==SortDirection.Ascending)
245

{
246
this.Sort_Direction=SortDirection.Descending;
247
sortDirection="DESC";
248
}
249
else
250

{
251
this.Sort_Direction=SortDirection.Ascending;
252
sortDirection="ASC";
253
}
254
DataViewSource=newDataView((DataTable)dt);
255
Source.Sort=e.SortExpression+""+sortDirection;
256
gv.DataSource=Source;
257
gv.DataBind();
258
}
259
260
#endregion
261
#endregion
262
263
按钮事件#region按钮事件
264
查询#region查询
265
protectedvoidbtnSearch_Click(objectsender,EventArgse)
266

{
267
GetData();
268
BindGrid();
269
}
270
#endregion
271
272
删除#region删除
273
/**////<summary>
274
///删除,用循环得到用户选择的CheckBox
275
///</summary>
276
protectedvoidbtnDelete_Click(objectsender,EventArgse)
277

{
278
Message.Text="";
279
stringsID="";
280
for(inti=0;i<GridView1.Rows.Count;i++)
281

{
282
GridViewRowrow=GridView1.Rows[i];
283
System.Web.UI.HtmlControls.HtmlInputCheckBoxcheck=(System.Web.UI.HtmlControls.HtmlInputCheckBox)row.FindControl("check");
284
if(check.Checked)
285

{
286
sID+=check.Value+",";
287
}
288
}
289
if(sID.Length>0)
290

{
291
//MX.Sys.DAL.Loglog=newMX.Sys.DAL.Log(this.UserInfo.UserName,"删除数据表"+tableName+",sID="+sID);
292
myBiz.DeleteBase(tableName,sID);//截取结尾的逗号
293
Message.Text="删除成功!";
294
GetData();
295
BindGrid();
296
}
297
else
298

{
299
Message.Text="删除失败,请勾选要删除项!";
300
}
301
}
302
#endregion
303
304
导出打印#region导出打印
305
publicoverridevoidVerifyRenderingInServerForm(Controlcontrol)
306

{
307
//为转excel出问题而加上的过程(一定加!!)//ConfirmsthatanHtmlFormcontrolisrenderedfor
308
}
309
310
protectedvoidbtnToExcel_Click(objectsender,EventArgse)
311

{
312
System.IO.StringWriterstringWrite=newSystem.IO.StringWriter();
313
System.Web.UI.HtmlTextWriterhtmlWrite=newHtmlTextWriter(stringWrite);
314
315
GridView1.AllowPaging=false;//取消分页
316
BindGrid();
317
318
GridView1.RenderControl(htmlWrite);
319
320
GridView1.AllowPaging=true;//恢复分页
321
BindGrid();
322
323
//---专用导出页面
324
PrintInfoinfo=newPrintInfo();
325
info.Title="工程列表";
326
info.Orientation=true;
327
info.ShowTime=true;
328
info.Text=stringWrite.ToString();
329
ToPrint(info);
330
//---
331
}
332
/**////<summary>
333
///打印
334
///</summary>
335
privatevoidToPrint(PrintInfoinfo)
336

{
337
Session["ToExcel"]=info;//***用Session传递参数
338
339
stringstrURL="../Tools/WebPrint";
340
stringsFeatures="height=600,width=800,top=0,left=50,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=1";
341
stringjs="window.open('{0}','print','{1}');";
342
js=string.Format(js,strURL,sFeatures);
343
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"print",js,true);
344
}
345
#endregion
346
#endregion
347
348
公共函数#region公共函数
349
350
#endregion
351
352
}
353
PM_Base.aspx
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->1

<%
@PageLanguage="C#"AutoEventWireup="true"CodeFile="PM_Base.aspx.cs"Inherits="PM_Base"2
EnableEventValidation="false"%>3

4
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">5
<htmlxmlns="http://www.w3.org/1999/xhtml">6
<headrunat="server">7
<title>基本信息</title>8
</head>9
<body>10
<formid="form1"runat="server">11
<!--导航事件按钮-->12
<tablecellpadding="0"cellspacing="0"width="100%">13
<tr>14
<tdalign="right"style="height:25px;"nowrap="nowrap">15
<inputtype="hidden"id="HiddenID"name="HiddenID"runat="server"/>16
<inputid="btnView"runat="server"type="button"value="查看"style="width:50px;height:20px;"17
class="ClassBtn"onclick="returnView();"/>18
<asp:ButtonID="btnSearch"runat="server"Text="查询"Enabled="false"Width="50px"19
Height="20px"OnClick="btnSearch_Click"/>20
<inputid="btnAdd"runat="server"type="button"value="添加"style="width:50px;height:20px;"21
class="ClassBtn"onclick="returnAdd();"/>22
<inputid="btnEdit"runat="server"type="button"value="修改"style="width:50px;height:20px;"23
class="ClassBtn"onclick="returnEdit();"/>24
<asp:ButtonID="btnDelete"runat="server"Text="删除"Width="50px"Height="20px"OnClick="btnDelete_Click"25
OnClientClick="javascript:return(checkSelect()&&confirm('警告:删除将无法恢复!/n确认删除吗?'));"/>26
<asp:ButtonID="btnPrint"runat="server"Text="打印"Width="50px"Height="20px"OnClick="btnToExcel_Click"/>27
<asp:ButtonID="btnOut"runat="server"Text="导出"Width="50px"Height="20px"OnClick="btnToExcel_Click"/>28
</td>29
</tr>30
</table>31
<asp:GridViewID="GridView1"SkinID="gvClass2"runat="server"Width="100%"DataKeyNames="ID"32
AutoGenerateColumns="False"OnRowDataBound="GridView1_RowDataBound"OnDataBound="GridView1_DataBound"33
AllowSorting="True"OnSorting="GridView1_Sorting"AllowPaging="True"PageSize="10"34
OnPageIndexChanging="GridView1_PageIndexChanging">35
<PagerTemplate>36
<tablewidth="100%">37
<tr>38
<tdwidth="50%"align="left">39
<asp:LabelID="MessageLabel"ForeColor="Blue"Text="页码:"runat="server"/>40
<asp:DropDownListID="PageDropDownList"AutoPostBack="true"OnSelectedIndexChanged="PageDropDownList_SelectedIndexChanged"41
runat="server"/>42
<asp:LinkButtonCommandName="Page"CommandArgument="First"ID="linkBtnFirst"runat="server">首页</asp:LinkButton>43
<asp:LinkButtonCommandName="Page"CommandArgument="Prev"ID="linkBtnPrev"runat="server">上一页</asp:LinkButton>44
<asp:LinkButtonCommandName="Page"CommandArgument="Next"ID="linkBtnNext"runat="server">下一页</asp:LinkButton>45
<asp:LinkButtonCommandName="Page"CommandArgument="Last"ID="linkBtnLast"runat="server">尾页</asp:LinkButton>46
</td>47
<tdwidth="50%"align="right">48
<asp:LabelID="CurrentPageLabel"ForeColor="Blue"runat="server"/>49
</td>50
</tr>51
</table>52
</PagerTemplate>53
<Columns>54
<asp:TemplateField>55
<ItemTemplate>56
<inputid="check"runat="server"type="checkbox"value='<%#Eval("ID")%>'/>57
</ItemTemplate>58
<HeaderTemplate>59
<inputid="checkAll"title="全选"onclick="javascript:CheckAll(this);"runat="server"60
type="checkbox"/>61
</HeaderTemplate>62
<ItemStyleWidth="5%"/>63
</asp:TemplateField>64
<asp:BoundFieldDataField="Name"HeaderText="名称"SortExpression="Name">65
<ItemStyleWidth="30%"HorizontalAlign="Left"/>66
</asp:BoundField>67
<asp:BoundFieldDataField="Description"HeaderText="描述"SortExpression="Description">68
<ItemStyleHorizontalAlign="Left"/>69
</asp:BoundField>70
</Columns>71
</asp:GridView>72
<asp:LabelID="Message"runat="server"ForeColor="Red"></asp:Label>73
</form>74
</body>75
</html>76

77

<scriptlanguage="javascript">
78

function$(s)
{returndocument.getElementById(s);}79

functionisNull(_sVal)
{return(_sVal==""||_sVal==null||_sVal=="undefined");}80
//获取地址栏参数81
functionGetURL(name)82


{83
varURLParams=newArray();84
varaParams=document.location.search.substr(1).split('&');85
for(i=0;i<aParams.length;i++)86


{87
varaParam=aParams[i].split('=');88
URLParams[aParam[0]]=aParam[1];89
}90
//取得传过来的name参数91
returnURLParams[name];92
}93

94
vartgs;95
vartmp_background_val;96
functiontog(n,flags)97


{98

if(tgs)
{99
tgs.style.background=tmp_background_val;100
}101
n.style.background='#99ccff';102
tmp_background_val=flags;103
tgs=n;104
}105

106
functionGetRowIndex(obj)107


{108
$('<%=HiddenID.ClientID%>').value=obj;109
}110

111
varHiddenID;112
functioncheckSelect()113


{114
HiddenID=$('<%=HiddenID.ClientID%>').value;115
if(isNull(HiddenID))116


{117
alert("请选择一条记录!");118
}119
return!isNull(HiddenID);120
}121

122
functionCheckAll(spanChk)//CheckBox全选123


{124
varoItem=spanChk.children;125
vartheBox=(spanChk.type=="checkbox")?spanChk:spanChk.children.item[0];126
xState=theBox.checked;127
elm=theBox.form.elements;128
for(i=0;i<elm.length;i++)129
if(elm[i].type=="checkbox"&&elm[i].id!=theBox.id)130


{131
if(elm[i].checked!=xState)132
elm[i].click();133
}134
}135
//---------------------------------------------------------136
//查看137
functionView()138


{139
if(checkSelect())140


{141
varurl="PM_BaseEdit.aspx?ID="+HiddenID+"&tableName="+GetURL("tableName");142
winOpen(url);143
}144
}145
//添加146
functionAdd()147


{148
varurl="PM_BaseAdd.aspx?tableName="+GetURL("tableName");149
winOpen(url);150
}151
//编辑152
functionEdit()153


{154
if(checkSelect())155


{156
varurl="PM_BaseEdit.aspx?ID="+HiddenID+"&tableName="+GetURL("tableName");157
winOpen(url);158
}159
}160

161
//弹出打开新页面-------------------------------------------162
functionwinOpen(url)163


{164
varsFeatures='height=400,width=650,top=100,left=200,toolbar=0,menubar=0,scrollbars=auto,resizable=1,location=0,status=0';165
window.open(url,"PM_Base",sFeatures);166
}167

168
functionshowDialog(url)169


{170
varsFeatures="dialogHeight:400px;dialogWidth:500px;resizeable:no;help:no;status:no";171
varobj=showModalDialog(url,window,sFeatures);172
}173
</script>
PM_Base.aspx.cs
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->1
usingSystem;2
usingSystem.Data;3
usingSystem.Data.OleDb;4
usingSystem.Data.SqlClient;5
usingSystem.Configuration;6
usingSystem.Collections;7
usingSystem.Web;8
usingSystem.Web.Security;9
usingSystem.Web.UI;10
usingSystem.Web.UI.WebControls;11
usingSystem.Web.UI.WebControls.WebParts;12
usingSystem.Web.UI.HtmlControls;13
usingSystem.Web.Caching;14
usingFramework.Components;15

16
publicpartialclassPM_Base:Framework.UI.PageBase17


{18

Page事件#regionPage事件19
MX.PM.BLL.ProgressmyBiz=newMX.PM.BLL.Progress();20
stringtableName;21

22
protectedvoidPage_Load(objectsender,EventArgse)23


{24
tableName=base.GetUrl("tableName");25
//首先要进行权限判断26
if(!string.IsNullOrEmpty(tableName))27


{28
if(!Page.IsPostBack)29


{30
if(!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseBrowse))31


{32
Framework.AppExceptionexception=newFramework.AppException("对不起,您无权操作此页面!");33
GridView1.Visible=false;34
btnView.Disabled=true;35
}36
if(!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseSelect))37


{38
btnSearch.Enabled=false;39
btnPrint.Enabled=false;40
btnOut.Enabled=false;41
}42
if(!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseAdd))43


{44
btnAdd.Disabled=true;45
}46
if(!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseUpdate))47


{48
btnEdit.Disabled=true;49
}50
if(!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseDelete))51


{52
btnDelete.Enabled=false;53
}54
//在此处放置查询代码55
stringstrTableName="PM_Progress";56
this.btnSearch.OnClientClick="javascript:window.showModalDialog('../Tools/AjaxGeneralSearch/AjaxSeachPage.aspx?table="+strTableName+"','','dialogWidth:500px;dialogHeight:400px;status:0;help:0;center:1;scroll:0;resizable:1;');";57
}58

59
GetData();//重新获取操作后的数据源60
if(!Page.IsPostBack)61


{62
BindGrid();//绑定GridView,为删除服务63
}64
}65
}66

67
#endregion68

69

GridView#regionGridView70

属性#region属性71

/**////<summary>72
///获取或设置数据源73
///</summary>74
publicDataTableDataSource75


{76
get77


{78
if(ViewState["dataSource"]==null)79
returnnull;80
else81
return(DataTable)ViewState["dataSource"];82
}83
set84


{85
ViewState["dataSource"]=value;86
}87
}88

/**////<summary>89
///获取或设置排序方向90
///</summary>91
publicSortDirectionSort_Direction92


{93
get94


{95
if(ViewState["sortDirection"]==null)96
returnSortDirection.Ascending;97
else98
return(SortDirection)ViewState["sortDirection"];99
}100
set101


{102
ViewState["sortDirection"]=value;103
}104
}105

/**////<summary>106
///获取或设置当前分页码107
///</summary>108
publicintCurrentPage109


{110
get111


{112
if(ViewState["CurrentPage"]==null)113
return0;114
else115
return(int)ViewState["CurrentPage"];116
}117
set118


{119
ViewState["CurrentPage"]=value;120
}121
}122
#endregion123

124

数据绑定#region数据绑定125

/**////<summary>126
///获取数据127
///</summary>128
protectedvoidGetData()129


{130
this.DataSource=myBiz.GetBase(tableName).Tables[0];;131
}132

133

/**////<summary>134
///GridView数据绑定135
///</summary>136
privatevoidBindGrid()137


{138
Message.Text="";139
GridView1.DataSource=this.DataSource;140
GridView1.DataBind();141
GridView1.PageIndex=CurrentPage;//设置当前页142
}143

144
protectedvoidGridView1_RowDataBound(objectsender,GridViewRowEventArgse)145


{146
if(e.Row.RowType==DataControlRowType.DataRow)147


{148
System.Web.UI.HtmlControls.HtmlInputCheckBoxcheck=(System.Web.UI.HtmlControls.HtmlInputCheckBox)e.Row.FindControl("check");149
stringid=check.Value;150

151
if(e.Row.RowIndex%2!=0)152
e.Row.Attributes.Add("onclick","GetRowIndex('"+id+"'),tog(this,'#EFF3FB')");153
else154
e.Row.Attributes.Add("onclick","GetRowIndex('"+id+"'),tog(this,'#ffffff')");155
}156
}157
#endregion158

159

分页绑定#region分页绑定160
protectedvoidPageDropDownList_SelectedIndexChanged(Objectsender,EventArgse)161


{162
GridViewRowpagerRow=GridView1.BottomPagerRow;163
DropDownListpageList=(DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");164
GridView1.PageIndex=pageList.SelectedIndex;165
this.CurrentPage=pageList.SelectedIndex;166
BindGrid();167
}168

169
protectedvoidGridView1_DataBound(Objectsender,EventArgse)170


{171
if(this.DataSource.Rows.Count>0)172


{173
GridViewRowpagerRow=GridView1.BottomPagerRow;174
LinkButtonlinkBtnFirst=(LinkButton)pagerRow.Cells[0].FindControl("linkBtnFirst");175
LinkButtonlinkBtnPrev=(LinkButton)pagerRow.Cells[0].FindControl("linkBtnPrev");176
LinkButtonlinkBtnNext=(LinkButton)pagerRow.Cells[0].FindControl("linkBtnNext");177
LinkButtonlinkBtnLast=(LinkButton)pagerRow.Cells[0].FindControl("linkBtnLast");178
if(GridView1.PageIndex==0)179


{180
linkBtnFirst.Enabled=false;181
linkBtnPrev.Enabled=false;182
}183
elseif(GridView1.PageIndex==GridView1.PageCount-1)184


{185
linkBtnLast.Enabled=false;186
linkBtnNext.Enabled=false;187
}188
elseif(GridView1.PageCount<=0)189


{190
linkBtnFirst.Enabled=false;191
linkBtnPrev.Enabled=false;192
linkBtnNext.Enabled=false;193
linkBtnLast.Enabled=false;194
}195
DropDownListpageList=(DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");196
LabelpageLabel=(Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");197
if(pageList!=null)198


{199
for(inti=0;i<GridView1.PageCount;i++)200


{201
intpageNumber=i+1;202
ListItemitem=newListItem(pageNumber.ToString()+"/"+GridView1.PageCount.ToString(),pageNumber.ToString());203
if(i==GridView1.PageIndex)204


{205
item.Selected=true;206
}207
pageList.Items.Add(item);208
}209
}210
if(pageLabel!=null)211


{212
intcurrentPage=GridView1.PageIndex+1;213
pageLabel.Text="当前页:"+currentPage.ToString()+214
"/"+GridView1.PageCount.ToString();215
}216
}217
}218
#endregion219

220

分页#region分页221
protectedvoidGridView1_PageIndexChanging(objectsender,GridViewPageEventArgse)222


{223
GridView1.PageIndex=e.NewPageIndex;224
this.CurrentPage=e.NewPageIndex;//保存当前页码225
BindGrid();226
}227
#endregion228

229

排序#region排序230

/**////<summary>231
///排序232
///</summary>233
protectedvoidGridView1_Sorting(objectsender,GridViewSortEventArgse)234


{235
SortGridView(refGridView1,this.DataSource,e);236
}237

/**////<summary>238
///实现排序239
///</summary>240
privatevoidSortGridView(refGridViewgv,objectdt,GridViewSortEventArgse)241


{242
stringsortDirection="";243
stringsortExpression=e.SortExpression;244
if(this.Sort_Direction==SortDirection.Ascending)245


{246
this.Sort_Direction=SortDirection.Descending;247
sortDirection="DESC";248
}249
else250


{251
this.Sort_Direction=SortDirection.Ascending;252
sortDirection="ASC";253
}254
DataViewSource=newDataView((DataTable)dt);255
Source.Sort=e.SortExpression+""+sortDirection;256
gv.DataSource=Source;257
gv.DataBind();258
}259

260
#endregion261
#endregion262

263

按钮事件#region按钮事件264

查询#region查询265
protectedvoidbtnSearch_Click(objectsender,EventArgse)266


{267
GetData();268
BindGrid();269
}270
#endregion271

272

删除#region删除273

/**////<summary>274
///删除,用循环得到用户选择的CheckBox275
///</summary>276
protectedvoidbtnDelete_Click(objectsender,EventArgse)277


{278
Message.Text="";279
stringsID="";280
for(inti=0;i<GridView1.Rows.Count;i++)281


{282
GridViewRowrow=GridView1.Rows[i];283
System.Web.UI.HtmlControls.HtmlInputCheckBoxcheck=(System.Web.UI.HtmlControls.HtmlInputCheckBox)row.FindControl("check");284
if(check.Checked)285


{286
sID+=check.Value+",";287
}288
}289
if(sID.Length>0)290


{291
//MX.Sys.DAL.Loglog=newMX.Sys.DAL.Log(this.UserInfo.UserName,"删除数据表"+tableName+",sID="+sID);292
myBiz.DeleteBase(tableName,sID);//截取结尾的逗号293
Message.Text="删除成功!";294
GetData();295
BindGrid();296
}297
else298


{299
Message.Text="删除失败,请勾选要删除项!";300
}301
}302
#endregion303

304

导出打印#region导出打印305
publicoverridevoidVerifyRenderingInServerForm(Controlcontrol)306


{307
//为转excel出问题而加上的过程(一定加!!)//ConfirmsthatanHtmlFormcontrolisrenderedfor308
}309

310
protectedvoidbtnToExcel_Click(objectsender,EventArgse)311


{312
System.IO.StringWriterstringWrite=newSystem.IO.StringWriter();313
System.Web.UI.HtmlTextWriterhtmlWrite=newHtmlTextWriter(stringWrite);314

315
GridView1.AllowPaging=false;//取消分页316
BindGrid();317

318
GridView1.RenderControl(htmlWrite);319

320
GridView1.AllowPaging=true;//恢复分页321
BindGrid();322

323
//---专用导出页面324
PrintInfoinfo=newPrintInfo();325
info.Title="工程列表";326
info.Orientation=true;327
info.ShowTime=true;328
info.Text=stringWrite.ToString();329
ToPrint(info);330
//---331
}332

/**////<summary>333
///打印334
///</summary>335
privatevoidToPrint(PrintInfoinfo)336


{337
Session["ToExcel"]=info;//***用Session传递参数338

339
stringstrURL="../Tools/WebPrint";340
stringsFeatures="height=600,width=800,top=0,left=50,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=1";341
stringjs="window.open('{0}','print','{1}');";342
js=string.Format(js,strURL,sFeatures);343
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"print",js,true);344
}345
#endregion346
#endregion347

348

公共函数#region公共函数349

350
#endregion351

352
}353
本文详细介绍了ASP.NET中GridView控件的使用方法,包括页面加载事件中的权限判断、数据绑定、分页处理、排序功能及按钮事件的实现。通过具体的代码示例,帮助读者更好地理解和应用GridView控件。
4万+

被折叠的 条评论
为什么被折叠?



