<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" %>网页查询
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data;using MySql.Data.MySqlClient;using DBHelper;public partial class Index : System.Web.UI.Page{ Student stu_s = new Student(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ViewState["SortOrder"] = "stu_id"; ViewState["OrderDire"] = "ASC"; BindView(); } } public void BindView() { string names = ""; if (!string.IsNullOrEmpty(this.TextBox1.Text)) { names += " and stu_name like '" + this.TextBox1.Text + "%'"; } DataView view=stu_s.GetTableSet(names); string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"]; view.Sort = sort; GridView1.DataSource = view; GridView1.DataKeyNames = new string[] { "stu_id" }; GridView1.DataBind(); //GridView1.BottomPagerRow.Visible = true; //GridViewRow pagerow = GridView1.BottomPagerRow; } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; BindView(); } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { GridViewRow pagerRow = GridView1.BottomPagerRow; DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("DropDownList1"); GridView1.PageIndex = pageList.SelectedIndex; BindView(); } protected void GridView1_DataBound(object sender, EventArgs e) { //GridViewRow pagerRow = GridView1.BottomPagerRow; //DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("DropDownList1"); //if (pageList != null) //{ // for (int i = 0; i < GridView1.PageCount; i++) // { // int pageNumber = i + 1; // ListItem item = new ListItem(pageNumber.ToString()); // if (i == GridView1.PageIndex) // { // item.Selected = true; // } // pageList.Items.Add(item); // } //} } protected void Button1_Click(object sender, EventArgs e) { } protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { string sPage = e.SortExpression; if (ViewState["SortOrder"].ToString() == sPage) { if (ViewState["OrderDire"].ToString() == "Desc") ViewState["OrderDire"] = "ASC"; else ViewState["OrderDire"] = "Desc"; } else { ViewState["SortOrder"] = e.SortExpression; } BindView(); } protected void Button1_Click1(object sender, EventArgs e) { //if (this.TextBox1.Text == "") //{ // BindView(); //} //else //{ // string names = this.TextBox1.Text.Trim(); // DataView view = stu_s.GetTableViewString(names); // GridView1.DataSource = view; // GridView1.DataKeyNames = new string[] { "stu_id" }; // GridView1.DataBind(); //} BindView(); } protected void Button5_Click(object sender, EventArgs e) { Response.Redirect("AddStudent.aspx"); } protected void Button2_Click(object sender, EventArgs e) { string id=""; for (int i = 0; i < GridView1.Rows.Count - 1; i++) { CheckBox ckb = (CheckBox)GridView1.Rows[i].FindControl("Checkbox1"); if (ckb.Checked == true) { id = GridView1.DataKeys[i].Value.ToString(); } } Response.Redirect("AddStudent.aspx?id=" + id); } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string sqlstr = "delete from t_student where stu_id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'"; DBHelper.SqlHelper.ExecteNonQueryText(sqlstr); Response.Write(""); GridView1.EditIndex = -1; BindView(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { //如果是绑定数据行 if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) { ((LinkButton)e.Row.Cells[11].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:" + e.Row.Cells[3].Text + "吗?')"); } //首先判断是否是数据行 if (e.Row.RowType == DataControlRowType.DataRow) { //当鼠标停留时更改背景色 e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'"); //当鼠标移开时还原背景色 e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c"); } //如果是绑定数据行 if (e.Row.RowType == DataControlRowType.DataRow) { //鼠标经过时,行背景色变 e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'"); //鼠标移出时,行背景色变 e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'"); } } } protected void Button3_Click(object sender, EventArgs e) { string id = ""; for (int i = 0; i < GridView1.Rows.Count - 1; i++) { CheckBox ckb = (CheckBox)GridView1.Rows[i].FindControl("Checkbox1"); if (ckb.Checked == true) { id = GridView1.DataKeys[i].Value.ToString(); } } int x = stu_s.GetDelStudent(id); if (x > 0) { Response.Write(""); } else { Response.Write(""); } BindView(); }}