473,386 Members | 1,720 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,386 software developers and data experts.

Custom DataGridColumn DataGridItems not saving to ViewState. Not Visible on Postback

I created a custom DataGridColumn based on Marcie Robillard's MSDN
Article:

Creating Custom Columns for the ASP.NET Datagrid
http://msdn.microsoft.com/library/de...tomcolumns.asp

The problem I am having is that the data in the custom datagridcolumn
is not saved to viewstate and after postback, the column does not
contain data.

Any idea's?

I simplified the code so that the error is easy to reproduce.
Here is the code: (notice that the CustomImage that is standalone on
the page and the CustomImage that is embedded in a template column are
visible after postback.)

I can send a zip file of the project on request.

-=-=-=-=- CustomColumn.cs (begin) -=-=-=-=-
using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace PostbackError
{
public class CustomColumn : System.Web.UI.WebControls.DataGridColumn
{
public override void
InitializeCell(System.Web.UI.WebControls.TableCell cell, int
columnIndex, System.Web.UI.WebControls.ListItemType itemType)
{
//if the grid is using sorting, the the header will contain a
DataGridLinkButton
//the functionality is set by the base class initializecell.
base.InitializeCell (cell, columnIndex, itemType);
switch (itemType)
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
cell.DataBinding += new EventHandler(OnDataBinding);
break;
}}

private void OnDataBinding(object sender, EventArgs e)
{
TableCell cell = sender as TableCell;
DataGridItem dgi = cell.NamingContainer as DataGridItem;
DataRowView drv = dgi.DataItem as DataRowView;
//hardcoded for this example
string image = "arrow.gif";
if (image.Length == 0)
return;

CustomImage ri = new CustomImage();
cell.Controls.Add(ri);
TrackViewState();
string tooltip = "Help";
ri.ImageUrl = image;
ri.ToolTip = tooltip;
}}}
-=-=-=-=- CustomColumn.cs (end) -=-=-=-=-
-=-=-=-=- CustomImage.cs (begin) -=-=-=-=-
using System;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.Design;

