B
Bryce Budd
Hi,
I have a webform which has several server controls attached. The entire
form is data bound to custom objects. The collection objects are cached
using the Cache object. In my use case, I have to update my database,
synchronize my cached collection and refresh my UI. After my database
update I call dropdownlist1.databind() and my items are exactly the same
when they should be less 1 item. If I understand the "control execution
lifecycle" document correctly, server-side changes that happen in event
handlers should cause the dropdownlist to update because they happen before
the pre-render event. Perhaps I've misunderstood the document though. Any
thoughts or advice would be greatly appreciated.
Thanks,
Bryce
<Sample 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 BindingTest
{
/// <summary>
/// Summary description for ColectionBindingTest.
/// </summary>
public class ColectionBindingTest : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList colorList;
protected System.Web.UI.WebControls.Button btnRemove;
ArrayList colors = null;
private void Page_Load(object sender, System.EventArgs e)
{
if (null == Cache["items"])
{
colors = new ArrayList();
object[] items = new object[] {
Color.AliceBlue,
Color.Beige,
Color.Black,
Color.ForestGreen
};
colors.AddRange(items);
Cache["items"] = colors;
}
else
{
colors = (ArrayList)Cache["items"];
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// 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.colorList.Load += new System.EventHandler(this.colorList_Load);
this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void colorList_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
colorList.DataSource = colors;
colorList.DataMember = "Color";
colorList.DataTextField = "Name";
colorList.DataBind();
colorList.Items.Insert(0, new ListItem("-- Choose --", String.Empty));
}
}
private void btnRemove_Click(object sender, System.EventArgs e)
{
// Retrieve item from cache
Color theColor = (Color)colors[colorList.SelectedIndex -1];
if (Color.Empty != theColor)
{
// Perform some database action
// ....
bool dbUpdateSuccessful = true;
if (dbUpdateSuccessful)
{
colors.Remove(theColor);
// the following call never updates the dropdown items
// when according to the execution lifecycle it should
// even if viewstate is enabled because the pre-render event
// for this control has not fired yet.
colorList.DataBind();
}
}
}
}
}
</Sample Code-Behind >
<Sample Webform>
<%@ Page language="c#" Codebehind="ColectionBindingTest.aspx.cs"
AutoEventWireup="false" Inherits="BindingTest.ColectionBindingTest" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>ColectionBindingTest</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="FlowLayout">
<form id="Form1" method="post" runat="server">
<aspropDownList id="colorList" runat="server"></aspropDownList>
<asp:Button id="btnRemove" runat="server" Text="Remove"></asp:Button>
</form>
</body>
</HTML>
</Sample Webform>
I have a webform which has several server controls attached. The entire
form is data bound to custom objects. The collection objects are cached
using the Cache object. In my use case, I have to update my database,
synchronize my cached collection and refresh my UI. After my database
update I call dropdownlist1.databind() and my items are exactly the same
when they should be less 1 item. If I understand the "control execution
lifecycle" document correctly, server-side changes that happen in event
handlers should cause the dropdownlist to update because they happen before
the pre-render event. Perhaps I've misunderstood the document though. Any
thoughts or advice would be greatly appreciated.
Thanks,
Bryce
<Sample 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 BindingTest
{
/// <summary>
/// Summary description for ColectionBindingTest.
/// </summary>
public class ColectionBindingTest : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList colorList;
protected System.Web.UI.WebControls.Button btnRemove;
ArrayList colors = null;
private void Page_Load(object sender, System.EventArgs e)
{
if (null == Cache["items"])
{
colors = new ArrayList();
object[] items = new object[] {
Color.AliceBlue,
Color.Beige,
Color.Black,
Color.ForestGreen
};
colors.AddRange(items);
Cache["items"] = colors;
}
else
{
colors = (ArrayList)Cache["items"];
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// 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.colorList.Load += new System.EventHandler(this.colorList_Load);
this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void colorList_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
colorList.DataSource = colors;
colorList.DataMember = "Color";
colorList.DataTextField = "Name";
colorList.DataBind();
colorList.Items.Insert(0, new ListItem("-- Choose --", String.Empty));
}
}
private void btnRemove_Click(object sender, System.EventArgs e)
{
// Retrieve item from cache
Color theColor = (Color)colors[colorList.SelectedIndex -1];
if (Color.Empty != theColor)
{
// Perform some database action
// ....
bool dbUpdateSuccessful = true;
if (dbUpdateSuccessful)
{
colors.Remove(theColor);
// the following call never updates the dropdown items
// when according to the execution lifecycle it should
// even if viewstate is enabled because the pre-render event
// for this control has not fired yet.
colorList.DataBind();
}
}
}
}
}
</Sample Code-Behind >
<Sample Webform>
<%@ Page language="c#" Codebehind="ColectionBindingTest.aspx.cs"
AutoEventWireup="false" Inherits="BindingTest.ColectionBindingTest" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>ColectionBindingTest</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="FlowLayout">
<form id="Form1" method="post" runat="server">
<aspropDownList id="colorList" runat="server"></aspropDownList>
<asp:Button id="btnRemove" runat="server" Text="Remove"></asp:Button>
</form>
</body>
</HTML>
</Sample Webform>