GridView和下拉菜單DropDownList結(jié)合先來看看效果圖: 更新前: 點擊編輯后(專業(yè)那一列出現(xiàn)數(shù)據(jù)綁定的DropDownList): 點擊更新后保存了結(jié)果(注意對比第一張政治經(jīng)濟學(xué)原理那一行的所屬專業(yè)): 前臺代碼:(主要是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="所屬專業(yè)"> <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('確認(rèn)要刪除嗎?')"></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(); } } |
|