我有一个带有 3 个模板字段的 Gridview:1 个带有 DropDownList,另外 2 个带有 Texboxes。
我设法获得了 GridView 中的第一个空行。现在,我需要在 DropDownList 的 SelectedIndexChanged 事件上添加另一行。
我怎样才能做到这一点?
谢谢
默认.aspx
<%@ Page Language="C#" AutoEventWireup="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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager runat="server">
</asp:ScriptManager>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="317px">
<Columns>
<asp:TemplateField HeaderText="Text 1">
<ItemTemplate>
<asp:TextBox runat="server" Visible='<%# Eval("isLast") %>' ID="Text1"></asp:TextBox>
<asp:Label runat="server" Visible='<%# !(bool)Eval("isLast") %>' Text='<%# Eval("text1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Text 2">
<ItemTemplate>
<asp:TextBox runat="server" Visible='<%# Eval("isLast") %>' ID="Text2"></asp:TextBox>
<asp:Label runat="server" Visible='<%# !(bool)Eval("isLast") %>' Text='<%# Eval("text2") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID='DropDownList' Visible='<%# Eval("isLast") %>' runat="server"
onselectedindexchanged="DropDownList_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="Option 1" Value="1" />
<asp:ListItem Text="Option 2" Value="2" />
<asp:ListItem Text="Option 3" Value="3" />
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
默认.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable("table1");
dt.Columns.Add("ID", Type.GetType("System.Int32"));
dt.Columns.Add("text1");
dt.Columns.Add("text2");
dt.Columns.Add("isLast", Type.GetType("System.Boolean"));
for (int i = 0; i < 3; i++)
{
dt.Rows.Add(new object[] {
i,
i < 2 ? "text 1 row " + i : string.Empty,
i < 2 ? "text 2 row " + i : string.Empty,
i == 2 });
}
GridView1.DataSource = dt;
GridView1.DataBind();
Session["dataTable"] = dt; // change to sql data source
}
}
protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
DataTable dt = (DataTable)Session["dataTable"];
var maxId = dt.AsEnumerable().Max(row => Convert.ToInt32(row["ID"]));
var dataRow = dt.AsEnumerable().Where(row => (int)row["ID"] == maxId)
.Select(row => row)
.FirstOrDefault();
dataRow["text1"] = ((TextBox)GridView1.Rows[GridView1.Rows.Count - 1].FindControl("Text1")).Text;
dataRow["text2"] = ((TextBox)GridView1.Rows[GridView1.Rows.Count - 1].FindControl("Text2")).Text;
dataRow["isLast"] = false;
dt.Rows.Add(new object[] { maxId + 1, string.Empty, string.Empty, true });
GridView1.DataSource = dt;
GridView1.DataBind();
Session["dataTable"] = dt;
}
}