刚刚把那个程序编出来~~~呵呵,写一下 语言表达能力不强,看代码就ok了~~~
先来看看效果图:
更新前:
:
点击编辑后(专业那一列出现数据绑定的DropDownList):
点击更新后保存了结果(注意对比第一张政治经济学原理那一行的所属专业):
晕~~~~宿舍要断点了~~~明天把代码贴出来
前台代码:(主要是GridView控件的代码)
<
asp:GridView ID
=
"
GridView1
"
runat
=
"
server
"
AllowPaging
=
"
True
"
AutoGenerateColumns = " False " BackColor = " White " BorderColor = " #CCCCCC "
BorderStyle = " None " BorderWidth = " 1px " CellPadding = " 3 " Width = " 438px "
AllowSorting = " True " onpageindexchanging = " GridView1_PageIndexChanging "
onrowdeleting = " GridView1_RowDeleting " PageSize = " 6 "
onrowcancelingedit = " GridView1_RowCancelingEdit "
onrowediting = " GridView1_RowEditing " onrowupdating = " GridView1_RowUpdating " >
< PagerSettings FirstPageText = " 第一页 " LastPageText = " 最后一页 "
Mode = " NextPreviousFirstLast " NextPageText = " 下一页 " PreviousPageText = " 上一页 " />
< FooterStyle BackColor = " White " ForeColor = " #000066 " />
< RowStyle ForeColor = " #000066 " />
< Columns >
< asp:BoundField DataField = " Name " HeaderText = " 课程名称 " />
< asp:TemplateField HeaderText = " 所属专业 " >
< ItemTemplate >
< asp:Label ID = " Label1 " runat = " server " Text = ' <%# Bind("Expr1") %> ' ></ asp:Label >
</ ItemTemplate >
< EditItemTemplate >
< asp:DropDownList ID = " DropDownList2 " runat = " server "
DataSource = " <%# ddlBind() %> " DataTextField = " Name "
DataValueField = " ID " >
</ asp:DropDownList >
</ EditItemTemplate >
</ asp:TemplateField >
< asp:BoundField DataField = " JoinTime " HeaderText = " 加入时间 "
DataFormatString = " {0:yyyy-MM-dd} " />
< asp:CommandField HeaderText = " 编辑 " ShowEditButton = " True " />
< asp:TemplateField HeaderText = " 删除 " ShowHeader = " False " >
< ItemTemplate >
< asp:LinkButton ID = " LinkButton1 " runat = " server " CausesValidation = " False "
CommandName = " Delete " Text = " 删除 " OnClientClick = " return confirm('确认要删除吗?') " ></ asp:LinkButton >
</ ItemTemplate >
</ asp:TemplateField >
</ Columns >
< PagerStyle BackColor = " White " ForeColor = " #000066 " HorizontalAlign = " Left " />
< SelectedRowStyle BackColor = " #669999 " Font - Bold = " True " ForeColor = " White " />
< HeaderStyle BackColor = " #006699 " Font - Bold = " True " ForeColor = " White " />
</ asp:GridView >
后台代码:
AutoGenerateColumns = " False " BackColor = " White " BorderColor = " #CCCCCC "
BorderStyle = " None " BorderWidth = " 1px " CellPadding = " 3 " Width = " 438px "
AllowSorting = " True " onpageindexchanging = " GridView1_PageIndexChanging "
onrowdeleting = " GridView1_RowDeleting " PageSize = " 6 "
onrowcancelingedit = " GridView1_RowCancelingEdit "
onrowediting = " GridView1_RowEditing " onrowupdating = " GridView1_RowUpdating " >
< PagerSettings FirstPageText = " 第一页 " LastPageText = " 最后一页 "
Mode = " NextPreviousFirstLast " NextPageText = " 下一页 " PreviousPageText = " 上一页 " />
< FooterStyle BackColor = " White " ForeColor = " #000066 " />
< RowStyle ForeColor = " #000066 " />
< Columns >
< asp:BoundField DataField = " Name " HeaderText = " 课程名称 " />
< asp:TemplateField HeaderText = " 所属专业 " >
< ItemTemplate >
< asp:Label ID = " Label1 " runat = " server " Text = ' <%# Bind("Expr1") %> ' ></ asp:Label >
</ ItemTemplate >
< EditItemTemplate >
< asp:DropDownList ID = " DropDownList2 " runat = " server "
DataSource = " <%# ddlBind() %> " DataTextField = " Name "
DataValueField = " ID " >
</ asp:DropDownList >
</ EditItemTemplate >
</ asp:TemplateField >
< asp:BoundField DataField = " JoinTime " HeaderText = " 加入时间 "
DataFormatString = " {0:yyyy-MM-dd} " />
< asp:CommandField HeaderText = " 编辑 " ShowEditButton = " True " />
< asp:TemplateField HeaderText = " 删除 " ShowHeader = " False " >
< ItemTemplate >
< asp:LinkButton ID = " LinkButton1 " runat = " server " CausesValidation = " False "
CommandName = " Delete " Text = " 删除 " OnClientClick = " return confirm('确认要删除吗?') " ></ asp:LinkButton >
</ ItemTemplate >
</ asp:TemplateField >
</ Columns >
< PagerStyle BackColor = " White " ForeColor = " #000066 " HorizontalAlign = " Left " />
< SelectedRowStyle BackColor = " #669999 " Font - Bold = " True " ForeColor = " White " />
< HeaderStyle BackColor = " #006699 " Font - Bold = " True " ForeColor = " White " />
</ asp:GridView >
using
System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
public partial class Admin_kecheng_xinxi : System.Web.UI.Page
{
BassClass bc = new BassClass();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
private void Bind()
{
GridView1.DataSource = bc.GetDataSet("Select * From kecheng_zhuanye_view ORDER BY ID DESC", "kecheng_zhuanye_view");
GridView1.DataKeyNames = new String[] { "ID" };
GridView1.DataBind();
}
public DataSet ddlBind()
{
SqlConnection conn = bc.getCon();
conn.Open();
SqlDataAdapter adq = new SqlDataAdapter("select * from tb_Profession", conn);
DataSet dataset = new DataSet();
adq.Fill(dataset, "tb_Profession");
conn.Close();
return dataset;
}
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string Name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text.ToString().Trim();
DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList2");
int ofProfession =Convert.ToInt32( ddl.SelectedValue);
bool bl = bc.ExecSQL("update tb_Lesson set Name='" + Name + "' , ofProfession='"+ofProfession+"' where ID='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'");
if (bl)
{
Response.Write(bc.messageBox("更新成功!"));
}
else
{
Response.Write(bc.messageBox("更新失败!"));
}
GridView1.EditIndex = -1;
Bind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
Bind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
bool bl = bc.ExecSQL("delete from tb_lesson where ID='" + GridView1.DataKeys[e.RowIndex] + "'");
if (bl)
{
Response.Write(bc.messageBox("删除成功!"));
}
else
{
Response.Write(bc.messageBox("删除失败!"));
}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
Bind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
Bind();
}
}
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
public partial class Admin_kecheng_xinxi : System.Web.UI.Page
{
BassClass bc = new BassClass();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
private void Bind()
{
GridView1.DataSource = bc.GetDataSet("Select * From kecheng_zhuanye_view ORDER BY ID DESC", "kecheng_zhuanye_view");
GridView1.DataKeyNames = new String[] { "ID" };
GridView1.DataBind();
}
public DataSet ddlBind()
{
SqlConnection conn = bc.getCon();
conn.Open();
SqlDataAdapter adq = new SqlDataAdapter("select * from tb_Profession", conn);
DataSet dataset = new DataSet();
adq.Fill(dataset, "tb_Profession");
conn.Close();
return dataset;
}
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string Name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text.ToString().Trim();
DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList2");
int ofProfession =Convert.ToInt32( ddl.SelectedValue);
bool bl = bc.ExecSQL("update tb_Lesson set Name='" + Name + "' , ofProfession='"+ofProfession+"' where ID='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'");
if (bl)
{
Response.Write(bc.messageBox("更新成功!"));
}
else
{
Response.Write(bc.messageBox("更新失败!"));
}
GridView1.EditIndex = -1;
Bind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
Bind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
bool bl = bc.ExecSQL("delete from tb_lesson where ID='" + GridView1.DataKeys[e.RowIndex] + "'");
if (bl)
{
Response.Write(bc.messageBox("删除成功!"));
}
else
{
Response.Write(bc.messageBox("删除失败!"));
}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
Bind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
Bind();
}
}