473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Control.DataBind() question

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">
<asp:DropDownList id="colorList" runat="server"></asp:DropDownList>
<asp:Button id="btnRemove" runat="server" Text="Remove"></asp:Button>
</form>
</body>
</HTML>
</Sample Webform>
Nov 18 '05 #1
0 1121

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: patrick_a | last post by:
Hello, I am trying to insert multiple instances of a custom user control into a placeholder on an aspx page, based on the records retrieved from the database. I use the datareader to loop through...
1
by: dasein fiasco | last post by:
I'm sure this is a design question that is commonly encountered, but I can't find any resources which explain how to solve it. I would deeply appreciate any advice. Basically, when building a...
4
by: Daves | last post by:
hi, sorry for re-posting but it's a simple question and ought to have a very simple solution; I am trying to use Findcontrol to grab a Button control within a Formview control: <form ID="Form1"...
2
by: Unforgiven | last post by:
I have an ASP.NET page with 14 different text box on it that gets data from a SQL Server database. Please see the following code. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As...
1
by: Mark Olbert | last post by:
I have a "master" composite control which, in turn, holds an instance of a "detail" composite control (the "master" control will ultimately contain multiple instances of the "detail" control, but...
4
by: Mark Olbert | last post by:
This involves a family of related, databound ASPNET2 composite controls. I've managed to arrange things so that the composite controls restore themselves from ViewState on postback after they're...
1
by: Owen Blacker | last post by:
I've spent loads of time Googling to try to work this one out and I'm sure it's something obvious. I get an InvalidOperationException reading "Databinding methods such as Eval(), XPath(), and...
5
by: Arpan | last post by:
In order to populate any server control with data dynamically, is it ALWAYS NECESSARY to either BIND the DataSource to that server control or call the DataBind method of that server control? For...
6
by: Jonathan Wood | last post by:
I have a databound dropdownlist control. Based on some other criteria, I need to specify the selected item in my pages Load event. The problem is that, in my load event, the control does not yet...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.