I am in the process of extending GridView and adding some new features.
I added a Single Click and Double Click, the first causes a postback and the second causes a CallBack.
The problem now is when I define an event handler for those two events, I get something as:
>> The type name 'Grid' does not exist in the type 'Grid.Grid'
The code is as follows, feel free to copy it into a new class called Grid:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Grid
{
///*** Add the BuildArgument methods***\\\
///
[DefaultProperty("Text")]
[ToolboxData("<{0}:Grid runat=server></{0}:Grid>")]
public class Grid : GridView
{
// Private fields to handle GetCallBackResult();
private bool isDoubleClick = false;
///*** TO BE CHANGED LATER ***\\\
//Add property attributes ...
/// <summary>
/// Specify whether this property is enabled or not
/// </summary>
[Bindable(true), Category("Behavior"), DefaultValue(false), Description("To enable RowClick or not")]
public bool EnableRowClick
{
get
{
bool enableRowClick = false;
object obj = ViewState["EnableRowClick"];
if (obj != null)
{
enableRowClick = (bool)obj;
}
return enableRowClick;
}
set
{
ViewState["EnableRowClick"] = value;
}
}
/// <summary>
/// Specify whether this property is enabled or not
/// </summary>
[Bindable(true), Category("Behavior"), DefaultValue(false), Description("To enable Double Click on a row or not")]
public bool EnableDoubleRowClick
{
get
{
bool enableDoubleRowClick = false;
object obj = ViewState["EnableDoubleRowClick"];
if (obj != null)
{
enableDoubleRowClick = (bool)obj;
}
return enableDoubleRowClick;
}
set
{
ViewState["EnableDoubleRowClick"] = value;
}
}
/// <summary>
/// Specify whether this property is enabled or not
/// </summary>
[Bindable(true), Category("Appearance"), DefaultValue(typeof(Color)), Description("Mouse over color on a row")]
public Color MoseOverColor
{
get
{
Color mouseOverColor = Color.White;
object obj = ViewState["MouseOverColor"];
if (obj != null)
{
mouseOverColor = (Color)obj;
}
return mouseOverColor;
}
set
{
ViewState["MouseOverColor"] = value;
}
}
// Define a new event called RowClick
private static readonly object EventRowClick = new object();
public delegate void RowClickEventHandler(object sender, RowClickEventArgs e);
public event RowClickEventHandler RowClick
{
add
{
base.Events.AddHandler(Grid.EventRowClick, value);
}
remove
{
base.Events.RemoveHandler(Grid.EventRowClick, value);
}
}
protected virtual void OnRowClick(RowClickEventArgs e)
{
// Set a selection in the Grid
this.SelectedIndex = e.GridViewRow.RowIndex;
RowClickEventHandler handler1 = (RowClickEventHandler)base.Events[Grid.EventRowClick];
if (handler1 != null)
{
handler1(this, e);
}
}
// Define a new event called RowDoubleClick
private static readonly object EventRowDoubleClick = new object();
public delegate void RowDoubleClickEventHandler(object sender, RowDoubleClickEventArgs e);
public event RowDoubleClickEventHandler RowDoubleClick
{
add
{
base.Events.AddHandler(Grid.EventRowDoubleClick, value);
}
remove
{
base.Events.RemoveHandler(Grid.EventRowDoubleClick, value);
}
}
protected virtual void OnRowDoubleClick(RowDoubleClickEventArgs e)
{
// Set a selection in the Grid
this.SelectedIndex = e.GridViewRow.RowIndex;
RowDoubleClickEventHandler handler1 = (RowDoubleClickEventHandler)base.Events[Grid.EventRowDoubleClick];
if (handler1 != null)
{
handler1(this, e);
}
}
// Override the PreRender to add some JS script
protected override void OnPreRender(EventArgs e)
{
// Hold the script key
string scriptKey = "";
// Single/Double click JS routines to manage MouseOver/MouseOut
if (EnableRowClick || EnableDoubleRowClick)
{
// Register two Js methods to change color when the mouse hovers over the row
scriptKey = "ChangeColor" + this.UniqueID;
if (!Page.ClientScript.IsStartupScriptRegistered(scriptKey))
{
StringBuilder script = new StringBuilder();
script.Append("var mouseOutColor;");
// Add two seperating line
script.Append("\r\n");
// MouseOverRowClick
script.Append("function MouseOverRow(source)");
script.Append("\r\n");
script.Append("{");
script.Append("\r\n\t");
script.Append("mouseOutColor = source.style.backgroundColor;");
script.Append("\r\n\t");
// Use the MouseOverColor Property set by the user
script.Append("source.style.backgroundColor = '" + MoseOverColor.ToKnownColor() + "';");
script.Append("\r\n");
script.Append("}");
// Add two seperating lines
script.Append("\r\n");
script.Append("\r\n");
// MouseOutRowClick
script.Append("function MouseOutRow(source)");
script.Append("\r\n");
script.Append("{");
script.Append("\r\n\t");
script.Append("source.style.backgroundColor = mouseOutColor;");
script.Append("\r\n");
script.Append("}");
script.Append("\r\n");
Page.ClientScript.RegisterStartupScript(typeof(Grid), scriptKey, script.ToString(), true);
}
}
// Add the CallBack Js routines needed for double click
if (EnableDoubleRowClick)
{
// Define callback reference.
String cbReference = Page.ClientScript.GetCallbackEventReference(this,"arg","ClientDoubleClick",null,false);
StringBuilder clientCallBackScript = new StringBuilder();
clientCallBackScript.Append("\r\n");
clientCallBackScript.Append("function DoubleClickCallBack(arg, context)");
clientCallBackScript.Append("\r\n");
clientCallBackScript.Append("{");
clientCallBackScript.Append("\r\n\t");
clientCallBackScript.AppendFormat("{0}", cbReference);
clientCallBackScript.Append("\r\n");
clientCallBackScript.Append("}");
clientCallBackScript.Append("\r\n");
// Add the client callback method, in this case it is a dummy function
clientCallBackScript.Append("function ClientDoubleClick(arg, context) {}");
clientCallBackScript.Append("\r\n");
// Register two Js methods to change color when the mouse hovers over the row
scriptKey = "DoubleClick" + this.UniqueID;
if (!Page.ClientScript.IsStartupScriptRegistered(scriptKey))
{
// Register script blocks will perform call to the server.
Page.ClientScript.RegisterStartupScript(typeof(Grid), scriptKey, clientCallBackScript.ToString(), true);
}
}
// Call base PreRender
base.OnPreRender(e);
}
// Customize each row to distinguish the RowClicked event
protected override void PrepareControlHierarchy()
{
base.PrepareControlHierarchy();
// Hold the row/double click row
string argsData = string.Empty;
// if the EnableRowClick is true, then customize the rows
if (EnableRowClick)
{
for (int i = 0; i < (base.Rows.Count); i++)
{
argsData = "rc" + base.Rows[i].RowIndex.ToString();
this.Rows[i].Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(this, argsData));
//this.Rows[i].Attributes.Add("onMouseOver", "MouseOverRowClick(this);");
//this.Rows[i].Attributes.Add("onMouseOut", "MouseOutRowClick(this);");
///*** TO BE CHANGED LATER ***\\\
//I will have a dropdownlist to change the cursor wanted
//this.Rows[i].Style.Add("cursor", "hand");
}
}
// if the EnableDoubleRowClick is true, then customize the rows
if (EnableDoubleRowClick)
{
for (int i = 0; i < (base.Rows.Count); i++)
{
argsData = "rdc" + base.Rows[i].RowIndex.ToString();
this.Rows[i].Attributes.Add("onDblClick", "DoubleClickCallBack('" + argsData + "','')");
//this.Rows[i].Attributes.Add("onMouseOver", "MouseOverRowClick(this);");
//this.Rows[i].Attributes.Add("onMouseOut", "MouseOutRowClick(this);");
///*** TO BE CHANGED LATER ***\\\
//I will have a dropdownlist to change the cursor wanted
//this.Rows[i].Style.Add("cursor", "hand");
}
}
// If any of the two properties is enabled add the following
// customization features.
if (EnableRowClick || EnableDoubleRowClick)
{
for (int i = 0; i < (base.Rows.Count); i++)
{
this.Rows[i].Attributes.Add("onMouseOver", "MouseOverRow(this);");
this.Rows[i].Attributes.Add("onMouseOut", "MouseOutRow(this);");
}
}
}
// Override the RaisePostBackEvent to handle the Row Click
protected override void RaisePostBackEvent(string eventArgument)
{
if (eventArgument.StartsWith("rc"))
{
int rowClicked = Int32.Parse(eventArgument.Substring(2));
RowClickEventArgs rowClickEventArgs = new RowClickEventArgs(this.Rows[rowClicked]);
OnRowClick(rowClickEventArgs);
}
else
{
base.RaisePostBackEvent(eventArgument);
}
}
// Override the RaiseCallbackEvent to handle the row double click
protected override void RaiseCallbackEvent(string eventArgument)
{
if (eventArgument.StartsWith("rdc"))
{
int rowDoubleClicked = Int32.Parse(eventArgument.Substring(3));
RowDoubleClickEventArgs rowDoubleClickEventArgs = new RowDoubleClickEventArgs(this.Rows[rowDoubleClicked]);
isDoubleClick = true;
OnRowDoubleClick(rowDoubleClickEventArgs);
}
else
{
base.RaiseCallbackEvent(eventArgument);
}
}
// Override GetCallBackResult to process client call back event
protected override string GetCallbackResult()
{
// if Double Click Event, return nothing
if (isDoubleClick)
{
return string.Empty;
}
// return normal result to client
return base.GetCallbackResult();
}
}
// RowClickEventArgs defined
public class RowClickEventArgs : EventArgs
{
private GridViewRow gridViewRow;
public GridViewRow GridViewRow
{
get { return gridViewRow; }
set { gridViewRow = value; }
}
public RowClickEventArgs()
{
}
public RowClickEventArgs(GridViewRow gridViewRow)
{
this.gridViewRow = gridViewRow;
}
}
// RowDoubleClickEventArgs defined
public class RowDoubleClickEventArgs : EventArgs
{
private GridViewRow gridViewRow;
public GridViewRow GridViewRow
{
get { return gridViewRow; }
set { gridViewRow = value; }
}
public RowDoubleClickEventArgs()
{
}
public RowDoubleClickEventArgs(GridViewRow gridViewRow)
{
this.gridViewRow = gridViewRow;
}
}
}
haidar_bilal
All-Star
45609 Points
8730 Posts
MVP
Extend GridView
Jun 20, 2006 07:04 PM|LINK
Hello:
I am in the process of extending GridView and adding some new features.
I added a Single Click and Double Click, the first causes a postback and the second causes a CallBack.
The problem now is when I define an event handler for those two events, I get something as:
>> The type name 'Grid' does not exist in the type 'Grid.Grid'
The code is as follows, feel free to copy it into a new class called Grid:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace Grid { ///*** Add the BuildArgument methods***\\\ /// [DefaultProperty("Text")] [ToolboxData("<{0}:Grid runat=server></{0}:Grid>")] public class Grid : GridView { // Private fields to handle GetCallBackResult(); private bool isDoubleClick = false; ///*** TO BE CHANGED LATER ***\\\ //Add property attributes ... /// <summary> /// Specify whether this property is enabled or not /// </summary> [Bindable(true), Category("Behavior"), DefaultValue(false), Description("To enable RowClick or not")] public bool EnableRowClick { get { bool enableRowClick = false; object obj = ViewState["EnableRowClick"]; if (obj != null) { enableRowClick = (bool)obj; } return enableRowClick; } set { ViewState["EnableRowClick"] = value; } } /// <summary> /// Specify whether this property is enabled or not /// </summary> [Bindable(true), Category("Behavior"), DefaultValue(false), Description("To enable Double Click on a row or not")] public bool EnableDoubleRowClick { get { bool enableDoubleRowClick = false; object obj = ViewState["EnableDoubleRowClick"]; if (obj != null) { enableDoubleRowClick = (bool)obj; } return enableDoubleRowClick; } set { ViewState["EnableDoubleRowClick"] = value; } } /// <summary> /// Specify whether this property is enabled or not /// </summary> [Bindable(true), Category("Appearance"), DefaultValue(typeof(Color)), Description("Mouse over color on a row")] public Color MoseOverColor { get { Color mouseOverColor = Color.White; object obj = ViewState["MouseOverColor"]; if (obj != null) { mouseOverColor = (Color)obj; } return mouseOverColor; } set { ViewState["MouseOverColor"] = value; } } // Define a new event called RowClick private static readonly object EventRowClick = new object(); public delegate void RowClickEventHandler(object sender, RowClickEventArgs e); public event RowClickEventHandler RowClick { add { base.Events.AddHandler(Grid.EventRowClick, value); } remove { base.Events.RemoveHandler(Grid.EventRowClick, value); } } protected virtual void OnRowClick(RowClickEventArgs e) { // Set a selection in the Grid this.SelectedIndex = e.GridViewRow.RowIndex; RowClickEventHandler handler1 = (RowClickEventHandler)base.Events[Grid.EventRowClick]; if (handler1 != null) { handler1(this, e); } } // Define a new event called RowDoubleClick private static readonly object EventRowDoubleClick = new object(); public delegate void RowDoubleClickEventHandler(object sender, RowDoubleClickEventArgs e); public event RowDoubleClickEventHandler RowDoubleClick { add { base.Events.AddHandler(Grid.EventRowDoubleClick, value); } remove { base.Events.RemoveHandler(Grid.EventRowDoubleClick, value); } } protected virtual void OnRowDoubleClick(RowDoubleClickEventArgs e) { // Set a selection in the Grid this.SelectedIndex = e.GridViewRow.RowIndex; RowDoubleClickEventHandler handler1 = (RowDoubleClickEventHandler)base.Events[Grid.EventRowDoubleClick]; if (handler1 != null) { handler1(this, e); } } // Override the PreRender to add some JS script protected override void OnPreRender(EventArgs e) { // Hold the script key string scriptKey = ""; // Single/Double click JS routines to manage MouseOver/MouseOut if (EnableRowClick || EnableDoubleRowClick) { // Register two Js methods to change color when the mouse hovers over the row scriptKey = "ChangeColor" + this.UniqueID; if (!Page.ClientScript.IsStartupScriptRegistered(scriptKey)) { StringBuilder script = new StringBuilder(); script.Append("var mouseOutColor;"); // Add two seperating line script.Append("\r\n"); // MouseOverRowClick script.Append("function MouseOverRow(source)"); script.Append("\r\n"); script.Append("{"); script.Append("\r\n\t"); script.Append("mouseOutColor = source.style.backgroundColor;"); script.Append("\r\n\t"); // Use the MouseOverColor Property set by the user script.Append("source.style.backgroundColor = '" + MoseOverColor.ToKnownColor() + "';"); script.Append("\r\n"); script.Append("}"); // Add two seperating lines script.Append("\r\n"); script.Append("\r\n"); // MouseOutRowClick script.Append("function MouseOutRow(source)"); script.Append("\r\n"); script.Append("{"); script.Append("\r\n\t"); script.Append("source.style.backgroundColor = mouseOutColor;"); script.Append("\r\n"); script.Append("}"); script.Append("\r\n"); Page.ClientScript.RegisterStartupScript(typeof(Grid), scriptKey, script.ToString(), true); } } // Add the CallBack Js routines needed for double click if (EnableDoubleRowClick) { // Define callback reference. String cbReference = Page.ClientScript.GetCallbackEventReference(this,"arg","ClientDoubleClick",null,false); StringBuilder clientCallBackScript = new StringBuilder(); clientCallBackScript.Append("\r\n"); clientCallBackScript.Append("function DoubleClickCallBack(arg, context)"); clientCallBackScript.Append("\r\n"); clientCallBackScript.Append("{"); clientCallBackScript.Append("\r\n\t"); clientCallBackScript.AppendFormat("{0}", cbReference); clientCallBackScript.Append("\r\n"); clientCallBackScript.Append("}"); clientCallBackScript.Append("\r\n"); // Add the client callback method, in this case it is a dummy function clientCallBackScript.Append("function ClientDoubleClick(arg, context) {}"); clientCallBackScript.Append("\r\n"); // Register two Js methods to change color when the mouse hovers over the row scriptKey = "DoubleClick" + this.UniqueID; if (!Page.ClientScript.IsStartupScriptRegistered(scriptKey)) { // Register script blocks will perform call to the server. Page.ClientScript.RegisterStartupScript(typeof(Grid), scriptKey, clientCallBackScript.ToString(), true); } } // Call base PreRender base.OnPreRender(e); } // Customize each row to distinguish the RowClicked event protected override void PrepareControlHierarchy() { base.PrepareControlHierarchy(); // Hold the row/double click row string argsData = string.Empty; // if the EnableRowClick is true, then customize the rows if (EnableRowClick) { for (int i = 0; i < (base.Rows.Count); i++) { argsData = "rc" + base.Rows[i].RowIndex.ToString(); this.Rows[i].Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(this, argsData)); //this.Rows[i].Attributes.Add("onMouseOver", "MouseOverRowClick(this);"); //this.Rows[i].Attributes.Add("onMouseOut", "MouseOutRowClick(this);"); ///*** TO BE CHANGED LATER ***\\\ //I will have a dropdownlist to change the cursor wanted //this.Rows[i].Style.Add("cursor", "hand"); } } // if the EnableDoubleRowClick is true, then customize the rows if (EnableDoubleRowClick) { for (int i = 0; i < (base.Rows.Count); i++) { argsData = "rdc" + base.Rows[i].RowIndex.ToString(); this.Rows[i].Attributes.Add("onDblClick", "DoubleClickCallBack('" + argsData + "','')"); //this.Rows[i].Attributes.Add("onMouseOver", "MouseOverRowClick(this);"); //this.Rows[i].Attributes.Add("onMouseOut", "MouseOutRowClick(this);"); ///*** TO BE CHANGED LATER ***\\\ //I will have a dropdownlist to change the cursor wanted //this.Rows[i].Style.Add("cursor", "hand"); } } // If any of the two properties is enabled add the following // customization features. if (EnableRowClick || EnableDoubleRowClick) { for (int i = 0; i < (base.Rows.Count); i++) { this.Rows[i].Attributes.Add("onMouseOver", "MouseOverRow(this);"); this.Rows[i].Attributes.Add("onMouseOut", "MouseOutRow(this);"); } } } // Override the RaisePostBackEvent to handle the Row Click protected override void RaisePostBackEvent(string eventArgument) { if (eventArgument.StartsWith("rc")) { int rowClicked = Int32.Parse(eventArgument.Substring(2)); RowClickEventArgs rowClickEventArgs = new RowClickEventArgs(this.Rows[rowClicked]); OnRowClick(rowClickEventArgs); } else { base.RaisePostBackEvent(eventArgument); } } // Override the RaiseCallbackEvent to handle the row double click protected override void RaiseCallbackEvent(string eventArgument) { if (eventArgument.StartsWith("rdc")) { int rowDoubleClicked = Int32.Parse(eventArgument.Substring(3)); RowDoubleClickEventArgs rowDoubleClickEventArgs = new RowDoubleClickEventArgs(this.Rows[rowDoubleClicked]); isDoubleClick = true; OnRowDoubleClick(rowDoubleClickEventArgs); } else { base.RaiseCallbackEvent(eventArgument); } } // Override GetCallBackResult to process client call back event protected override string GetCallbackResult() { // if Double Click Event, return nothing if (isDoubleClick) { return string.Empty; } // return normal result to client return base.GetCallbackResult(); } } // RowClickEventArgs defined public class RowClickEventArgs : EventArgs { private GridViewRow gridViewRow; public GridViewRow GridViewRow { get { return gridViewRow; } set { gridViewRow = value; } } public RowClickEventArgs() { } public RowClickEventArgs(GridViewRow gridViewRow) { this.gridViewRow = gridViewRow; } } // RowDoubleClickEventArgs defined public class RowDoubleClickEventArgs : EventArgs { private GridViewRow gridViewRow; public GridViewRow GridViewRow { get { return gridViewRow; } set { gridViewRow = value; } } public RowDoubleClickEventArgs() { } public RowDoubleClickEventArgs(GridViewRow gridViewRow) { this.gridViewRow = gridViewRow; } } }Any idea?
Thanks.
Professional ASP.NET 3.5 Security, Membership, and Role Management with C# and VB
haidar_bilal
All-Star
45609 Points
8730 Posts
MVP
Re: Extend GridView
Jun 21, 2006 06:31 AM|LINK
I solved the problem by changing the extended Grid name. There was a conflict in fact between the Assembly and the Grid class it self, both were Grid.
But now, there is another problem, if I allow editing in the Grid, which means I will be calling base.RaisePostBackEvent method, and the exception is:
xGrid didn't handle the RowUpdating event!
Any new ideas [:D].
Thanks.
Professional ASP.NET 3.5 Security, Membership, and Role Management with C# and VB
haidar_bilal
All-Star
45609 Points
8730 Posts
MVP
Re: Extend GridView
Jun 21, 2006 10:42 AM|LINK
Everything is ok now. Minor changes only!!
Still working on making the CallBack thingy work fine for the double click event!
Regards
Professional ASP.NET 3.5 Security, Membership, and Role Management with C# and VB
tijana
Member
5 Points
6 Posts
Re: Extend GridView
Mar 04, 2008 02:48 PM|LINK
What if you want onClick and onDblClick to work together?
Is it possible?
For example, to use click on row to enable some button and
double click to redirect to another page.
So, is ti possible?
Mohammed Ask...
Contributor
2370 Points
535 Posts
Re: Extend GridView
Oct 17, 2009 10:34 AM|LINK
Hi
I want to extend a grid
can you send me samle code ?
http://askarnet.wordpress.com
lafjcr
Member
2 Points
1 Post
Re: Extend GridView
Nov 09, 2009 02:43 AM|LINK
Hi dude,
can you send me the fixed code plz?
Thanks in advance.
zouche
Member
2 Points
1 Post
Re: Extend GridView
Mar 16, 2010 07:00 PM|LINK
hi
thank you for this class
i found your code very interesting but i have some problems
i'd like to use OnClick
in the method OnPreRender, i use viewstate to add rows in my grid
i get exception : index out of range
and only OnClick event don't work, the delete event and OnMouseRollOn works perfectly
any solution please