473,666 Members | 2,181 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Databinding on Composite Control

I'm trying to implement databinding on a composite control and I'm getting an
error with the data when I change the DataSource. The first time I set the
DataSource and call DataBind() everything works fine, but when I set the
DataSource to a new value (DataTable) and then call DataBind again, I get
some strange results from the command events of some embedded ImageButton
controls... althought the data appears to be displayed correctly, the
CommandName and CommandArguemen t values are offset by 1 row. What's worse,
is the error goes away after I post-back a few times (which restores the data
from the ViewState). This is driving me nuts. It seems like the error is
somehow related to the order of operations surrounding the post-back,
EnsureChildCont rols (in OnLoad), and then calling DataBind (which creates the
controls again).

I'm attaching the full class, if anyone is willing to be of service of give
it a look. I would appreciate any help greatly.
//class code start here:

using System;
using System.Data;
using System.Collecti ons;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Componen tModel;
namespace System.Web.UI.W ebControls
{
/// <summary>
/// Summary description for ItemList.
/// </summary>
[DefaultProperty ("Text"),
ToolboxData("<{ 0}:ItemList runat=server></{0}:ItemList>")]
public class ItemList : System.Web.UI.W ebControls.WebC ontrol
{
private bool _isGrid = true;
private DataTable _dataTable = null;
#region public properties/methods/overrides
public ItemList() : base(){}
protected override void OnLoad(EventArg s e)
{
base.OnLoad (e);
if( this.Page.IsPos tBack )
{
EnsureChildCont rols();
}
}
public bool RenderGrid
{
get
{
return _isGrid;
}
set
{
_isGrid = value;
}
}
override public object DataSource
{
set
{
_dataTable = (DataTable)valu e;
ViewState["data"] = value;
}
}
public override void DataBind()
{
CreateControlHi erarchy();
}
protected override void CreateChildCont rols()
{
CreateControlHi erarchy();
}
private void CreateControlHi erarchy()
{
Controls.Clear( );
ClearChildViewS tate();
if( !IsTrackingView State ){ TrackViewState( ); }

if( _isGrid )
{
if( _dataTable == null )
{
CreateItemGrid( (DataTable)View State["data"] );
}
else
{
CreateItemGrid( _dataTable );
}
}
else
{
}

ChildControlsCr eated = true;
}
#endregion

#region CreateItemGrid
private void CreateItemGrid( DataTable dt)
{

Table t = new Table();
t.CssClass = "reportTabl e";

TableRow r = new TableRow();
TableCell cl = new TableCell();

r = new TableRow();
int c = 1;
for( ; c<dt.Columns.Co unt; c++ )
{
r.Cells.Add( AddGridHdrFtrCe ll( dt.Columns[c].ColumnName, true ) );
}
r.Cells.Add( AddGridHdrFtrCe ll( " ", true ) );
r.Cells.Add( AddGridHdrFtrCe ll( " ", true ) );
t.Rows.Add( r );

for( int row=0; row<dt.Rows.Cou nt; row++ )
{
r = new TableRow();
c = 1;
for( ; c<dt.Columns.Co unt; c++ )
{
r.Cells.Add( AddGridDataCell (
dt.Rows[row][dt.Columns[c].ColumnName].ToString(), row ) );
}
r.Cells.Add( AddGridLinkButt onCell( dt.Rows[row]["itmPk"].ToString(),
true, row) );
r.Cells.Add( AddGridLinkButt onCell( dt.Rows[row]["itmPk"].ToString(),
false, row) );
t.Rows.Add( r );
}

r = new TableRow();
cl = new TableCell();
cl.ColumnSpan = dt.Columns.Coun t + 2;
cl.Text = string.Format( "{0} items", dt.Rows.Count );
cl.CssClass = "reportFoot er";
r.Cells.Add( cl );
t.Rows.Add( r );

r = new TableRow();
cl = new TableCell();
cl.ColumnSpan = dt.Columns.Coun t + 2;
cl.Text = " ";
cl.CssClass = "reportItem ";
r.Cells.Add( cl );
t.Rows.Add( r );
this.Controls.A dd( t );
}
private TableCell AddGridHdrFtrCe ll(string text, bool hdr)
{
TableCell c = new TableCell();
c.Text = text;
c.CssClass = hdr ? "reportHead er" : "reportFoot er";
return c;
}
private TableCell AddGridDataCell (string text, int i)
{
TableCell c = new TableCell();
c.Text = text;
c.CssClass = (i+1)%2==0 ? "reportItem " : "reportAltItem" ;
return c;
}
private TableCell AddGridLinkButt onCell(string pk, bool view, int i)
{
ImageButton l = new ImageButton();
l.Visible = visible;
l.CommandArgume nt = pk;
//l.Command += new CommandEventHan dler(ImageButto n_Command);

if( view )
{
l.ImageUrl = "../images/rec_view.gif";
l.AlternateText = "View detail " + pk;
l.CommandName = "view";
}
else
{
l.ImageUrl = "../images/rec_edit.gif";
l.AlternateText = "Edit this item " + pk;
l.CommandName = "edit";
}

TableCell c = new TableCell();
c.Controls.Add( l );
c.CssClass = (i+1)%2==0 ? "reportItem " : "reportAltItem" ;
return c;
}
protected override bool OnBubbleEvent(o bject source, EventArgs ea)
{
bool handled = false;
if( ea is CommandEventArg s )
{
CommandEventArg s e = (CommandEventAr gs)ea;

switch( e.CommandName.T oLower() )
{
case "view":
{
OnRecordAction( new RecordActionEve ntArgs(
e.CommandArgume nt.ToString(), RecordMode.Sele ct ) );
handled = true;
break;
}
case "edit":
{
OnRecordAction( new RecordActionEve ntArgs(
e.CommandArgume nt.ToString(), RecordMode.Upda te ) );
handled = true;
break;
}
}

}

return handled;
}

private void ImageButton_Com mand(object sender, CommandEventArg s e)
{
}
#endregion

}
}
Dec 15 '05 #1
0 3219

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

