📄 adminstudentdetails.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection conn;
protected void Page_Load(object sender, EventArgs e)
{
string connStr = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
conn = new SqlConnection(connStr);
if (Session["userName"] == null)
{
Response.Redirect("Login.aspx");
}
else
{
if (!IsPostBack)
GridViewBind();
}
}
private void GridViewBind()
{
string SqlStr = "";
if (txtstuID.Text.Trim() == "")
{
SqlStr = "SELECT student.*,Depart.* FROM Student ,Depart where Student.stuDepart=Depart.departID order by Student.stuDepart";
}
else
{
SqlStr = "SELECT student.*,Depart.* FROM Student ,Depart where Student.stuDepart=Depart.departID and Student.stuID like '%"+txtstuID.Text.Trim()+"%' order by Student.stuDepart";
}
DataSet ds = new DataSet();
try
{
if (conn.State.ToString() == "Closed")
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
finally
{
if (conn.State.ToString() == "Open")
conn.Close();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (((DropDownList)e.Row.FindControl("ddlDepart")) != null)
{
DropDownList ddldepart = (DropDownList)e.Row.FindControl("ddlDepart");
string SqlStr = "SELECT * from Depart";
DataSet ds = new DataSet();
if (conn.State.ToString() == "Closed")
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds);
if (conn.State.ToString() == "Open")
conn.Close();
ddldepart.DataSource = ds.Tables[0].DefaultView;
ddldepart.DataTextField = "departName";
ddldepart.DataValueField = "departID";
ddldepart.DataBind();
ddldepart.SelectedValue = ((HiddenField)e.Row.FindControl("hdfDepart")).Value;
ddldepart.SelectedItem.Text = ((HiddenField)e.Row.FindControl("hdfDepart")).Value;
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridViewBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridViewBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string stuid = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
string stuName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName")).Text;
int stuDepart = int.Parse(((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlDepart")).SelectedValue);
int stuGrade = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtGrade")).Text);
string stuClass = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtClass")).Text;
string SqlStr = "update Student set stuName='" + stuName + "',stuDepart='" + stuDepart + "',stuGrade='" + stuGrade + "',stuClass='" + stuClass + "' where stuID='" + stuid+"'";
try
{
if (conn.State.ToString() == "Closed")
conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
int i = comm.ExecuteNonQuery();
comm.Dispose();
if (conn.State.ToString() == "Open")
conn.Close();
GridView1.EditIndex = -1;
GridViewBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
string SqlStr1 = "delete from Student where stuID='" + id+"'";
string SqlStr = "delete from Elect where stuID='" + id + "'";
try
{
if (conn.State.ToString() == "Closed")
conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
SqlCommand comm1 = new SqlCommand(SqlStr1 ,conn );
comm.ExecuteNonQuery();
comm.Dispose();
comm1.ExecuteNonQuery();
comm1.Dispose();
if (conn.State.ToString() == "Open")
conn.Close();
GridView1.EditIndex = -1;
GridViewBind();
Response.Redirect("adminStudentDetails.aspx");
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("elect.aspx?="+txtstuID );
}
protected void Button1_Click1(object sender, EventArgs e)
{
GridViewBind();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -