N
nhsu
I have found that a databound control will not postback correctly if
you access its control collection in the Init stage. Obviously, the
control collection would be empty at this point as long as if have not
called DataBind, but simply accessing it will mess up the postback.
Here is the code, I've included the checkbox to force a postback.
Default2.aspx
<%@ Page Language="C#" AutoEventWireup="false"
CodeFile="Default2.aspx.cs" Inherits="Default2"
EnableEventValidation="false" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FormView ID="fv_Object" runat="server">
<InsertItemTemplate>
<asp:TextBox ID="tbx" runat="server" />
</InsertItemTemplate>
</asp:FormView>
</div>
<asp:CheckBox ID="cbx" AutoPostBack="true" runat="server" />
</form>
</body>
</html>
Default2.aspx.cs
using System;
using System.Web;
using System.Web.UI;
public partial class Default2 : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
// Comment this line out, and the FormView will post back
correctly
ControlCollection x = fv_Object.Controls;
base.OnInit(e);
}
}
you access its control collection in the Init stage. Obviously, the
control collection would be empty at this point as long as if have not
called DataBind, but simply accessing it will mess up the postback.
Here is the code, I've included the checkbox to force a postback.
Default2.aspx
<%@ Page Language="C#" AutoEventWireup="false"
CodeFile="Default2.aspx.cs" Inherits="Default2"
EnableEventValidation="false" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FormView ID="fv_Object" runat="server">
<InsertItemTemplate>
<asp:TextBox ID="tbx" runat="server" />
</InsertItemTemplate>
</asp:FormView>
</div>
<asp:CheckBox ID="cbx" AutoPostBack="true" runat="server" />
</form>
</body>
</html>
Default2.aspx.cs
using System;
using System.Web;
using System.Web.UI;
public partial class Default2 : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
// Comment this line out, and the FormView will post back
correctly
ControlCollection x = fv_Object.Controls;
base.OnInit(e);
}
}