namespace PostbackError
{
public class CustomImage : System.Web.UI.WebControls.Image
{
private string _imagePath = string.Empty;

public CustomImage():base()
{
_imagePath = string.Empty;
}

[
Browsable(true),
Category("Appearance"),
Description("Image to be displayed when the mouse is not over the
image")
]
public new string ImageUrl
{
get { return (string)ViewState["riImageUrl"]; }
set { ViewState["riImageUrl"] = value; }
}

[
Browsable(true),
Category("Appearance"),
Description("Image to be displayed when the mouse is over the
image")
]
public string MouseOverImageUrl
{
get { return (string)ViewState["MouseOverImageUrl"]; }
set { ViewState["MouseOverImageUrl"] = value; }
}

[
Browsable(true),
Category("Navigation"),
Description("The url to navigate to")
]
public string NavigateUrl
{
get { return (string)ViewState["NavigateUrl"]; }
set { ViewState["NavigateUrl"] = value; }
}

[
Browsable(true),
Category("Appearance"),
DefaultValue(true),
Description("Use a MouseOver Image derived from the Image name.
(i.e. image.gif -> image_ovr.gif)")
]
public bool UseMouseOverImage
{
get
{
string property = "UseMouseOverImage";
if (ViewState[property] == null)
{
object obj = _GetDefaultValue(property);
if (obj != null)
ViewState[property] = (bool)obj;
}
return (bool)ViewState[property];
}
set { ViewState["UseMouseOverImage"] = value; }
}
override protected void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
string s = base.ImageUrl;
base.ImageUrl = _imagePath + ImageUrl;
s = base.ImageUrl;
string mouseOver = _GetImageMouseOver();
if (mouseOver != null && UseMouseOverImage)
{
Attributes.Add("onmouseover", "this.src='" + _imagePath +
mouseOver + "'");
Attributes.Add("onmouseout", "this.src='" + _imagePath + ImageUrl
+ "'");
}
}

/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void Render(HtmlTextWriter output)
{
bool bWriteAnchor = NavigateUrl != null && NavigateUrl.Length > 0;
string s1 = base.ImageUrl;
base.ImageUrl = _imagePath + ImageUrl;

StringWriter sw = new StringWriter();
HtmlTextWriter buffer = new HtmlTextWriter(sw);

if (bWriteAnchor)
{
buffer.AddAttribute(HtmlTextWriterAttribute.Href, NavigateUrl);
buffer.RenderBeginTag(HtmlTextWriterTag.A);
}

base.Render(buffer);

if (bWriteAnchor)
buffer.RenderEndTag();

string s = sw.ToString();
output.Write(s);
}

private object _GetDefaultValue(string propertyName)
{
System.ComponentModel.AttributeCollection attributes =
TypeDescriptor.GetProperties(this)[propertyName].Attributes;
DefaultValueAttribute myAttribute =
(DefaultValueAttribute)attributes[typeof(DefaultValueAttribute)];
if (myAttribute != null)
return myAttribute.Value;
else
return null;
}

private string _GetImageMouseOver()
{
if (MouseOverImageUrl != null) return MouseOverImageUrl;

StringBuilder sb = new StringBuilder();
Regex re = new Regex(@"\.");
//Image should be like "media.gif" over image should be
"media_ovr.gif"
//and have a part left and right of the period
//if the entry is not in this format, then the entry is invalid
//so return null
if (ImageUrl == null) return null;

string[] sa = re.Split(ImageUrl);
if (sa.Length != 2) return null;
return sa[0] + "_ovr." + sa[1];
}
}

public class CustomImageDesigner:ControlDesigner
{
public override string GetDesignTimeHtml( )
{
return "<h6>" + this.ID + "</h6>";
}
}
}
-=-=-=-=- CustomImage.cs (end) -=-=-=-=-
-=-=-=-=- WebForm1.aspx (begin) -=-=-=-=-
<%@ Register TagPrefix="cust" Namespace="PostbackError"
Assembly="PostbackError" %>
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="PostbackError.WebForm1"
trace="true"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</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>
<form id="Form1" method="post" runat="server">
<br>
<cust:CustomImage id="ciTest" runat="server" ImageUrl="arrow.gif"
Border="1" />
<br>
<br>
<asp:DataGrid id="DataGrid1" runat="server" CellSpacing="1"
CellPadding="0" GridLines="None" AutoGenerateColumns="False"
AllowSorting="True" Width="100%">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:CheckBox id="ckSelectAll" runat="server"
AutoPostBack="false" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox id="ckSelect" runat="server" AutoPostBack="false"
/>
</ItemTemplate>
</asp:TemplateColumn>
<cust:CustomColumn HeaderText="L" ItemStyle-BackColor="#ff0000"/>
<asp:TemplateColumn>
<ItemTemplate>
<cust:CustomImage id="ciGridImage" runat="server"
ImageUrl="arrow.gif" Border="1" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="col1" HeaderText="Col1">
<ItemStyle HorizontalAlign="Left" />
</asp:BoundColumn>
<asp:BoundColumn DataField="col2" HeaderText="Col2">
<ItemStyle HorizontalAlign="Left" />
</asp:BoundColumn>
</Columns>
</asp:DataGrid>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
</form>
</body>
</HTML>
-=-=-=-=- WebForm1.aspx (end) -=-=-=-=-
-=-=-=-=- WebForm1.aspx.cs (begin) -=-=-=-=-
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 PostbackError
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected PostbackError.CustomImage ciTest;
protected System.Web.UI.WebControls.DataGrid DataGrid1;

ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add(new DataColumn("col1", typeof(Int32)));
dt.Columns.Add(new DataColumn("col2", typeof(string)));

for (int i = 0; i < 5; i++)
{
dr = dt.NewRow();

dr[0] = i;
dr[1] = "Item " + i.ToString();

dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;
}

private void Page_Load(object sender, System.EventArgs e)
{

if (!IsPostBack)
{
// Load this data only once.
DataGrid1.DataSource= CreateDataSource();

DataGrid1.DataBind();
}
}

#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.Load += new System.EventHandler(this.Page_Load);

}
#endregion

}
}
-=-=-=-=- WebForm1.aspx.cs (end) -=-=-=-=-
Nov 18 '05 #1
2 3159
I have similar kinda problem. Actually, I have a checkbox in my
datagrid which I created dynamically. Now when the user checks the
Checkboxes and hits submit, the Datagrid is recreated. I think i have
to add the checkbox value to ViewState. Is that correct.
Will appreciate your help.

