GRIDVIEW增加DROPDOWNLIST
HTML code
<%@ Page Language="C#" AutoEventWireup="true" Debug="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" datakeynames="ID" CommadName="Select" OnRowCreated="GridView1_RowCreated" DataKeyNames="ID" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" >
<asp:ListItem Text="选项1" Value="1001" Selected="False" ></asp:ListItem>
<asp:ListItem Text="选项2" Value="1002" Selected="False"></asp:ListItem>
<asp:ListItem Text="选项3" Value="1003" Selected="False"></asp:ListItem>
<asp:ListItem Text="选项4" Value="0" Selected="False"></asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ShowHeader="true" DataField="ID" HeaderText="ID" />
<asp:BoundField ShowHeader="true" DataField="name" HeaderText="姓名" />
</Columns>
</asp:GridView>
<span>评价:</span>
<asp:TextBox ID="SelectedRLText" runat="server" Text=""></asp:TextBox>
</div>
</form>
</body>
</html>
- C# code
-
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.FindControl("DropDownList1") != null) { DropDownList ddl = ( DropDownList)e.Row.FindControl(" DropDownList1"); ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged); } } } protected void ddl_SelectedIndexChanged(object sender, EventArgs e) { //此处写入获取GridView中的DataKeys.Value }