ASP。net 之view

时间:2022-01-30 16:08:19
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>网页查询</title>
<link href="http://cdn.bootcss.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
<script src="http://cdn.bootcss.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<!-- HTML5 Shim 和 Respond.js 用于让 IE8 支持 HTML5元素和媒体查询 -->
<!-- 注意: 如果通过 file:// 引入 Respond.js 文件,则该文件无法起效果 -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
<style>
* {
margin:0px;
padding:0px;
} .lab-a {
margin:20px auto; } </style>
</head>
<body>
<form id="form1" runat="server" class="form-horizontal"> <div class="form-group lab-a">
<asp:Label ID="Label1" runat="server" Text="标题:" style="margin-bottom:-30px;" class="col-sm-3 control-label "></asp:Label>
<div class="col-sm-10 col-sm-offset-3">
<asp:TextBox ID="TextBox1" runat="server" Height="30px" Width="271px" class="form-control"></asp:TextBox>
</div>
</div>
<div class="form-group ">
<div class="col-sm-10 col-sm-offset-3">
<label for="name">查询条件选择:</label>
<div class="lab-a ">
<label class="checkbox-inline">
<asp:CheckBox ID="CheckBox2" runat="server" /> 学生性别
</label> <label class="checkbox-inline">
<asp:CheckBox ID="CheckBox4" runat="server" /> 学生姓名
</label>
<label class="checkbox-inline">
<asp:CheckBox ID="CheckBox5" runat="server" />学生年龄
</label> </div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-10">
<asp:Button ID="Button1" runat="server" Text="查询" class="btn btn-primary" OnClick="Button1_Click1" />
<asp:Button ID="Button5" runat="server" Text="添加" class="btn btn-success" OnClick="Button5_Click" />
<asp:Button ID="Button2" runat="server" Text="修改" class="btn btn-warning" OnClick="Button2_Click" />
<asp:Button ID="Button3" runat="server" Text="删除" class="btn btn-danger" OnClick="Button3_Click" /> <asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager> </div> </div> <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="126px" Width="855px" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal" AllowPaging="True" CssClass="table-hover table" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging" OnDataBound="GridView1_DataBound" AllowSorting="True" OnSorting="GridView1_Sorting" OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="GridView1_RowDeleting" >
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="stu_id" HeaderText="学生编号" SortExpression="stu_id" />
<asp:BoundField DataField="stu_code" HeaderText="学生学号" />
<asp:BoundField DataField="stu_name" HeaderText="学生姓名" />
<asp:BoundField DataField="stu_sex" HeaderText="学生性别" />
<asp:BoundField DataField="age" HeaderText="学生年龄" />
<asp:BoundField DataField="stu_dept" HeaderText="学生系别" />
<asp:BoundField DataField="stu_admin" HeaderText="学生密码" />
<asp:BoundField DataField="stu_provinces" HeaderText="省" />
<asp:BoundField DataField="stu_city" HeaderText="市" />
<asp:BoundField DataField="stu_districts" HeaderText="县" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#333333" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<PagerTemplate>
<asp:Label ID="lblPage" runat="server" Text='<%# "第" + (((GridView)Container.NamingContainer).PageIndex + 1) + "页/共" + (((GridView)Container.NamingContainer).PageCount) + "页" %> '></asp:Label>
<asp:LinkButton ID="lbnFirst" runat="Server" Text="首页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>' CommandName="Page" CommandArgument="First" ></asp:LinkButton>
<asp:LinkButton ID="lbnPrev" runat="server" Text="上一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>' CommandName="Page" CommandArgument="Prev" class="previous" ></asp:LinkButton>
<asp:LinkButton ID="lbnNext" runat="Server" Text="下一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>' CommandName="Page" CommandArgument="Next" class="next"></asp:LinkButton>
<asp:LinkButton ID="lbnLast" runat="Server" Text="尾页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>' CommandName="Page" CommandArgument="Last" ></asp:LinkButton>
到第 <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" ForeColor="Red" Height="19px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" Width="70px"></asp:DropDownList> 页
</PagerTemplate>
<RowStyle BackColor="White" ForeColor="#333333" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#487575" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#275353" />
</asp:GridView>
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel> </form>
</body>
</html>

  

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("<script>alert('删除成功')</script>");
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("<script>alert('删除成功,当前删除值':" + id + ");</script>");
}
else { Response.Write("<script>alert('删除失败')</script>");
}
BindView();
}
}