Best regards!

go******************@illexcascade.com (Jay Walker) wrote in message news:<40************************@posting.google.co m>...
I created a custom DataGridColumn based on Marcie Robillard's MSDN
Article:

Creating Custom Columns for the ASP.NET Datagrid
http://msdn.microsoft.com/library/de...tomcolumns.asp

The problem I am having is that the data in the custom datagridcolumn
is not saved to viewstate and after postback, the column does not
contain data.

Any idea's?

I simplified the code so that the error is easy to reproduce.
Here is the code: (notice that the CustomImage that is standalone on
the page and the CustomImage that is embedded in a template column are
visible after postback.)

I can send a zip file of the project on request.

-=-=-=-=- CustomColumn.cs (begin) -=-=-=-=-
using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace PostbackError
{
public class CustomColumn : System.Web.UI.WebControls.DataGridColumn
{
public override void
InitializeCell(System.Web.UI.WebControls.TableCell cell, int
columnIndex, System.Web.UI.WebControls.ListItemType itemType)
{
//if the grid is using sorting, the the header will contain a
DataGridLinkButton
//the functionality is set by the base class initializecell.
base.InitializeCell (cell, columnIndex, itemType);
switch (itemType)
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
cell.DataBinding += new EventHandler(OnDataBinding);
break;
}}

private void OnDataBinding(object sender, EventArgs e)
{
TableCell cell = sender as TableCell;
DataGridItem dgi = cell.NamingContainer as DataGridItem;
DataRowView drv = dgi.DataItem as DataRowView;
//hardcoded for this example
string image = "arrow.gif";
if (image.Length == 0)
return;

CustomImage ri = new CustomImage();
cell.Controls.Add(ri);
TrackViewState();
string tooltip = "Help";
ri.ImageUrl = image;
ri.ToolTip = tooltip;
}}}
-=-=-=-=- CustomColumn.cs (end) -=-=-=-=-
-=-=-=-=- CustomImage.cs (begin) -=-=-=-=-
using System;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.Design;

