?? editstudent.aspx.cs
字號(hào):
?using System;
using System.Data;
using System.Configuration;
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;
using SMS;
public partial class _Default : System.Web.UI.Page
{
OPDataBase OP = new OPDataBase();
SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Stuinform;Integrated Security=True");
static string Sql= "select StudentID,ClassID,Name,Age,Sex from student ";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
GridView1.Attributes.Add("style", "table-layout:fixed");
if ((int)Session["Power"] != 0)
{
GridView1.Columns[5].Visible = false;
GridView1.Columns[6].Visible = false;
}
}
public void bind()
{
SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Stuinform;Integrated Security=True");
SqlDataAdapter da = new SqlDataAdapter(Sql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[] { "StudentID" };
GridView1.DataBind();
conn.Close();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
GridView1.PageIndex = e.NewPageIndex;
bind();
}
catch { }
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string str = "delete from student where StudentID='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
SqlCommand cmd = new SqlCommand(str ,conn );
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
bind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string sqlstr = "update student set ClassID='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',Name='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',Age='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',Sex='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "' where StudentID='"
+GridView1 .DataKeys [e.RowIndex ].Value .ToString ()+"'";
SqlCommand cmd = new SqlCommand(sqlstr ,conn );
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
protected void chaxun_btn_Click(object sender, EventArgs e)
{
object x = Infor_box.Text.ToString();
string y = DropDownList1.SelectedValue.ToString();
Sql = "select *from student where " + y + " = '" + x + "'";
try
{
OP.BinData(Sql ,GridView1);
//OP.BinData(sql ,GridView1 );
//Response.Write("<script> alert('成功!')</script>");
}
catch(Exception ex)
{
string a=ex .Message .ToString ();
Label2.Text = a;
}
finally
{
conn.Close();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//鼠標(biāo)經(jīng)過(guò)時(shí),行背景色變
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
//鼠標(biāo)移出時(shí),行背景色變
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
//((ImageButton)(e.Row.Cells[9].Controls[0])).Attributes.Add("onclick", "return confirm('你確認(rèn)要?jiǎng)h除嗎?')");
//如果是綁定數(shù)據(jù)行 //清清月兒http://blog.csdn.net/21aspnet
e.Row.Cells[0].ControlStyle.Width = Unit.Pixel(50);
}
}
protected void ImageButton5_Click(object sender, ImageClickEventArgs e)
{
Sql = "select StudentID,ClassID,Name,Age,Sex from student ";
OP.BinData(Sql, GridView1);
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -