Thanks for the responses.
While it is true that declaring the object as a custom one initially
solves the sample problem, I still have the issue of beginning with an
intrinsic framework object, DataGridItem.
The suggestion posted by
"100" is what I tried with an explicit implementation of the
constructor in a derived object, CustomDGI.
The actual code below shows demonstrates that the derived object is
not quite fully populated (the FindControl method works on the
intrinsic object but not on the derived one). I'm not sure if this a
coding issue on my part or a bug with DataGridItem. I'd appreciate
any further insight you may have.
- Fred
********ASPX**************
<%@ Page language="c#" Codebehind="Upcasting.aspx.cs"
AutoEventWireup="false" Inherits="UCtest.Upcasting" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Upcasting</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<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="GridLayout">
<form id="Upcasting" method="post" runat="server">
<asp
ataGrid id="DataGrid1" runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="col1"
HeaderText="Column1"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="TemplateCol">
<ItemTemplate>
<asp:Label id="lblCol2" runat="server">Col2 Label</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp
ataGrid>
</form>
</body>
</HTML>
********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 UCtest
{
public class Upcasting : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void DataGrid1_ItemCreated(object sender,
DataGridItemEventArgs e)
{
Label oLabel;
if (e.Item.ItemIndex == 0)
{
//this works
DataGridItem oDataGridItem = e.Item;
oLabel = (Label) oDataGridItem.FindControl("lblCol2");
oLabel.Text = "base";
}
if (e.Item.ItemIndex == 1)
{
//how is this done?
CustomDGI oCustomDGI = new CustomDGI(e.Item.ItemIndex,
e.Item.DataSetIndex, e.Item.ItemType) ;
//CustomDGI oCustomDGI = e.Item as CustomDGI;
oLabel = oCustomDGI.FindLabel("lblCol2");
oLabel.Text = "derived";
}
}
private void Page_Load(object sender, System.EventArgs e)
{
DataTable oDataTable = new DataTable();
oDataTable.Columns.Add("col1");
oDataTable.Rows.Add(new string[1] {"Row1"});
oDataTable.Rows.Add(new string[1] {"Row2"});
DataGrid1.DataSource = oDataTable;
DataGrid1.DataBind ();
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.DataGrid1.ItemCreated += new
DataGridItemEventHandler(this.DataGrid1_ItemCreated);
this.Load += new System.EventHandler(this.Page_Load);
}
}
public class CustomDGI : DataGridItem
{
public CustomDGI(int itemIndex, int DataSetIndex, ListItemType
itemType) : base(itemIndex, DataSetIndex, itemType)
{
}
public Label FindLabel(string sID)
{
return (Label) base.FindControl("lblCol2");
}
}
}
*******************************************************************************