namespace PostbackError
{
public class CustomImage : System.Web.UI.WebControls.Image
{
private string _imagePath = string.Empty;

public CustomImage():base()
{
_imagePath = string.Empty;
}

[
Browsable(true),
Category("Appearance"),
Description("Image to be displayed when the mouse is not over the
image")
]
public new string ImageUrl
{
get { return (string)ViewState["riImageUrl"]; }
set { ViewState["riImageUrl"] = value; }
}

[
Browsable(true),
Category("Appearance"),
Description("Image to be displayed when the mouse is over the
image")
]
public string MouseOverImageUrl
{
get { return (string)ViewState["MouseOverImageUrl"]; }
set { ViewState["MouseOverImageUrl"] = value; }
}

[
Browsable(true),
Category("Navigation"),
Description("The url to navigate to")
]
public string NavigateUrl
{
get { return (string)ViewState["NavigateUrl"]; }
set { ViewState["NavigateUrl"] = value; }
}

[
Browsable(true),
Category("Appearance"),
DefaultValue(true),
Description("Use a MouseOver Image derived from the Image name.
(i.e. image.gif -> image_ovr.gif)")
]
public bool UseMouseOverImage
{
get
{
string property = "UseMouseOverImage";
if (ViewState[property] == null)
{
object obj = _GetDefaultValue(property);
if (obj != null)
ViewState[property] = (bool)obj;
}
return (bool)ViewState[property];
}
set { ViewState["UseMouseOverImage"] = value; }
}
override protected void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
string s = base.ImageUrl;
base.ImageUrl = _imagePath + ImageUrl;
s = base.ImageUrl;
string mouseOver = _GetImageMouseOver();
if (mouseOver != null && UseMouseOverImage)
{
Attributes.Add("onmouseover", "this.src='" + _imagePath +
mouseOver + "'");
Attributes.Add("onmouseout", "this.src='" + _imagePath + ImageUrl
+ "'");
}
}

/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void Render(HtmlTextWriter output)
{
bool bWriteAnchor = NavigateUrl != null && NavigateUrl.Length > 0;
string s1 = base.ImageUrl;
base.ImageUrl = _imagePath + ImageUrl;

StringWriter sw = new StringWriter();
HtmlTextWriter buffer = new HtmlTextWriter(sw);

if (bWriteAnchor)
{
buffer.AddAttribute(HtmlTextWriterAttribute.Href, NavigateUrl);
buffer.RenderBeginTag(HtmlTextWriterTag.A);
}

base.Render(buffer);

if (bWriteAnchor)
buffer.RenderEndTag();

string s = sw.ToString();
output.Write(s);
}

private object _GetDefaultValue(string propertyName)
{
System.ComponentModel.AttributeCollection attributes =
TypeDescriptor.GetProperties(this)[propertyName].Attributes;
DefaultValueAttribute myAttribute =
(DefaultValueAttribute)attributes[typeof(DefaultValueAttribute)];
if (myAttribute != null)
return myAttribute.Value;
else
return null;
}

private string _GetImageMouseOver()
{
if (MouseOverImageUrl != null) return MouseOverImageUrl;

StringBuilder sb = new StringBuilder();
Regex re = new Regex(@"\.");
//Image should be like "media.gif" over image should be
"media_ovr.gif"
//and have a part left and right of the period
//if the entry is not in this format, then the entry is invalid
//so return null
if (ImageUrl == null) return null;

string[] sa = re.Split(ImageUrl);
if (sa.Length != 2) return null;
return sa[0] + "_ovr." + sa[1];
}
}

public class CustomImageDesigner:ControlDesigner
{
public override string GetDesignTimeHtml( )
{
return "<h6>" + this.ID + "</h6>";
}
}
}
-=-=-=-=- CustomImage.cs (end) -=-=-=-=-
-=-=-=-=- WebForm1.aspx (begin) -=-=-=-=-
<%@ Register TagPrefix="cust" Namespace="PostbackError"
Assembly="PostbackError" %>
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="PostbackError.WebForm1"
trace="true"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</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>
<form id="Form1" method="post" runat="server">
<br>
<cust:CustomImage id="ciTest" runat="server" ImageUrl="arrow.gif"
Border="1" />
<br>
<br>
<asp:DataGrid id="DataGrid1" runat="server" CellSpacing="1"
CellPadding="0" GridLines="None" AutoGenerateColumns="False"
AllowSorting="True" Width="100%">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:CheckBox id="ckSelectAll" runat="server"
AutoPostBack="false" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox id="ckSelect" runat="server" AutoPostBack="false"
/>
</ItemTemplate>
</asp:TemplateColumn>
<cust:CustomColumn HeaderText="L" ItemStyle-BackColor="#ff0000"/>
<asp:TemplateColumn>
<ItemTemplate>
<cust:CustomImage id="ciGridImage" runat="server"
ImageUrl="arrow.gif" Border="1" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="col1" HeaderText="Col1">
<ItemStyle HorizontalAlign="Left" />
</asp:BoundColumn>
<asp:BoundColumn DataField="col2" HeaderText="Col2">
<ItemStyle HorizontalAlign="Left" />
</asp:BoundColumn>
</Columns>
</asp:DataGrid>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
</form>
</body>
</HTML>
-=-=-=-=- WebForm1.aspx (end) -=-=-=-=-
-=-=-=-=- WebForm1.aspx.cs (begin) -=-=-=-=-
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 PostbackError
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected PostbackError.CustomImage ciTest;
protected System.Web.UI.WebControls.DataGrid DataGrid1;

ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add(new DataColumn("col1", typeof(Int32)));
dt.Columns.Add(new DataColumn("col2", typeof(string)));

for (int i = 0; i < 5; i++)
{
dr = dt.NewRow();

dr[0] = i;
dr[1] = "Item " + i.ToString();

dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;
}

private void Page_Load(object sender, System.EventArgs e)
{

if (!IsPostBack)
{
// Load this data only once.
DataGrid1.DataSource= CreateDataSource();

DataGrid1.DataBind();
}
}

#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.Load += new System.EventHandler(this.Page_Load);

}
#endregion

}
}
-=-=-=-=- WebForm1.aspx.cs (end) -=-=-=-=-

