你的位置:首页 > ASP.net教程

[ASP.net教程]自己代码备份


 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.Data.SqlClient; 8 using System.Text; 9 using System.Data; 10 using System.IO; 11  12 public partial class ScoreManage : System.Web.UI.Page 13 { 14   protected static PagedDataSource pds = new PagedDataSource();//创建一个分页数据源的对象且一定要声明为静态 15   Access acc = new Access(); 16   public static class uid 17   { 18     public static string name = ""; 19     public static string Name 20     { 21       get { return name; } 22     } 23     public static string strsysdatetime = ""; 24     public static string Strsysdatetime 25     { 26       get { return strsysdatetime; } 27     } 28     public static int list2 = -1; 29     public static int List2 30     { 31       get { return list2; } 32     } 33     public static DataSet tempst = new DataSet(); 34     public static DataSet Tempst 35     { 36       get { return tempst; } 37     } 38   } 39   protected void Page_Load(object sender, EventArgs e) 40   { 41     if (!Page.IsPostBack) 42     { 43       if (Session["userid"] == null) 44       { 45         Response.Redirect("Default.aspx"); 46       } 47       else 48       { 49         bing(); 50  51       } 52     } 53   } 54   private void bing() 55   { 56     uid.name = Session["userid"].ToString(); 57     subjectlist.Items.Clear(); 58     SqlCommand cm1 = new SqlCommand("select REPLACE(t.positionname+a.subjectname,'老师','--') as sunname,a.ID as subid from P_Position t,S_SubjectRel a,UP_UserPosRel b where a.teacherid = t.id and b.pos_id = t.id and b.user_id = " + uid.Name + "", acc.conn); 59     acc.conn.Open(); 60     SqlDataReader dr1 = cm1.ExecuteReader(); 61     this.subjectlist.DataSource = dr1; 62     this.subjectlist.DataTextField = "sunname"; 63     this.subjectlist.DataValueField = "subid"; 64     this.subjectlist.DataBind(); 65     dr1.Close(); 66     acc.conn.Close(); 67     ListItem it = new ListItem("-----------请选择-----------", "0000"); // 68  69     DropDownList1.Items.Insert(0, it); 70     DropDownList1.Items.Clear(); 71     SqlCommand cm11 = new SqlCommand("select b.id as classid,b.classname as classname from UC_UserClassRel a ,C_Class b where a.class_id = b.id and a.user_id =" + uid.Name + "", acc.conn); 72     acc.conn.Open(); 73     SqlDataReader dr11 = cm11.ExecuteReader(); 74     this.DropDownList1.DataSource = dr11; 75     this.DropDownList1.DataTextField = "classname"; 76     this.DropDownList1.DataValueField = "classid"; 77     this.DropDownList1.DataBind(); 78     dr11.Close(); 79     acc.conn.Close(); 80     DropDownList1.Items.Insert(0, it); 81     subjectlist.Items.Insert(0, it); 82   } 83   private void BindDataList(int currentpage) 84   { 85     pds.AllowPaging = true;//允许分页 86     //pds.PageSize = 3;//每页显示3条数据 87     pds.CurrentPageIndex = currentpage;//当前页为传入的一个int型值 88     pds.DataSource = uid.Tempst.Tables[0].DefaultView;//把数据集中的数据放入分页数据源中 89     DataList1.DataSource = pds;//绑定Datalist 90     DataList1.DataBind(); 91   } 92  93   protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e) 94   { 95     switch (e.CommandName) 96     { 97       //以下5个为 捕获用户点击 上一页 下一页等时发生的事件 98       case "first"://第一页 99         pds.CurrentPageIndex = 0;100         BindDataList(pds.CurrentPageIndex);101         break;102       case "pre"://上一页103         pds.CurrentPageIndex = pds.CurrentPageIndex - 1;104         BindDataList(pds.CurrentPageIndex);105         break;106       case "next"://下一页107         pds.CurrentPageIndex = pds.CurrentPageIndex + 1;108         BindDataList(pds.CurrentPageIndex);109         break;110       case "last"://最后一页111         pds.CurrentPageIndex = pds.PageCount - 1;112         BindDataList(pds.CurrentPageIndex);113         break;114       case "search"://页面跳转页115         if (e.Item.ItemType == ListItemType.Footer)116         {117           int PageCount = int.Parse(pds.PageCount.ToString());118           TextBox txtPage = e.Item.FindControl("txtPage") as TextBox;119           int MyPageNum = 0;120           if (!txtPage.Text.Equals(""))121             MyPageNum = Convert.ToInt32(txtPage.Text.ToString());122           if (MyPageNum <= 0 || MyPageNum > PageCount)123           {124             Label3.Text = "请输入页数并确定没有超出总页数!";125             txtPage.Text = "";126           }127           else128           {129 130             BindDataList(MyPageNum - 1);131           }132         }133         break;134       case "size"://显示条数135         if (e.Item.ItemType == ListItemType.Footer)136         {137           int PageSize = int.Parse(pds.PageSize.ToString());138           TextBox txtSize = e.Item.FindControl("txtSize") as TextBox;139           int MySizeNum = 0;140           if (!txtSize.Text.Equals(""))141             MySizeNum = Convert.ToInt32(txtSize.Text.ToString());142           if (MySizeNum <= 0 || MySizeNum > 500)143           {144             Label3.Text = "请输入正确的显示条数最大值为500!";145             txtSize.Text = "";146           }147           else148           {149             pds.PageSize = MySizeNum;150             BindDataList(0);151           }152 153         }154         break;155     }156   }157   protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)158   {159     if (e.Item.ItemType == ListItemType.Footer)160     {161       //以下六个为得到脚模板中的控件,并创建变量.162       Label CurrentPage = e.Item.FindControl("labCurrentPage") as Label;163       Label PageCount = e.Item.FindControl("labPageCount") as Label;164       LinkButton FirstPage = e.Item.FindControl("lnkbtnFirst") as LinkButton;165       LinkButton PrePage = e.Item.FindControl("lnkbtnFront") as LinkButton;166       LinkButton NextPage = e.Item.FindControl("lnkbtnNext") as LinkButton;167       LinkButton LastPage = e.Item.FindControl("lnkbtnLast") as LinkButton;168       CurrentPage.Text = (pds.CurrentPageIndex + 1).ToString();//绑定显示当前页169       PageCount.Text = pds.PageCount.ToString();//绑定显示总页数170       if (pds.IsFirstPage)//如果是第一页,首页和上一页不能用171       {172         FirstPage.Enabled = false;173         PrePage.Enabled = false;174       }175       if (pds.IsLastPage)//如果是最后一页"下一页"和"尾页"按钮不能用176       {177         NextPage.Enabled = false;178         LastPage.Enabled = false;179       }180     }181   }182   public DataSet txtinsql(string str)183   {184     string insertCmd = string.Empty;185     acc.conn.Open();186     StreamReader sr = new StreamReader(new FileStream(str, FileMode.Open, FileAccess.Read), System.Text.Encoding.Default);187     insertCmd = "insert into [I_ImportRecords](studentcode,score,importsign) values (@studentcode,@score,@importsign)";188     char delimChar = ',';189     string[] split = null;190     string strTemp = sr.ReadLine();191     while (strTemp != null)192     {193       split = strTemp.Split(delimChar);194       SqlCommand cmd = new SqlCommand(insertCmd, acc.conn);195       cmd.Parameters.Add("@studentcode", SqlDbType.NChar).Value = split[0];196       cmd.Parameters.Add("@score", SqlDbType.Float).Value = split[1];197       cmd.Parameters.Add("@importsign", SqlDbType.NChar).Value = uid.Strsysdatetime;198       cmd.ExecuteNonQuery();199       strTemp = sr.ReadLine();200     }201     insertCmd = "select s.ID as studentid,s.Student_Code as studentcode,s.Student_Name as studentname,c.classname as classname,tt.score as score from [I_ImportRecords] tt, S_Student s,C_Class c,SC_StudentClassRel sc where s.ID = sc.student_id and sc.class_id = c.id and tt.studentcode = s.Student_Code and importsign = '" + uid.Strsysdatetime + "'";202     SqlDataAdapter sda = new SqlDataAdapter(insertCmd, acc.conn);203     DataSet ds = new DataSet();204     sda.Fill(ds);205     acc.conn.Close();206     sr.Close();207     sda.Dispose();208     return ds;209   }210   protected void Button1_Click(object sender, EventArgs e)211   {212     if (subjectlist.SelectedValue == "0000" || DropDownList1.SelectedValue == "0000" || FileUpload1.HasFile == false)213     {214       Label3.Text = "科目,班级列表或者文件还未选择.";215     }216     else217     {218       uid.strsysdatetime = DateTime.Now.ToFileTime().ToString() + uid.Name;219       FileUpload1.SaveAs(System.AppDomain.CurrentDomain.BaseDirectory + @"file\" + uid.Strsysdatetime + ".txt");220       uid.tempst = txtinsql(System.AppDomain.CurrentDomain.BaseDirectory + @"file\" + uid.Strsysdatetime + ".txt");221       pds.PageSize = 3;222       BindDataList(0);223       Button2.Enabled = true;224     }225   }226 }

ScoreManage.aspx
 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ScoreManage.aspx.cs" Inherits="ScoreManage" %> 2  3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4  5 <html ="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7   <title></title> 8   <link rel="stylesheet" type="text/css" href="StyleSheet.css" /> 9 </head> 10 <body> 11   <form id="form2" runat="server"> 12   <div class="ifarmdiv"> 13   <asp:Panel ID="Panel1" runat="server" GroupingText="成绩录入" ForeColor="#0076C8"  style="COLOR: #0076C8; BACKGROUND-COLOR: #F4FAFF;" Font-Size="12pt"> 14   <table width = "790px" border="0" cellpadding="3" style="background-color: #b9d8f3; text-align:center;"> 15   <tr style="text-align: center; COLOR: #0076C8; BACKGROUND-COLOR: #F4FAFF; font-weight: bold"><td style="width:395px"><asp:label ID="Label1" runat="server" text="科目:" Font-Size="12pt"></asp:label> 16     <asp:DropDownList ID="subjectlist" runat="server" ></asp:DropDownList></td>   17   <td style="width:395px"><asp:label ID="Label2" runat="server" text="班级:" Font-Size="12pt"></asp:label> 18     <asp:DropDownList ID="DropDownList1" runat="server"> 19       <asp:ListItem Value="3">33</asp:ListItem> 20       <asp:ListItem>12</asp:ListItem> 21       <asp:ListItem>33</asp:ListItem> 22       <asp:ListItem Value="33"></asp:ListItem> 23     </asp:DropDownList></td></tr>  24   <tr style="text-align: center; COLOR: #0076C8; BACKGROUND-COLOR: #F4FAFF; font-weight: bold"><td style="width:395px"><asp:FileUpload ID="FileUpload1" runat="server"  25       /><asp:Button ID="Button1" runat="server" Text="预览"  26        onclick="Button1_Click" /></td><td style="width:395px"><asp:Button ID="Button2"  27          runat="server" Text="确认导入" Enabled="False" /> 28       </td></tr> 29   </table> 30   <table border="0" cellpadding="0"  31       style="width:790px"> 32     <tr> 33      <td style="text-align:center"> 34     <asp:DataList ID="DataList1" runat="server" 35       Width="785px" onitemcommand="DataList1_ItemCommand"  36       onitemdatabound="DataList1_ItemDataBound" BackColor="LightGoldenrodYellow"  37          BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" Font-Size="12pt"> 38          <HeaderTemplate> 39          <table> 40         <tr>   41           <td style=" width:100px">学生ID</td> 42           <td style=" width:185px">学号</td> 43           <td style=" width:210px">班级</td> 44           <td style=" width:210px">姓名</td> 45           <td style=" width:80px">成绩</td> 46         </tr>  47         </table> 48     </HeaderTemplate> 49       <HeaderStyle BackColor="Tan" Font-Bold="True" /> 50     <ItemTemplate> 51      <table> 52        <tr onmouseover="backcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff'" onmouseout="this.style.backgroundColor=backcolor"> 53        <td style=" width:100px"><%# Eval("studentid")%></td> 54         <td style=" width:185px"><%# Eval("studentcode")%></td> 55         <td style=" width:210px"><%# Eval("classname")%></td> 56         <td style=" width:210px"><%# Eval("studentname")%></td> 57         <td style=" width:80px"><%# Eval("score")%></td> 58       </tr> 59             </table> 60     </ItemTemplate> 61     62       <AlternatingItemStyle BackColor="PaleGoldenrod" /> 63       <FooterStyle BackColor="Tan" /> 64     65     <FooterTemplate> 66     <div style="text-align:center"> 67           <table id="Page" cellpadding="0"  68             style="font-size: 12px; width:780px"> 69             <tr> 70               <td> 71         <asp:Label ID="labCurrentPage" runat="server"></asp:Label>/ 72         <asp:Label ID="labPageCount" runat="server"></asp:Label> 73         <asp:LinkButton ID="lnkbtnFirst" runat="server" CommandName="first" Font-Underline="False"  74                   ForeColor="Black">首页</asp:LinkButton> 75         <asp:LinkButton ID="lnkbtnFront" runat="server" CommandName="pre" Font-Underline="False"  76                   ForeColor="Black">上一页</asp:LinkButton>  77         <asp:LinkButton ID="lnkbtnNext" runat="server" CommandName="next" Font-Underline="False"  78                   ForeColor="Black">下一页</asp:LinkButton> 79         <asp:LinkButton ID="lnkbtnLast" runat="server" CommandName="last" Font-Underline="False"  80                   ForeColor="Black">尾页</asp:LinkButton> 81                 &nbsp;&nbsp; 跳转至:<asp:TextBox ID="txtPage" runat="server" Width="35px" Height="21px"></asp:TextBox> 82                 <asp:Button ID="Button1" runat="server" CommandName="search" Text="GO"  83                   Height="19px" /> 84                 &nbsp;&nbsp; 每页显示条数:<asp:TextBox ID="txtSize" runat="server" Width="35px" Height="21px"></asp:TextBox> 85                 <asp:Button ID="Button3" runat="server" CommandName="size" Text="GO"  86                   Height="19px" /> 87               </td> 88             </tr> 89           </table> 90         </div> 91     </FooterTemplate> 92  93       <SelectedItemStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" /> 94  95     </asp:DataList> 96           </td> 97     </tr> 98     <tr><td><asp:Label ID="Label3" runat="server" ForeColor="Red"></asp:Label> 99       </td></tr>100    </table>101   102    </asp:Panel>103   </div>104   </form>105 </body>106 </html>

ScoreManage.aspx