- 浏览: 904326 次
文章分类
最新评论
-
wen262856298:
$ = function (id)
function $() -
wchengyu:
http://huihai.iteye.com/blog/10 ...
spring mvc 3 最简单demo -
maling765775866:
yy
js生成级联下拉列表 -
maling765775866:
yy[color=orange][/color]
js生成级联下拉列表 -
hu_teye:
运行报错了
android抽屉实现
GridView 实现自定义分页、排序、查询、添加、编辑、多选删除 之代码规范
<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"
2EnableEventValidation="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;"
17class="ClassBtn"onclick="returnView();"/>
18<asp:ButtonID="btnSearch"runat="server"Text="查询"Enabled="false"Width="50px"
19Height="20px"OnClick="btnSearch_Click"/>
20<inputid="btnAdd"runat="server"type="button"value="添加"style="width:50px;height:20px;"
21class="ClassBtn"onclick="returnAdd();"/>
22<inputid="btnEdit"runat="server"type="button"value="修改"style="width:50px;height:20px;"
23class="ClassBtn"onclick="returnEdit();"/>
24<asp:ButtonID="btnDelete"runat="server"Text="删除"Width="50px"Height="20px"OnClick="btnDelete_Click"
25OnClientClick="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"
32AutoGenerateColumns="False"OnRowDataBound="GridView1_RowDataBound"OnDataBound="GridView1_DataBound"
33AllowSorting="True"OnSorting="GridView1_Sorting"AllowPaging="True"PageSize="10"
34OnPageIndexChanging="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"
41runat="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"
60type="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">
78function$(s){returndocument.getElementById(s);}
79functionisNull(_sVal){return(_sVal==""||_sVal==null||_sVal=="undefined");}
80//获取地址栏参数
81functionGetURL(name)
82{
83varURLParams=newArray();
84varaParams=document.location.search.substr(1).split('&');
85for(i=0;i<aParams.length;i++)
86{
87varaParam=aParams[i].split('=');
88URLParams[aParam[0]]=aParam[1];
89}
90//取得传过来的name参数
91returnURLParams[name];
92}
93
94vartgs;
95vartmp_background_val;
96functiontog(n,flags)
97{
98if(tgs){
99tgs.style.background=tmp_background_val;
100}
101n.style.background='#99ccff';
102tmp_background_val=flags;
103tgs=n;
104}
105
106functionGetRowIndex(obj)
107{
108$('<%=HiddenID.ClientID%>').value=obj;
109}
110
111varHiddenID;
112functioncheckSelect()
113{
114HiddenID=$('<%=HiddenID.ClientID%>').value;
115if(isNull(HiddenID))
116{
117alert("请选择一条记录!");
118}
119return!isNull(HiddenID);
120}
121
122functionCheckAll(spanChk)//CheckBox全选
123{
124varoItem=spanChk.children;
125vartheBox=(spanChk.type=="checkbox")?spanChk:spanChk.children.item[0];
126xState=theBox.checked;
127elm=theBox.form.elements;
128for(i=0;i<elm.length;i++)
129if(elm[i].type=="checkbox"&&elm[i].id!=theBox.id)
130{
131if(elm[i].checked!=xState)
132elm[i].click();
133}
134}
135//---------------------------------------------------------
136//查看
137functionView()
138{
139if(checkSelect())
140{
141varurl="PM_BaseEdit.aspx?ID="+HiddenID+"&tableName="+GetURL("tableName");
142winOpen(url);
143}
144}
145//添加
146functionAdd()
147{
148varurl="PM_BaseAdd.aspx?tableName="+GetURL("tableName");
149winOpen(url);
150}
151//编辑
152functionEdit()
153{
154if(checkSelect())
155{
156varurl="PM_BaseEdit.aspx?ID="+HiddenID+"&tableName="+GetURL("tableName");
157winOpen(url);
158}
159}
160
161//弹出打开新页面-------------------------------------------
162functionwinOpen(url)
163{
164varsFeatures='height=400,width=650,top=100,left=200,toolbar=0,menubar=0,scrollbars=auto,resizable=1,location=0,status=0';
165window.open(url,"PM_Base",sFeatures);
166}
167
168functionshowDialog(url)
169{
170varsFeatures="dialogHeight:400px;dialogWidth:500px;resizeable:no;help:no;status:no";
171varobj=showModalDialog(url,window,sFeatures);
172}
173</script>
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->1usingSystem;
2usingSystem.Data;
3usingSystem.Data.OleDb;
4usingSystem.Data.SqlClient;
5usingSystem.Configuration;
6usingSystem.Collections;
7usingSystem.Web;
8usingSystem.Web.Security;
9usingSystem.Web.UI;
10usingSystem.Web.UI.WebControls;
11usingSystem.Web.UI.WebControls.WebParts;
12usingSystem.Web.UI.HtmlControls;
13usingSystem.Web.Caching;
14usingFramework.Components;
15
16publicpartialclassPM_Base:Framework.UI.PageBase
17{
18Page事件#regionPage事件
19MX.PM.BLL.ProgressmyBiz=newMX.PM.BLL.Progress();
20stringtableName;
21
22protectedvoidPage_Load(objectsender,EventArgse)
23{
24tableName=base.GetUrl("tableName");
25//首先要进行权限判断
26if(!string.IsNullOrEmpty(tableName))
27{
28if(!Page.IsPostBack)
29{
30if(!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseBrowse))
31{
32Framework.AppExceptionexception=newFramework.AppException("对不起,您无权操作此页面!");
33GridView1.Visible=false;
34btnView.Disabled=true;
35}
36if(!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseSelect))
37{
38btnSearch.Enabled=false;
39btnPrint.Enabled=false;
40btnOut.Enabled=false;
41}
42if(!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseAdd))
43{
44btnAdd.Disabled=true;
45}
46if(!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseUpdate))
47{
48btnEdit.Disabled=true;
49}
50if(!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseDelete))
51{
52btnDelete.Enabled=false;
53}
54//在此处放置查询代码
55stringstrTableName="PM_Progress";
56this.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
59GetData();//重新获取操作后的数据源
60if(!Page.IsPostBack)
61{
62BindGrid();//绑定GridView,为删除服务
63}
64}
65}
66
67#endregion
68
69GridView#regionGridView
70属性#region属性
71/**////<summary>
72///获取或设置数据源
73///</summary>
74publicDataTableDataSource
75{
76get
77{
78if(ViewState["dataSource"]==null)
79returnnull;
80else
81return(DataTable)ViewState["dataSource"];
82}
83set
84{
85ViewState["dataSource"]=value;
86}
87}
88/**////<summary>
89///获取或设置排序方向
90///</summary>
91publicSortDirectionSort_Direction
92{
93get
94{
95if(ViewState["sortDirection"]==null)
96returnSortDirection.Ascending;
97else
98return(SortDirection)ViewState["sortDirection"];
99}
100set
101{
102ViewState["sortDirection"]=value;
103}
104}
105/**////<summary>
106///获取或设置当前分页码
107///</summary>
108publicintCurrentPage
109{
110get
111{
112if(ViewState["CurrentPage"]==null)
113return0;
114else
115return(int)ViewState["CurrentPage"];
116}
117set
118{
119ViewState["CurrentPage"]=value;
120}
121}
122#endregion
123
124数据绑定#region数据绑定
125/**////<summary>
126///获取数据
127///</summary>
128protectedvoidGetData()
129{
130this.DataSource=myBiz.GetBase(tableName).Tables[0];;
131}
132
133/**////<summary>
134///GridView数据绑定
135///</summary>
136privatevoidBindGrid()
137{
138Message.Text="";
139GridView1.DataSource=this.DataSource;
140GridView1.DataBind();
141GridView1.PageIndex=CurrentPage;//设置当前页
142}
143
144protectedvoidGridView1_RowDataBound(objectsender,GridViewRowEventArgse)
145{
146if(e.Row.RowType==DataControlRowType.DataRow)
147{
148System.Web.UI.HtmlControls.HtmlInputCheckBoxcheck=(System.Web.UI.HtmlControls.HtmlInputCheckBox)e.Row.FindControl("check");
149stringid=check.Value;
150
151if(e.Row.RowIndex%2!=0)
152e.Row.Attributes.Add("onclick","GetRowIndex('"+id+"'),tog(this,'#EFF3FB')");
153else
154e.Row.Attributes.Add("onclick","GetRowIndex('"+id+"'),tog(this,'#ffffff')");
155}
156}
157#endregion
158
159分页绑定#region分页绑定
160protectedvoidPageDropDownList_SelectedIndexChanged(Objectsender,EventArgse)
161{
162GridViewRowpagerRow=GridView1.BottomPagerRow;
163DropDownListpageList=(DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
164GridView1.PageIndex=pageList.SelectedIndex;
165this.CurrentPage=pageList.SelectedIndex;
166BindGrid();
167}
168
169protectedvoidGridView1_DataBound(Objectsender,EventArgse)
170{
171if(this.DataSource.Rows.Count>0)
172{
173GridViewRowpagerRow=GridView1.BottomPagerRow;
174LinkButtonlinkBtnFirst=(LinkButton)pagerRow.Cells[0].FindControl("linkBtnFirst");
175LinkButtonlinkBtnPrev=(LinkButton)pagerRow.Cells[0].FindControl("linkBtnPrev");
176LinkButtonlinkBtnNext=(LinkButton)pagerRow.Cells[0].FindControl("linkBtnNext");
177LinkButtonlinkBtnLast=(LinkButton)pagerRow.Cells[0].FindControl("linkBtnLast");
178if(GridView1.PageIndex==0)
179{
180linkBtnFirst.Enabled=false;
181linkBtnPrev.Enabled=false;
182}
183elseif(GridView1.PageIndex==GridView1.PageCount-1)
184{
185linkBtnLast.Enabled=false;
186linkBtnNext.Enabled=false;
187}
188elseif(GridView1.PageCount<=0)
189{
190linkBtnFirst.Enabled=false;
191linkBtnPrev.Enabled=false;
192linkBtnNext.Enabled=false;
193linkBtnLast.Enabled=false;
194}
195DropDownListpageList=(DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
196LabelpageLabel=(Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
197if(pageList!=null)
198{
199for(inti=0;i<GridView1.PageCount;i++)
200{
201intpageNumber=i+1;
202ListItemitem=newListItem(pageNumber.ToString()+"/"+GridView1.PageCount.ToString(),pageNumber.ToString());
203if(i==GridView1.PageIndex)
204{
205item.Selected=true;
206}
207pageList.Items.Add(item);
208}
209}
210if(pageLabel!=null)
211{
212intcurrentPage=GridView1.PageIndex+1;
213pageLabel.Text="当前页:"+currentPage.ToString()+
214"/"+GridView1.PageCount.ToString();
215}
216}
217}
218#endregion
219
220分页#region分页
221protectedvoidGridView1_PageIndexChanging(objectsender,GridViewPageEventArgse)
222{
223GridView1.PageIndex=e.NewPageIndex;
224this.CurrentPage=e.NewPageIndex;//保存当前页码
225BindGrid();
226}
227#endregion
228
229排序#region排序
230/**////<summary>
231///排序
232///</summary>
233protectedvoidGridView1_Sorting(objectsender,GridViewSortEventArgse)
234{
235SortGridView(refGridView1,this.DataSource,e);
236}
237/**////<summary>
238///实现排序
239///</summary>
240privatevoidSortGridView(refGridViewgv,objectdt,GridViewSortEventArgse)
241{
242stringsortDirection="";
243stringsortExpression=e.SortExpression;
244if(this.Sort_Direction==SortDirection.Ascending)
245{
246this.Sort_Direction=SortDirection.Descending;
247sortDirection="DESC";
248}
249else
250{
251this.Sort_Direction=SortDirection.Ascending;
252sortDirection="ASC";
253}
254DataViewSource=newDataView((DataTable)dt);
255Source.Sort=e.SortExpression+""+sortDirection;
256gv.DataSource=Source;
257gv.DataBind();
258}
259
260#endregion
261#endregion
262
263按钮事件#region按钮事件
264查询#region查询
265protectedvoidbtnSearch_Click(objectsender,EventArgse)
266{
267GetData();
268BindGrid();
269}
270#endregion
271
272删除#region删除
273/**////<summary>
274///删除,用循环得到用户选择的CheckBox
275///</summary>
276protectedvoidbtnDelete_Click(objectsender,EventArgse)
277{
278Message.Text="";
279stringsID="";
280for(inti=0;i<GridView1.Rows.Count;i++)
281{
282GridViewRowrow=GridView1.Rows[i];
283System.Web.UI.HtmlControls.HtmlInputCheckBoxcheck=(System.Web.UI.HtmlControls.HtmlInputCheckBox)row.FindControl("check");
284if(check.Checked)
285{
286sID+=check.Value+",";
287}
288}
289if(sID.Length>0)
290{
291//MX.Sys.DAL.Loglog=newMX.Sys.DAL.Log(this.UserInfo.UserName,"删除数据表"+tableName+",sID="+sID);
292myBiz.DeleteBase(tableName,sID);//截取结尾的逗号
293Message.Text="删除成功!";
294GetData();
295BindGrid();
296}
297else
298{
299Message.Text="删除失败,请勾选要删除项!";
300}
301}
302#endregion
303
304导出打印#region导出打印
305publicoverridevoidVerifyRenderingInServerForm(Controlcontrol)
306{
307//为转excel出问题而加上的过程(一定加!!)//ConfirmsthatanHtmlFormcontrolisrenderedfor
308}
309
310protectedvoidbtnToExcel_Click(objectsender,EventArgse)
311{
312System.IO.StringWriterstringWrite=newSystem.IO.StringWriter();
313System.Web.UI.HtmlTextWriterhtmlWrite=newHtmlTextWriter(stringWrite);
314
315GridView1.AllowPaging=false;//取消分页
316BindGrid();
317
318GridView1.RenderControl(htmlWrite);
319
320GridView1.AllowPaging=true;//恢复分页
321BindGrid();
322
323//---专用导出页面
324PrintInfoinfo=newPrintInfo();
325info.Title="工程列表";
326info.Orientation=true;
327info.ShowTime=true;
328info.Text=stringWrite.ToString();
329ToPrint(info);
330//---
331}
332/**////<summary>
333///打印
334///</summary>
335privatevoidToPrint(PrintInfoinfo)
336{
337Session["ToExcel"]=info;//***用Session传递参数
338
339stringstrURL="../Tools/WebPrint";
340stringsFeatures="height=600,width=800,top=0,left=50,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=1";
341stringjs="window.open('{0}','print','{1}');";
342js=string.Format(js,strURL,sFeatures);
343Page.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"
2EnableEventValidation="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;"
17class="ClassBtn"onclick="returnView();"/>
18<asp:ButtonID="btnSearch"runat="server"Text="查询"Enabled="false"Width="50px"
19Height="20px"OnClick="btnSearch_Click"/>
20<inputid="btnAdd"runat="server"type="button"value="添加"style="width:50px;height:20px;"
21class="ClassBtn"onclick="returnAdd();"/>
22<inputid="btnEdit"runat="server"type="button"value="修改"style="width:50px;height:20px;"
23class="ClassBtn"onclick="returnEdit();"/>
24<asp:ButtonID="btnDelete"runat="server"Text="删除"Width="50px"Height="20px"OnClick="btnDelete_Click"
25OnClientClick="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"
32AutoGenerateColumns="False"OnRowDataBound="GridView1_RowDataBound"OnDataBound="GridView1_DataBound"
33AllowSorting="True"OnSorting="GridView1_Sorting"AllowPaging="True"PageSize="10"
34OnPageIndexChanging="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"
41runat="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"
60type="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">
78function$(s){returndocument.getElementById(s);}
79functionisNull(_sVal){return(_sVal==""||_sVal==null||_sVal=="undefined");}
80//获取地址栏参数
81functionGetURL(name)
82{
83varURLParams=newArray();
84varaParams=document.location.search.substr(1).split('&');
85for(i=0;i<aParams.length;i++)
86{
87varaParam=aParams[i].split('=');
88URLParams[aParam[0]]=aParam[1];
89}
90//取得传过来的name参数
91returnURLParams[name];
92}
93
94vartgs;
95vartmp_background_val;
96functiontog(n,flags)
97{
98if(tgs){
99tgs.style.background=tmp_background_val;
100}
101n.style.background='#99ccff';
102tmp_background_val=flags;
103tgs=n;
104}
105
106functionGetRowIndex(obj)
107{
108$('<%=HiddenID.ClientID%>').value=obj;
109}
110
111varHiddenID;
112functioncheckSelect()
113{
114HiddenID=$('<%=HiddenID.ClientID%>').value;
115if(isNull(HiddenID))
116{
117alert("请选择一条记录!");
118}
119return!isNull(HiddenID);
120}
121
122functionCheckAll(spanChk)//CheckBox全选
123{
124varoItem=spanChk.children;
125vartheBox=(spanChk.type=="checkbox")?spanChk:spanChk.children.item[0];
126xState=theBox.checked;
127elm=theBox.form.elements;
128for(i=0;i<elm.length;i++)
129if(elm[i].type=="checkbox"&&elm[i].id!=theBox.id)
130{
131if(elm[i].checked!=xState)
132elm[i].click();
133}
134}
135//---------------------------------------------------------
136//查看
137functionView()
138{
139if(checkSelect())
140{
141varurl="PM_BaseEdit.aspx?ID="+HiddenID+"&tableName="+GetURL("tableName");
142winOpen(url);
143}
144}
145//添加
146functionAdd()
147{
148varurl="PM_BaseAdd.aspx?tableName="+GetURL("tableName");
149winOpen(url);
150}
151//编辑
152functionEdit()
153{
154if(checkSelect())
155{
156varurl="PM_BaseEdit.aspx?ID="+HiddenID+"&tableName="+GetURL("tableName");
157winOpen(url);
158}
159}
160
161//弹出打开新页面-------------------------------------------
162functionwinOpen(url)
163{
164varsFeatures='height=400,width=650,top=100,left=200,toolbar=0,menubar=0,scrollbars=auto,resizable=1,location=0,status=0';
165window.open(url,"PM_Base",sFeatures);
166}
167
168functionshowDialog(url)
169{
170varsFeatures="dialogHeight:400px;dialogWidth:500px;resizeable:no;help:no;status:no";
171varobj=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>-->1usingSystem;
2usingSystem.Data;
3usingSystem.Data.OleDb;
4usingSystem.Data.SqlClient;
5usingSystem.Configuration;
6usingSystem.Collections;
7usingSystem.Web;
8usingSystem.Web.Security;
9usingSystem.Web.UI;
10usingSystem.Web.UI.WebControls;
11usingSystem.Web.UI.WebControls.WebParts;
12usingSystem.Web.UI.HtmlControls;
13usingSystem.Web.Caching;
14usingFramework.Components;
15
16publicpartialclassPM_Base:Framework.UI.PageBase
17{
18Page事件#regionPage事件
19MX.PM.BLL.ProgressmyBiz=newMX.PM.BLL.Progress();
20stringtableName;
21
22protectedvoidPage_Load(objectsender,EventArgse)
23{
24tableName=base.GetUrl("tableName");
25//首先要进行权限判断
26if(!string.IsNullOrEmpty(tableName))
27{
28if(!Page.IsPostBack)
29{
30if(!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseBrowse))
31{
32Framework.AppExceptionexception=newFramework.AppException("对不起,您无权操作此页面!");
33GridView1.Visible=false;
34btnView.Disabled=true;
35}
36if(!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseSelect))
37{
38btnSearch.Enabled=false;
39btnPrint.Enabled=false;
40btnOut.Enabled=false;
41}
42if(!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseAdd))
43{
44btnAdd.Disabled=true;
45}
46if(!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseUpdate))
47{
48btnEdit.Disabled=true;
49}
50if(!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseDelete))
51{
52btnDelete.Enabled=false;
53}
54//在此处放置查询代码
55stringstrTableName="PM_Progress";
56this.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
59GetData();//重新获取操作后的数据源
60if(!Page.IsPostBack)
61{
62BindGrid();//绑定GridView,为删除服务
63}
64}
65}
66
67#endregion
68
69GridView#regionGridView
70属性#region属性
71/**////<summary>
72///获取或设置数据源
73///</summary>
74publicDataTableDataSource
75{
76get
77{
78if(ViewState["dataSource"]==null)
79returnnull;
80else
81return(DataTable)ViewState["dataSource"];
82}
83set
84{
85ViewState["dataSource"]=value;
86}
87}
88/**////<summary>
89///获取或设置排序方向
90///</summary>
91publicSortDirectionSort_Direction
92{
93get
94{
95if(ViewState["sortDirection"]==null)
96returnSortDirection.Ascending;
97else
98return(SortDirection)ViewState["sortDirection"];
99}
100set
101{
102ViewState["sortDirection"]=value;
103}
104}
105/**////<summary>
106///获取或设置当前分页码
107///</summary>
108publicintCurrentPage
109{
110get
111{
112if(ViewState["CurrentPage"]==null)
113return0;
114else
115return(int)ViewState["CurrentPage"];
116}
117set
118{
119ViewState["CurrentPage"]=value;
120}
121}
122#endregion
123
124数据绑定#region数据绑定
125/**////<summary>
126///获取数据
127///</summary>
128protectedvoidGetData()
129{
130this.DataSource=myBiz.GetBase(tableName).Tables[0];;
131}
132
133/**////<summary>
134///GridView数据绑定
135///</summary>
136privatevoidBindGrid()
137{
138Message.Text="";
139GridView1.DataSource=this.DataSource;
140GridView1.DataBind();
141GridView1.PageIndex=CurrentPage;//设置当前页
142}
143
144protectedvoidGridView1_RowDataBound(objectsender,GridViewRowEventArgse)
145{
146if(e.Row.RowType==DataControlRowType.DataRow)
147{
148System.Web.UI.HtmlControls.HtmlInputCheckBoxcheck=(System.Web.UI.HtmlControls.HtmlInputCheckBox)e.Row.FindControl("check");
149stringid=check.Value;
150
151if(e.Row.RowIndex%2!=0)
152e.Row.Attributes.Add("onclick","GetRowIndex('"+id+"'),tog(this,'#EFF3FB')");
153else
154e.Row.Attributes.Add("onclick","GetRowIndex('"+id+"'),tog(this,'#ffffff')");
155}
156}
157#endregion
158
159分页绑定#region分页绑定
160protectedvoidPageDropDownList_SelectedIndexChanged(Objectsender,EventArgse)
161{
162GridViewRowpagerRow=GridView1.BottomPagerRow;
163DropDownListpageList=(DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
164GridView1.PageIndex=pageList.SelectedIndex;
165this.CurrentPage=pageList.SelectedIndex;
166BindGrid();
167}
168
169protectedvoidGridView1_DataBound(Objectsender,EventArgse)
170{
171if(this.DataSource.Rows.Count>0)
172{
173GridViewRowpagerRow=GridView1.BottomPagerRow;
174LinkButtonlinkBtnFirst=(LinkButton)pagerRow.Cells[0].FindControl("linkBtnFirst");
175LinkButtonlinkBtnPrev=(LinkButton)pagerRow.Cells[0].FindControl("linkBtnPrev");
176LinkButtonlinkBtnNext=(LinkButton)pagerRow.Cells[0].FindControl("linkBtnNext");
177LinkButtonlinkBtnLast=(LinkButton)pagerRow.Cells[0].FindControl("linkBtnLast");
178if(GridView1.PageIndex==0)
179{
180linkBtnFirst.Enabled=false;
181linkBtnPrev.Enabled=false;
182}
183elseif(GridView1.PageIndex==GridView1.PageCount-1)
184{
185linkBtnLast.Enabled=false;
186linkBtnNext.Enabled=false;
187}
188elseif(GridView1.PageCount<=0)
189{
190linkBtnFirst.Enabled=false;
191linkBtnPrev.Enabled=false;
192linkBtnNext.Enabled=false;
193linkBtnLast.Enabled=false;
194}
195DropDownListpageList=(DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
196LabelpageLabel=(Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
197if(pageList!=null)
198{
199for(inti=0;i<GridView1.PageCount;i++)
200{
201intpageNumber=i+1;
202ListItemitem=newListItem(pageNumber.ToString()+"/"+GridView1.PageCount.ToString(),pageNumber.ToString());
203if(i==GridView1.PageIndex)
204{
205item.Selected=true;
206}
207pageList.Items.Add(item);
208}
209}
210if(pageLabel!=null)
211{
212intcurrentPage=GridView1.PageIndex+1;
213pageLabel.Text="当前页:"+currentPage.ToString()+
214"/"+GridView1.PageCount.ToString();
215}
216}
217}
218#endregion
219
220分页#region分页
221protectedvoidGridView1_PageIndexChanging(objectsender,GridViewPageEventArgse)
222{
223GridView1.PageIndex=e.NewPageIndex;
224this.CurrentPage=e.NewPageIndex;//保存当前页码
225BindGrid();
226}
227#endregion
228
229排序#region排序
230/**////<summary>
231///排序
232///</summary>
233protectedvoidGridView1_Sorting(objectsender,GridViewSortEventArgse)
234{
235SortGridView(refGridView1,this.DataSource,e);
236}
237/**////<summary>
238///实现排序
239///</summary>
240privatevoidSortGridView(refGridViewgv,objectdt,GridViewSortEventArgse)
241{
242stringsortDirection="";
243stringsortExpression=e.SortExpression;
244if(this.Sort_Direction==SortDirection.Ascending)
245{
246this.Sort_Direction=SortDirection.Descending;
247sortDirection="DESC";
248}
249else
250{
251this.Sort_Direction=SortDirection.Ascending;
252sortDirection="ASC";
253}
254DataViewSource=newDataView((DataTable)dt);
255Source.Sort=e.SortExpression+""+sortDirection;
256gv.DataSource=Source;
257gv.DataBind();
258}
259
260#endregion
261#endregion
262
263按钮事件#region按钮事件
264查询#region查询
265protectedvoidbtnSearch_Click(objectsender,EventArgse)
266{
267GetData();
268BindGrid();
269}
270#endregion
271
272删除#region删除
273/**////<summary>
274///删除,用循环得到用户选择的CheckBox
275///</summary>
276protectedvoidbtnDelete_Click(objectsender,EventArgse)
277{
278Message.Text="";
279stringsID="";
280for(inti=0;i<GridView1.Rows.Count;i++)
281{
282GridViewRowrow=GridView1.Rows[i];
283System.Web.UI.HtmlControls.HtmlInputCheckBoxcheck=(System.Web.UI.HtmlControls.HtmlInputCheckBox)row.FindControl("check");
284if(check.Checked)
285{
286sID+=check.Value+",";
287}
288}
289if(sID.Length>0)
290{
291//MX.Sys.DAL.Loglog=newMX.Sys.DAL.Log(this.UserInfo.UserName,"删除数据表"+tableName+",sID="+sID);
292myBiz.DeleteBase(tableName,sID);//截取结尾的逗号
293Message.Text="删除成功!";
294GetData();
295BindGrid();
296}
297else
298{
299Message.Text="删除失败,请勾选要删除项!";
300}
301}
302#endregion
303
304导出打印#region导出打印
305publicoverridevoidVerifyRenderingInServerForm(Controlcontrol)
306{
307//为转excel出问题而加上的过程(一定加!!)//ConfirmsthatanHtmlFormcontrolisrenderedfor
308}
309
310protectedvoidbtnToExcel_Click(objectsender,EventArgse)
311{
312System.IO.StringWriterstringWrite=newSystem.IO.StringWriter();
313System.Web.UI.HtmlTextWriterhtmlWrite=newHtmlTextWriter(stringWrite);
314
315GridView1.AllowPaging=false;//取消分页
316BindGrid();
317
318GridView1.RenderControl(htmlWrite);
319
320GridView1.AllowPaging=true;//恢复分页
321BindGrid();
322
323//---专用导出页面
324PrintInfoinfo=newPrintInfo();
325info.Title="工程列表";
326info.Orientation=true;
327info.ShowTime=true;
328info.Text=stringWrite.ToString();
329ToPrint(info);
330//---
331}
332/**////<summary>
333///打印
334///</summary>
335privatevoidToPrint(PrintInfoinfo)
336{
337Session["ToExcel"]=info;//***用Session传递参数
338
339stringstrURL="../Tools/WebPrint";
340stringsFeatures="height=600,width=800,top=0,left=50,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=1";
341stringjs="window.open('{0}','print','{1}');";
342js=string.Format(js,strURL,sFeatures);
343Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"print",js,true);
344}
345#endregion
346#endregion
347
348公共函数#region公共函数
349
350#endregion
351
352}
353
相关推荐
gridview实现自定义分页
asp.net中的GridView自定义分页及排序代码,对GridView控件进行扩展,实现效果不错的自定义分页及排序代码,供大家学习参考!
ASP.NET 控件GridView分页 ,最简单的GridView自定义分页
GridView无代码分页排序
GridView查询排序分页GridView查询排序分页GridView查询排序分页
GridView自定义分页及可拖动改变列宽代码 GridView自定义分页及可拖动改变列宽代码 GridView自定义分页及可拖动改变列宽代码 GridView自定义分页及可拖动改变列宽代码 GridView自定义分页及可拖动改变列宽代码 ...
自定义GridView分页模板.doc 自定义GridView分页模板.doc自定义GridView分页模板.doc
使用ObjectDataSource和Gridview自定义分页,采用Microsoft.Practices.EnterpriseLibrary.Data4.1操作数据库
实现了Gridview的自定义分页技术和可拖动列宽效果,而且已经全部封装好了。也可看源代码,非常实用。
数据量很大时,使用gridview内置的分页功能,页面加载会非常慢。此处自定义分页是在需要时才加载当页数据。 示例中包含完整的Sql数据库访问类(考虑到了存储过程的使用),也可以适应不同的数据库,简单的修改代码...
一个非常优秀的基于GridView实现的分页控件源程序代码,采用VS2005+C#+ASP.NET开发实现。
asp.net中的GridView自定义分页及可拖动改变列宽代码
android Gridview分页实现的一个小例子
ActivityGroup和GridView实现标签分页 ActivityGroup和GridView实现标签分页
GridView无代码分页排序,选中,编辑,取消,删除,正反双向排序,下拉菜单DropDownList结合
ActivityGroup + GridView 实现Tab分页标签
GridView实现自定义时间货币等字符串格式
gridview超简单实现分页及排序功能
实现Gridview的多种功能,功能基本是代码写的
GridView自定义分页并集成列可拖动改变表格的列宽功能控件 一个GridView自定义分页并集成列可拖动功能控件 GridViewPageable为空间源码,Demo为演示文件夹