Nov 18 '05 #2
With some some help from Microsoft Support, the issue has been
resolved.

The simplest way to fix is to have any controls associated with a
column cell be created in the InitializeCell method that is invoked
every time the CustomColumn is built regardless of whether the
CustomColumn is databound.

In the code below, child controls were being added to the cell only in
the databind event. The contents of those controls were being saved to
the viewstate fine but when the page posts back, initializecell is
called, then loadviewstate is called. the viewstate information for
the cell exists, but since no child controls were created in the
initializeCell method, there is no control to associate with the
viewstate information.
Here is a code example to demonstrate the fix:

public override void
InitializeCell(System.Web.UI.WebControls.TableCell cell, int
columnIndex, System.Web.UI.WebControls.ListItemType itemType)
{
base.InitializeCell (cell, columnIndex, itemType);

Control boundControl = null;

switch (itemType)
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
boundControl = cell;
//must create the control inside InitializeCell so it is available
at LoadViewState
CustomImage ci = new CustomImage();
cell.Controls.Add(ci);
break;
}

if (boundControl != null)
boundControl.DataBinding += new EventHandler(ItemDataBinding);
}
private void ItemDataBinding(object sender, EventArgs e)
{
TableCell cell = sender as TableCell;
CustomImage ci = cell.Controls[0] as CustomImage;

if (ci == null) return;
//set some properties on ci. These will be saved/loaded from
viewstate because the ci control was created in InitializeCell

ci.ImageUrl = "arrow.gif";
ci.Visible = true;
}


go******************@illexcascade.com (Jay Walker) wrote in message news:<40************************@posting.google.co m>...
I created a custom DataGridColumn based on Marcie Robillard's MSDN
Article:

Creating Custom Columns for the ASP.NET Datagrid
http://msdn.microsoft.com/library/de...tomcolumns.asp

The problem I am having is that the data in the custom datagridcolumn
is not saved to viewstate and after postback, the column does not
contain data.

Any idea's?

I simplified the code so that the error is easy to reproduce.
Here is the code: (notice that the CustomImage that is standalone on
the page and the CustomImage that is embedded in a template column are
visible after postback.)

I can send a zip file of the project on request.

-=-=-=-=- CustomColumn.cs (begin) -=-=-=-=-
using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace PostbackError
{
public class CustomColumn : System.Web.UI.WebControls.DataGridColumn
{
public override void
InitializeCell(System.Web.UI.WebControls.TableCell cell, int
columnIndex, System.Web.UI.WebControls.ListItemType itemType)
{
//if the grid is using sorting, the the header will contain a
DataGridLinkButton
//the functionality is set by the base class initializecell.
base.InitializeCell (cell, columnIndex, itemType);
switch (itemType)
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
cell.DataBinding += new EventHandler(OnDataBinding);
break;
}}

private void OnDataBinding(object sender, EventArgs e)
{
TableCell cell = sender as TableCell;
DataGridItem dgi = cell.NamingContainer as DataGridItem;
DataRowView drv = dgi.DataItem as DataRowView;
//hardcoded for this example
string image = "arrow.gif";
if (image.Length == 0)
return;

CustomImage ri = new CustomImage();
cell.Controls.Add(ri);
TrackViewState();
string tooltip = "Help";
ri.ImageUrl = image;
ri.ToolTip = tooltip;
}}}
-=-=-=-=- CustomColumn.cs (end) -=-=-=-=-
-=-=-=-=- CustomImage.cs (begin) -=-=-=-=-
using System;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.Design;

