Wednesday, September 16, 2009

Have link button inside ASPxGridview cell using ITemplate

Requirement :

This might be useful when you have list of items displayed in a ASPxGridview and a column have link which needs to open a popupmenu when user clicks on that link.
We have to create a ASPxPopupMenu with menu items and add ITemplate class to have link to be displayed inside the ASPxGridview cell. Define "CustomColumnDisplayText" event for the ASPxGridview and assign the ITemplate with the column. "CustomColumnDisplayText" event will be fired whenever the new record added to the ASPxGridview from Datasource. Then the ITemplate will be fired and place the control inside ASPxGridview cell.

I've given the above scenario with sample code.

Create menu at class scope
private ASPxPopupMenu menu;
private ASPxGridView gvControl;
Instantiate the menu this inside CreateChildControls() method and add menu items...
menu = new ASPxPopupMenu();
menu.ID = "menu";
menu.ClientInstanceName =”menu”;

menu.Items.Add('Add-Menu''mnuAdd');
gvControl = new ASPxGridView();
gvControl .ID = "gvControl ";
gvSalesOrderItemPegging.ClientInstanceName = "gvControl";
gvControl .SettingsBehavior.AllowFocusedRow = true;
gvControl .CustomColumnDisplayText += new ASPxGridViewColumnDisplayTextEventHandler(gvControl_CustomColumnDisplayText);
gvControl .SettingsBehavior.AllowSort = true;
// Kindly add the columns and assign datasource to the gvControl Gridview
Create ITemplate to display link in a Gridview cell.
public class MenuTemplate : ITemplate
{
ASPxHyperLink hp = new ASPxHyperLink();
// Create hyperlink to display the popup menu when user clicks on
hp.ID = "HyLink" + strHyperId;
hp.ClientInstanceName = "HyLink" + strHyperId;
hp.Text = strSelected;
hp.Cursor = "Hand";
sring strScript = "menu.ShowAtElement(s.mainElement);";
hp.ClientSideEvents.Click = String.Format("function(s, e) {{{0}}}", strScript);
container.Controls.Add(hp);
}
Create Gridview’s CustomColumnDisplayText event
private void gvControl_CustomColumnDisplayText(object sender, ASPxGridViewColumnDisplayTextEventArgs e)
{
If (e.Column.Name == "ReqMrpElementAbbr")
try
{
e.Column.DataItemTemplate = new MenuTemplate();
}
}

No comments:

Post a Comment