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;
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 else 128 { 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 else 148 { 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 else 217 { 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 xmlns="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