namespace PostbackError
{
public class CustomImage : System.Web.UI.WebControls.Image
{
private string _imagePath = string.Empty;

public CustomImage():base()
{
_imagePath = string.Empty;
}

[
Browsable(true),
Category("Appearance"),
Description("Image to be displayed when the mouse is not over the
image")
]
public new string ImageUrl
{
get { return (string)ViewState["riImageUrl"]; }
set { ViewState["riImageUrl"] = value; }
}

[
Browsable(true),
Category("Appearance"),
Description("Image to be displayed when the mouse is over the
image")
]
public string MouseOverImageUrl
{
get { return (string)ViewState["MouseOverImageUrl"]; }
set { ViewState["MouseOverImageUrl"] = value; }
}

[
Browsable(true),
Category("Navigation"),
Description("The url to navigate to")
]
public string NavigateUrl
{
get { return (string)ViewState["NavigateUrl"]; }
set { ViewState["NavigateUrl"] = value; }
}

[
Browsable(true),
Category("Appearance"),
DefaultValue(true),
Description("Use a MouseOver Image derived from the Image name.
(i.e. image.gif -> image_ovr.gif)")
]
public bool UseMouseOverImage
{
get
{
string property = "UseMouseOverImage";
if (ViewState[property] == null)
{
object obj = _GetDefaultValue(property);
if (obj != null)
ViewState[property] = (bool)obj;
}
return (bool)ViewState[property];
}
set { ViewState["UseMouseOverImage"] = value; }
}
override protected void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
string s = base.ImageUrl;
base.ImageUrl = _imagePath + ImageUrl;
s = base.ImageUrl;
string mouseOver = _GetImageMouseOver();
if (mouseOver != null && UseMouseOverImage)
{
Attributes.Add("onmouseover", "this.src='" + _imagePath +
mouseOver + "'");
Attributes.Add("onmouseout", "this.src='" + _imagePath + ImageUrl
+ "'");
}
}

/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void Render(HtmlTextWriter output)
{
bool bWriteAnchor = NavigateUrl != null && NavigateUrl.Length > 0;
string s1 = base.ImageUrl;
base.ImageUrl = _imagePath + ImageUrl;

StringWriter sw = new StringWriter();
HtmlTextWriter buffer = new HtmlTextWriter(sw);

if (bWriteAnchor)
{
buffer.AddAttribute(HtmlTextWriterAttribute.Href, NavigateUrl);
buffer.RenderBeginTag(HtmlTextWriterTag.A);
}

base.Render(buffer);

if (bWriteAnchor)
buffer.RenderEndTag();

string s = sw.ToString();
output.Write(s);
}

private object _GetDefaultValue(string propertyName)
{
System.ComponentModel.AttributeCollection attributes =
TypeDescriptor.GetProperties(this)[propertyName].Attributes;
DefaultValueAttribute myAttribute =
(DefaultValueAttribute)attributes[typeof(DefaultValueAttribute)];
if (myAttribute != null)
return myAttribute.Value;
else
return null;
}

private string _GetImageMouseOver()
{
if (MouseOverImageUrl != null) return MouseOverImageUrl;

StringBuilder sb = new StringBuilder();
Regex re = new Regex(@"\.");
//Image should be like "media.gif" over image should be
"media_ovr.gif"
//and have a part left and right of the period
//if the entry is not in this format, then the entry is invalid
//so return null
if (ImageUrl == null) return null;

string[] sa = re.Split(ImageUrl);
if (sa.Length != 2) return null;
return sa[0] + "_ovr." + sa[1];
}
}

public class CustomImageDesigner:ControlDesigner
{
public override string GetDesignTimeHtml( )
{
return "<h6>" + this.ID + "</h6>";
}
}
}
-=-=-=-=- CustomImage.cs (end) -=-=-=-=-
-=-=-=-=- WebForm1.aspx (begin) -=-=-=-=-
<%@ Register TagPrefix="cust" Namespace="PostbackError"
Assembly="PostbackError" %>
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="PostbackError.WebForm1"
trace="true"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</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>
<form id="Form1" method="post" runat="server">
<br>
<cust:CustomImage id="ciTest" runat="server" ImageUrl="arrow.gif"
Border="1" />
<br>
<br>
<asp:DataGrid id="DataGrid1" runat="server" CellSpacing="1"
CellPadding="0" GridLines="None" AutoGenerateColumns="False"
AllowSorting="True" Width="100%">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:CheckBox id="ckSelectAll" runat="server"
AutoPostBack="false" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox id="ckSelect" runat="server" AutoPostBack="false"
/>
</ItemTemplate>
</asp:TemplateColumn>
<cust:CustomColumn HeaderText="L" ItemStyle-BackColor="#ff0000"/>
<asp:TemplateColumn>
<ItemTemplate>
<cust:CustomImage id="ciGridImage" runat="server"
ImageUrl="arrow.gif" Border="1" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="col1" HeaderText="Col1">
<ItemStyle HorizontalAlign="Left" />
</asp:BoundColumn>
<asp:BoundColumn DataField="col2" HeaderText="Col2">
<ItemStyle HorizontalAlign="Left" />
</asp:BoundColumn>
</Columns>
</asp:DataGrid>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
</form>
</body>
</HTML>
-=-=-=-=- WebForm1.aspx (end) -=-=-=-=-
-=-=-=-=- WebForm1.aspx.cs (begin) -=-=-=-=-
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 PostbackError
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected PostbackError.CustomImage ciTest;
protected System.Web.UI.WebControls.DataGrid DataGrid1;

ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add(new DataColumn("col1", typeof(Int32)));
dt.Columns.Add(new DataColumn("col2", typeof(string)));

for (int i = 0; i < 5; i++)
{
dr = dt.NewRow();

dr[0] = i;
dr[1] = "Item " + i.ToString();

dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;
}

private void Page_Load(object sender, System.EventArgs e)
{

if (!IsPostBack)
{
// Load this data only once.
DataGrid1.DataSource= CreateDataSource();

DataGrid1.DataBind();
}
}

#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.Load += new System.EventHandler(this.Page_Load);

}
#endregion

}
}
-=-=-=-=- WebForm1.aspx.cs (end) -=-=-=-=-

