Experts challenge on dynamically created WebControls

J

Joel

I'm building an editable data grid that supports any WebControl and all
controls are always editable as opposed to the DataGrid model of having
everything in read mode until you select the "edit" command. I ran into a
strange problem when trying to tie events to a button in the grid. It's
difficult to explain so I've included working sample code. Basically if you
fire an event from a dynamically created control (in this case the click
event of the buttons in the grid) the state management seems to get screwed
up.

To see this behavior put a breakpoint in each click handler on the e=e;
line. Click Button1 and inspect the tbl.Rows.Count property. It goes up by
one on each click as it should.

Now click any of the buttons in the grid and inspect tbl.Rows.Count. It
always stays at 1. It acts as though the state management overwrites the
changes you make to the tbl object in the OnInit().

Any ideas?

ASPX:

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="GridTest.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<script language="javascript" id="clientEventHandlersJS">
<!--


//-->
</script>
</HEAD>
<body language="javascript">
<form id="Form1" method="post" runat="server">
<P>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button></P>
<P>
<TABLE id="tbl" cellSpacing="1" cellPadding="1" width="300" border="1"
runat="server">
<TR>
<TD>Header</TD>
<td>Button</td>
</TR>
</TABLE>
</P>
</form>
</body>
</HTML>


Code-Behind:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace GridTest
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.HtmlControls.HtmlTable tbl;

private void Page_Load(object sender, System.EventArgs e)
{
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
HtmlTable t;
Button btn;

if(IsPostBack)
{
t=(HtmlTable)Session["table"];
HtmlTableRow tr=new HtmlTableRow();
tr.ID=Guid.NewGuid().ToString();

HtmlTableCell tc=new HtmlTableCell();
tc.InnerText=DateTime.Now.ToShortDateString();
tc.ID=Guid.NewGuid().ToString();
tr.Cells.Add(tc);

btn=new Button();
btn.Text="Notes";
btn.ID=Guid.NewGuid().ToString();
btn.Click+=new EventHandler(btn_Click);

tc=new HtmlTableCell();
tc.ID=Guid.NewGuid().ToString();
tc.Controls.Add(btn);
tr.Cells.Add(tc);

t.Rows.Add(tr);

while(t.Rows.Count>1)
{
tbl.Rows.Add(t.Rows[1]);
}

}

Session["table"]=tbl;
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btn_Click(object sender, EventArgs e)
{
e=e;
}

private void Button1_Click(object sender, System.EventArgs e)
{
e=e;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top