Asp.net源码专业站
首页->新知实践->GridView实用示例源码附加导出Excel功能>>Default.aspx.cs>>源码在线查看
温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:GridView实用示例源码附加导出Excel功能
当前文件:文件类型 GridViewDemo/Default.aspx.cs[5K,2009-6-12 11:43:30]打开代码结构图
普通视图
		            
1using System; 2using System.Data; 3using System.Configuration; 4using System.Collections; 5using System.Web; 6using System.Web.Security; 7using System.Web.UI; 8using System.Web.UI.WebControls; 9using System.Web.UI.WebControls.WebParts; 10using System.Web.UI.HtmlControls; 11using System.Data.SqlClient; 12using System.IO; 13 14namespace GridViewDemo 15{ 16 //该源码下载自www.51aspx.com(51aspx.com) 17 //该源码为51aspx原创 18 public partial class _Default : System.Web.UI.Page 19 { 20 protected void Page_Load(object sender, EventArgs e) 21 { 22 Button2.Attributes["onclick"] = "return slcNo_click();"; 23 GridViewBind(""); 24 } 25 private void GridViewBind(string Sqlsort) 26 { 27 string connStr = ConfigurationManager.ConnectionStrings["Conn51aspx"].ConnectionString; 28 string SqlStr = "SELECT * FROM test01 where id<1000" + Sqlsort; 29 DataSet ds = new DataSet(); 30 31 try 32 { 33 SqlConnection conn = new SqlConnection(connStr); 34 if (conn.State.ToString() == "Closed") conn.Open(); 35 36 SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn); 37 da.Fill(ds, "test01"); 38 if (conn.State.ToString() == "Open") conn.Close(); 39 40 GridView1.DataSource = ds.Tables[0].DefaultView; 41 GridView1.DataBind(); 42 43 LblCurrentIndex.Text = "" + (GridView1.PageIndex + 1).ToString() + ""; 44 LblPageCount.Text = "" + GridView1.PageCount.ToString() + ""; 45 LblRecordCount.Text = "总共 " + ds.Tables[0].Rows.Count.ToString() + ""; 46 if (ds.Tables[0].Rows.Count == 0) 47 { 48 btnFirst.Visible = false; 49 btnPrev.Visible = false; 50 btnNext.Visible = false; 51 btnLast.Visible = false; 52 53 LblCurrentIndex.Visible = false; 54 LblPageCount.Visible = false; 55 LblRecordCount.Visible = false; 56 } 57 else if (GridView1.PageCount == 1) 58 { 59 btnFirst.Visible = false; 60 btnPrev.Visible = false; 61 btnNext.Visible = false; 62 btnLast.Visible = false; 63 } 64 65 // 计算生成分页页码,分别为:"首 页" "上一页" "下一页" "尾 页" 66 btnFirst.CommandName = "1"; 67 btnPrev.CommandName = (GridView1.PageIndex == 0 ? "1" : GridView1.PageIndex.ToString()); 68 69 btnNext.CommandName = (GridView1.PageCount == 1 ? GridView1.PageCount.ToString() : (GridView1.PageIndex + 2).ToString()); 70 btnLast.CommandName = GridView1.PageCount.ToString(); 71 // 72 } 73 catch (Exception ex) 74 { 75 Response.Write("数据库错误,错误原因:" + ex.Message); 76 Response.End(); 77 } 78 } 79 protected void PagerButtonClick(object sender, EventArgs e) 80 { 81 GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1; 82 GridViewBind(""); 83 } 84 85 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 86 { 87 e.Row.Attributes["onmouseover"] = "ItemOver(this)"; 88 89 if (e.Row.RowType == DataControlRowType.DataRow) 90 { 91 e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@;"); 92 } 93 } 94 95 protected void Button2_Click(object sender, EventArgs e) 96 { 97 string str = ""; 98 string[] ckb = null; 99 100 str = Request.Form.Get("checkboxname"); 101 ckb = str.Split(new char[] { ',' }); 102 103 Response.Write("直接在页面中得到的值为:" + str + "<br>"); 104 105 Response.Write("处理后存放在数组中,如下:<br>"); 106 for (int i = 0; i < ckb.Length; i++) 107 { 108 Response.Write("ckb[" + i + "]的值为:" + ckb[i] + "<br>"); 109 } 110 } 111 protected void Button1_Click(object sender, EventArgs e) 112 { 113 Response.Write(Request.Form.Get("RadioName")); 114 } 115 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) 116 { 117 string sql = ""; 118 119 if (ViewState["SortDirection"] == null || ViewState["SortDirection"].ToString().CompareTo("") == 0) 120 { 121 ViewState["SortDirection"] = " desc"; 122 } 123 else 124 ViewState["SortDirection"] = ""; 125 126 sql = " order by " + e.SortExpression + ViewState["SortDirection"]; 127 128 GridViewBind(sql); 129 } 130 131 //导出到Execl 132 protected void Button3_Click(object sender, EventArgs e) 133 { 134 Response.ClearContent(); 135 Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls"); 136 Response.ContentType = "application/excel"; 137 StringWriter sw = new StringWriter(); 138 HtmlTextWriter htw = new HtmlTextWriter(sw); 139 GridView1.RenderControl(htw); 140 Response.Write(sw.ToString()); 141 Response.End(); 142 143 144 } 145 146 public override void VerifyRenderingInServerForm( Control control ) 147 { } 148 149 150 } 151} 152
还没有找到您心仪的内容?请用.net源码大搜捕
代码片断 打包下载该项目完整源码:GridView实用示例源码附加导出Excel功能
51Aspx.com 版权所有 CopyRight © 2006-2010. 京ICP备06046876号 本站法律顾问:ITlaw-庄毅雄律师
返回顶部
客户服务:点击这里进行客户咨询 业务合作:点击这里洽谈业务合作 合作热线:010-68880146