无法获取GridView中动态添加的控件
我的目的是动态的添加一个控件,修改某行后,从该列中取出动态添加的那个控件中的值。程序如下,但实际操作中,却找不到那个控件。
============================
HTML代码(Default.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:GridView ID= "GridView1 " runat= "server " AutoGenerateColumns= "False " DataKeyNames= "CategoryID "
DataSourceID= "SqlDataSource1 " OnRowDataBound= "GridView1_RowDataBound " OnRowUpdating= "GridView1_RowUpdating " AllowPaging= "True " AllowSorting= "True ">
<Columns>
<asp:BoundField DataField= "CategoryID " HeaderText= "CategoryID " SortExpression= "CategoryID " />
<asp:BoundField DataField= "Description " HeaderText= "Description " SortExpression= "Description " />
<asp:BoundField DataField= "CategoryName " HeaderText= "CategoryName " SortExpression= "CategoryName " />
<asp:TemplateField> </asp:TemplateField>
<asp:CommandField ShowEditButton= "True " />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID= "SqlDataSource1 " runat= "server " ConflictDetection= "CompareAllValues "
ConnectionString= "Data Source=.;Initial Catalog=Northwind;Integrated Security=True "
DeleteCommand= "DELETE FROM [Categories] WHERE [CategoryID] = @original_CategoryID "
InsertCommand= "INSERT INTO [Categories] ([Description], [CategoryName]) VALUES (@Description, @CategoryName) "
OldValuesParameterFormatString= "original_{0} " ProviderName= "System.Data.SqlClient "
SelectCommand= "SELECT CategoryID, Description, CategoryName FROM Categories "
UpdateCommand= "UPDATE [Categories] SET [Description] = @Description, [CategoryName] = @CategoryName WHERE [CategoryID] = @original_CategoryID ">
<DeleteParameters>
<asp:Parameter Name= "original_CategoryID " Type= "Int32 " />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name= "Description " Type= "String " />
<asp:Parameter Name= "CategoryName " Type= "String " />
<asp:Parameter Name= "original_CategoryID " Type= "Int32 " />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name= "Description " Type= "String " />
<asp:Parameter Name= "CategoryName " Type= "String " />
</InsertParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
============================
CS代码(Default.aspx.cs):
------------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Label lab = new Label();
lab.Text = "HelloWorld! ";
if (DataControlRowType.DataRow == e.Row.RowType)
{
e.Row.Cells[3].Controls.Add(lab); //向第1列动态添加Label
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Response.Write( "第4列的控件个数: " + GridView1.Rows[GridView1.EditIndex].Cells[3].Controls.Count.ToString());
foreach (Control control in GridView1.Rows[GridView1.EditIndex].Cells[3].Controls)
{
Response.Write(control.ToString());
}
}
}
===========================
[解决办法]
友情UP
[解决办法]
Page_Load中.加一句. if (this.IsPostBack) { this.GridView1.DataBind(); }
[解决办法]
对,每次pageload()事件后,又得重新创建个控件的