===========================copy paste start
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace GUI.Controls
{
///
/// Summary description for GvTemplate
///
public class GvTemplate : ITemplate
{
ListItemType _templateType;
string _columnName;
string _toolTip;
string _itemButtonName;
public GvTemplate (ListItemType type , string columnName , string toolTip , string itemButtonName )
{
_templateType = type;
this._columnName = columnName;
this._itemButtonName = itemButtonName;
this._toolTip = toolTip;
} //eof constructor
void ITemplate.InstantiateIn ( System.Web.UI.Control container )
{
switch (_templateType)
{
case ListItemType.Header :
Label lab = new Label ();
lab.Text = _columnName ;
container.Controls.Add( lab ) ;
break ;
case ListItemType.Item :
Button but = new Button ();
but.DataBinding += new EventHandler ( butDataBinding );
but.ToolTip = _toolTip;
but.Visible = true;
but.Text = "Include"; //todo: parametrize
container.Controls.Add ( but );
break;
} //eof switch _templateTyp
} //eof method InstantiateIn
void butDataBinding ( object sender , EventArgs e )
{
Button but = (Button) sender;
GridViewRow container = (GridViewRow) but.NamingContainer;
object dataValue = DataBinder.Eval ( container.DataItem , _columnName );
if (dataValue != null && dataValue != DBNull.Value)
{
but.ID = dataValue.ToString () ;
but.CommandName = "selectItem"; //todo:parametrize
but.CommandArgument = System.Convert.ToString ( dataValue );
but.Click += new EventHandler ( ClickSelectItem );
}
} //eof method butDataBinding
protected void but_Command ( Object sender , CommandEventArgs e )
{
}
protected void ClickSelectItem ( Object sender , EventArgs e )
{
Button but = (Button) sender ;
Utils.Debugger.WriteIf ( "I have been clicked by " + but.ID );
new Page ().Session [ "global.ClickedItemId" ] = but.ID;
} //eof method edit_button_Click
} //eof class
} //eof namespace GUI.Controls
===========================copy paste end
//In code behind create dynamically your gridview :
GridView gv = new GridView();
//add the handler (only if AutoEvenFireUp is false otherwize it will trigger it twice
gv.RowCommand += new GridViewCommandEventHandler ( RowCommand );
//add the TemplateField
TemplateField colField = new TemplateField ();
colField.HeaderTemplate = new GUI.Controls.GvTemplate
( ListItemType.Header , rdsForReportGv.Tables [ 0 ].Columns [ 0 ].Caption ,
itemButtonToolTip , itemButtonName );
colField.ItemTemplate = new GUI.Controls.GvTemplate (
ListItemType.Item , rdsForReportGv.Tables [ 0 ].Columns [ 0 ].Caption ,
itemButtonToolTip , itemButtonName );
gv.Columns.Add ( colField );
//implement the event handler for a row
protected void RowCommand ( Object sender , GridViewCommandEventArgs e )
{
string _commandName = e.CommandName;
switch (_commandName)
{
case "selectItem":
Utils.Debugger.WriteIf ( "I am in selectedItem" );
Utils.Debugger.WriteIf ( "My command argument is " + e.CommandArgument.ToString () );
Utils.Debugger.WriteIf ( "My command source is " + e.CommandSource.ToString () );
Utils.Debugger.WriteIf ( "My command name is " + e.CommandName );
break;
case "somethingElse":
Utils.Debugger.WriteIf ( "I amd in something else " );
break;
} //eof switch
}
No comments:
Post a Comment
- the first minus - Comments have to be moderated because of the spammers
- the second minus - I am very lazy at moderating comments ... hardly find time ...
- the third minus - Short links are no good for security ...
- The REAL PLUS : Any critic and positive feedback is better than none, so your comments will be published sooner or later !!!!