一.说明
部分代码的运用放在以往的教程中,本部分只讲解编辑功能下拉列表填充数据功能
二.前端
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4-(代码变更).aspx.cs" Inherits="WebApplication6.WebForm4__代码变更_" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" BorderWidth="1px" AutoGenerateColumns="False" DataKeyNames="id" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="id" HeaderText="编号" />
<asp:BoundField DataField="name" HeaderText="姓名" />
<asp:BoundField DataField="sex" HeaderText="性别" />
<%--<asp:BoundField DataField="chatname" HeaderText="职位" />--%>
<asp:TemplateField HeaderText="职位" SortExpression="categoryID">
<%--编辑列(编辑模板)--%>
<EditItemTemplate>
<%--设置下拉列表的数据源,为自定义方法所返回的对象
DataSource:数据关联
DataTextField:下拉列表的前端显示数据(ddlBind()方法的值)
DataValueField:下拉列表的后台关联数据(ddlBind()方法的值)
SelectedValue:当前下拉列表所选中单条数据(bind()方法的值,及源数据表所选的值)
--%>
<asp:DropDownList ID="dropList1" runat="server" DataSource='<%# ddlBind() %>'
DataTextField="names" DataValueField="ids" SelectedValue='<%# Bind("postid") %>'></asp:DropDownList>
</EditItemTemplate>
<%--常规显示列(显示模板)--%>
<ItemTemplate>
<asp:Label ID="label1" runat="server" Text='<%# Bind("chatname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" BorderWidth="1" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</div>
</form>
</body>
</html>
三.后端代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace WebApplication6
{
public partial class WebForm4__代码变更_ : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
bind();
}
}
/// <summary>
/// 数据填充
/// </summary>
private void bind()
{
string sql = @"SELECT t1.`id`,t1.`name`,t1.`postid`,t1.`sex`,t2.`postid` AS chatid,t2.`chatname`
FROM student t1,student_ing t2
WHERE t1.`postid`=t2.`postid`";
GridView1.DataSource=MyDBSql.excutSql(sql);
GridView1.DataBind();
}
/// <summary>
/// 下拉列表的数据填充
/// </summary>
/// <returns></returns>
public DataTable ddlBind() {
return MyDBSql.excutSql("SELECT t1.`postid` AS `ids`,t1.`chatname` AS `names` FROM student_ing t1");
}
/// <summary>
/// 行编辑
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
/// <summary>
/// 行更新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//int sexs;
string sql = "update student set name='{0}',postid = {1},sex={2} where id={3}";
string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
string sex = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text ;
//因为下拉列表是我们创建的控件,所以我们要用FindControl搜索其ID,才能或许对应的值
string postid = ((DropDownList)GridView1.Rows[e.RowIndex].Cells[4].FindControl("dropList1")).Text;
string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
sql = string.Format(sql,name,postid, sex,id);
MyDBSql.excuteNonQuery(sql);
GridView1.EditIndex = -1;
bind();
}
}
}
编辑时显示:
下拉列表中可以选取对应的职位,然后更新源数据表的值,从而使职位发生改变