GridView删除功能:
首先设置GridView控件的
添加一个ButtonField控件然后将其
CommandName 属性设置为del,然后打开GridView控件的属性,绑定其GridView1_RowCommand事件
然后在GridView1_RowCommand()事件中写如下代码:
int index = Convert.ToInt32(e.CommandArgument);//得到当前行号
string Leid = GridView2.Rows[index].Cells[0].Text; //得到班级的ID号text;
if (e.CommandName == "del")
{
using (SqlConnection sqlCnn = new SqlConnection(Student.cnnstring))
{
using (SqlCommand sqlCmm = sqlCnn.CreateCommand())
{
sqlCmm.CommandText = "delete from lessonlist where lessonid='" + Leid + "'";
sqlCnn.Open();
int i = sqlCmm.ExecuteNonQuery();
if (i > 0)
{
scan1();
ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('删除成功')</script>", false);
}
else
{
ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('删除失败')</script>", false);
}
}
}
}
DropDownlist绑定数据到GridView:
using (SqlConnection sqlCnn = new SqlConnection(Student.cnnstring))
{
using (SqlCommand sqlCmm = sqlCnn.CreateCommand())
{
sqlCmm.CommandText = "select Timename from Time";
sqlCnn.Open();
SqlDataReader reader = sqlCmm.ExecuteReader();
if (reader != null)
{
while (reader.Read())
{
this.DropDownList1.Items.Add(reader["Timename"].ToString());
}
}
else
{
ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('数据库中没有数据')</script>", false);
//弹出对话框显示:“数据库中没有数据'”
}
}
}
浏览数据:从数据库到GridView:
private void scan()
{
using (SqlConnection sqlCnn = new SqlConnection(Student.cnnstring))
{
using (SqlCommand sqlCmm = sqlCnn.CreateCommand())
{
sqlCmm.CommandText = "select * from classlist";
DataSet dt = new DataSet();
SqlDataAdapter adapt = new SqlDataAdapter(sqlCmm.CommandText, sqlCnn);
DataSet ds = new DataSet();
adapt.Fill(ds);
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
}
}
向数据库插入数据:检查两个数据数否与数据库内的数据相等,如果相等,则给出提示,如果数据库内没有相同的数据则将其插入数据库:
protected void btn_addLe_Click(object sender, EventArgs e)
{
if (this.txb_Lename.Text.Length>=10)
{
ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('课程名称长度应小于十位')</script>", false);
}
bool bol = false;
using (SqlConnection sqlCon=new SqlConnection(Student.cnnstring))
{
using (SqlCommand sqlCom=sqlCon.CreateCommand())
{
sqlCom.CommandText = "select lessonid,lessonname from lessonlist";
sqlCon.Open();
SqlDataReader reader = sqlCom.ExecuteReader();
if (reader!=null)
{
while (reader.Read())
{
if (reader["lessonid"].ToString()==this.txb_Leid.Text)
{
ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alter('课程编号已存在!')</script>", false);
bol = true;
}
if (reader["lessonname"].ToString()==this.txb_Lename.Text)
{
ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alter('课程名称已存在!')</script>", false);
bol = true;
}
}
if (bol)
{
}
else
{
add_lesson();
}
}
else
{
ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alter('数据库没有数据!')</script>", false);
}
}
}
add_lesson();
}
private void add_lesson()
{
using (SqlConnection sqlCnn = new SqlConnection(Student.cnnstring))
{
using (SqlCommand sqlCmm = sqlCnn.CreateCommand())
{
sqlCmm.CommandText = "insert into lessonlist values(@lessonid,@lessonname)";
SqlParameter lessonid = sqlCmm.Parameters.Add("@lessonid", SqlDbType.Int);
lessonid.Value = this.txb_Leid.Text;
SqlParameter lessonname = sqlCmm.Parameters.Add("@lessonname", SqlDbType.NChar, 10);
lessonname.Value = this.txb_Lename.Text;
sqlCnn.Open();
int i = sqlCmm.ExecuteNonQuery();
if (i > 0)
{
scan1();
ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('添加成功')</script>", false);
}
else
{
ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('添加失败')</script>", false);
}
}
}
}
页面间使用参数传值:
本页面:
Response.Redirect("Classedit.aspx?param1='"+claid+"'¶m2='"+claname+"'");
Response.Redirect("Classedit.aspx");
目标页面:
this.txb_edit_num.Text = Request["param1"].Replace("'", "");
this.teb_edit_name.Text = Request["param2"].Replace("'", "");
用户登录后才可以访问,否则跳转到登录界面Login.aspx:
在Web.config文件中这样写:
<authentication mode="Forms">
<forms defaultUrl="Login.aspx" loginUrl="Login.aspx" timeout="2880" protection="All"/>
</authentication>
<authorization>
<deny users="?"/>
<allow users="*"/>
</authorization>//defaultUrl="Login.aspx"为跳转到的页面