Similar topics

0
1585
by: Satya Bojanapally | last post by:
Hi, I am unable to add a pager for this composite control. I had created a composite control in C#. The control is having 5 labels, one radio button and one DropDownList control. The composite control should display 2 columns and 2 rows per page. I am able to display 4 Composite Controls independently, when the user is changing the index of the DropDownList control, automatcially the output should get reflected on the composite control...
2
2040
by: John | last post by:
Hi, heres a brief description of what I'm trying to do. I have a sql table named colors that looks like this: color_id color_name 1 blue 2 red 3......... and so on
1
2064
by: Paul Kia | last post by:
I have an ATL composite control which I drop into a tab control dialog page of an MFC application. When I click on the composite control and then click anywhere outside the MFC application, the application immediately hangs! Spy++ indicates that the application is hanging on the WM_GETDLGCODE message. This problem does not happen if the ATL control is a standard (that is, non-composite) control, neither does it happen if the composite...
1
3142
by: sleigh | last post by:
Hello, I'm building a web application that will build a dynamic form based upon questions in a database. This form will have several different sections that consist of a panel containing one to many questions. To keep it simple, I'll describe the basics of what I'm trying to design. I've created a TextBox composite control that consists of a label for
1
1121
by: Charlie | last post by:
Hi: I'm creating some composite custom server controls that combine user interface element and a validation control. One such control combines a dropdownlist and required field validator. The problem is when I want to use it on a form, how do I get a reference to the dropdownlist contained within the control to call databind method? Also, if I call databind within server control, it works but viewstate doesn't work. Does any know of...
8
1824
by: Joey Chömpff | last post by:
L.S., Hello is there a way to implement 2-way databinding without using the datasources from dotnet 2.0. Why would you ask? Now that's simple. I've created an object model with BusinessObjects and I don't want to write an separate provider layer for the datasources. This is because I loose all the benefits of my objectmodel. Are there other developers who think this way?
3
3002
by: Beavis | last post by:
I hate to repost a message, but I am still at the same point where I was when I originally posted, and hopefully someone else will see this one... Ok, so I have gone off and documented the lifecycle of a page with a custom composite control on it. You can find that document here: http://www.ats-engineers.com/lifecycle.htm
3
1942
by: Eric | last post by:
I have created a fairly basic composite control consisting of a Label and a TextBox. In the overridden Render function, I'm creating a table with two rows and each row contains a cell (td). The Label and the TextBox are each rendered in one of the cells. Everything renders fine. The problem is that depending on the column the control represents I may want the textbox to be a different visible size during both design and runtime. I...
6
2621
by: shapper | last post by:
Hello, I am working in a class library with various custom controls. In which cases should a control inherit Control, WebControl and CompositeControl classes? And when should a custom control implement INamingContainer? In this moment I am working on a custom control that is composed by a
0
8454
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8362
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8878
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8560
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,...
1
6200
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
5671
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
4200
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...
1
2776
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
2
1778
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.