Nov 18 '05 #3

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

Similar topics

1
by: Lisa | last post by:
I have a web app that gets a recordset from the database and fills a grid. You can drilldown from this table to a detail table. Because the tables sometimes get huge, and because I have to go...
3
by: RCS | last post by:
I have an app that I have different "sections" that I want to switch back and forth from, all while having the server maintain viewstate for each page. In other words, when I am on Page1.aspx and...
7
by: Girish | last post by:
OK.. phew. Playing with data grids for the past few days has been fun and a huge learning experience.. My problem. I have a requirement to display a gird with a gird. Within the embedded grid,...
7
by: Shimon Sim | last post by:
I have a custom composite control I have following property
0
by: Walter | last post by:
Hi, can someone please help me with my custom control viewstate problem....I haven't slept for hours trying to get this fixed. I am making two custom controls which will be included on a single...
1
by: Andreas Bergmeier | last post by:
Hi. Is there some KB article how to use EnableEventValidation when doing customized posts? Basicly we're building a post string via javascript and issue it via means of XMLHttpRequest to the...
2
by: paulcis | last post by:
I am trying to produce a dynamic form from a database with unknown amount of records. I need to read the values and create a new textbox for each. I need to create the textboxes at page_init stage...
1
by: Veerle | last post by:
Hi, I use the infragistics tabcontrol on several pages of my application, that looks like this: <igtab:UltraWebTab ID="MyTabPage" runat="server"> <Tabs> <igtab:Tab Key="Tab1" Text="Title for...
8
by: Dan | last post by:
Hi, i experimented with postback and viewstate. With this code, there are 2 dropdownlists created, one visible and with AutoPostBack true, the other not visible and no AutoPostBack, and one...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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:
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...
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...

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.