472,805 Members | 944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 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 1089

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: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.