473,738 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Control.DataBin d() 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.d atabind() 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.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace BindingTest
{
/// <summary>
/// Summary description for ColectionBindin gTest.
/// </summary>
public class ColectionBindin gTest : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Drop DownList colorList;
protected System.Web.UI.W ebControls.Butt on btnRemove;
ArrayList colors = null;

private void Page_Load(objec t sender, System.EventArg s e)
{
if (null == Cache["items"])
{
colors = new ArrayList();

object[] items = new object[] {
Color.AliceBlue ,
Color.Beige,
Color.Black,
Color.ForestGre en
};

colors.AddRange (items);
Cache["items"] = colors;
}
else
{
colors = (ArrayList)Cach e["items"];
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeCompo nent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.colorList. Load += new System.EventHan dler(this.color List_Load);
this.btnRemove. Click += new System.EventHan dler(this.btnRe move_Click);
this.Load += new System.EventHan dler(this.Page_ Load);

}
#endregion

private void colorList_Load( object sender, System.EventArg s e)
{
if (!IsPostBack)
{
colorList.DataS ource = colors;
colorList.DataM ember = "Color";
colorList.DataT extField = "Name";
colorList.DataB ind();
colorList.Items .Insert(0, new ListItem("-- Choose --", String.Empty));
}
}

private void btnRemove_Click (object sender, System.EventArg s e)
{
// Retrieve item from cache
Color theColor = (Color)colors[colorList.Selec tedIndex -1];
if (Color.Empty != theColor)
{
// Perform some database action
// ....
bool dbUpdateSuccess ful = true;

if (dbUpdateSucces sful)
{
colors.Remove(t heColor);
// 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.DataB ind();
}
}
}
}
}
</Sample Code-Behind >

<Sample Webform>
<%@ Page language="c#" Codebehind="Col ectionBindingTe st.aspx.cs"
AutoEventWireup ="false" Inherits="Bindi ngTest.Colectio nBindingTest" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Colectio nBindingTest</title>
<meta name="GENERATOR " Content="Micros oft Visual Studio .NET 7.1">
<meta name="CODE_LANG UAGE" Content="C#">
<meta name="vs_defaul tClientScript" content="JavaSc ript">
<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING= "FlowLayout ">
<form id="Form1" method="post" runat="server">
<asp:DropDownLi st id="colorList" runat="server"> </asp:DropDownLis t>
<asp:Button id="btnRemove" runat="server" Text="Remove"></asp:Button>
</form>
</body>
</HTML>
</Sample Webform>
Nov 18 '05 #1
0 1146

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

Similar topics

4
8798
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 the records and I want to create a new instance of the user control for each record and then change the properties of the controls within each user control. The problem is that after I create the new instance of the user control, I get a...
1
1528
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 composite server control, I'm trying to avoid having to build the control twice. Here's why this happens: The routine for building the control can be called from two places: (1) CreateChildControls, or (2) DataBind. If DataBind is called, it sets...
4
4931
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" runat="server"> <asp:FormView ID="FormView1" Runat="server"> <ItemTemplate> <asp:Button ID="Button1" runat="server" Text="Breyta" CommandName="Edit"/> ...
2
1214
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 System.EventArgs) Handles MyBase.Load SqlDataAdapter1.Fill(DataSet11) If Not IsPostBack Then 'This works 'TextBox1.DataBind()
1
1977
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 I'm keeping things simple to try and figure out what's going wrong). The master control supports databinding. Upon postback, the detail control is not retrieving its state from ViewState after it is created and added to the master control during...
4
4089
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 initially configured during DataBind(). Thanks to Steven Cheng for pointing out that you have to set the constituent control properties after you add them to the composite control collection for the restore to work! However, I now have a...
1
16261
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 Bind() can only be used in the context of a databound control" when I start my page. Unless I'm missing something obvious, this is *not* the same issue as the "2-way databinding cascading lists" issue in...
5
1869
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 e.g. consider the following code: <script runat="server"> Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) If Not (Page.IsPostBack) Then 'create an array of colors
6
1859
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 have any data. I've found I can call DataBind() on that control and then it works okay. However, this has me wondering where the control normally databinds, and if me doing it manually would actually introduce the overhead of having the control...
0
9334
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9259
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6750
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4569
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4824
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.