请参考
- public class MyItemTemplate : FarPoint.Web.Spread.MenuItem, ITemplate
- {
- public MyItemTemplate(string text, string commandName, string commandArgument): base(text, commandName, commandArgument){}
- public void InstantiateIn(Control container)
- {
- if (container != null)
- {
- //Add a custom control as item
- CheckBox check1 = new CheckBox();
- check1.ID = base.CommandName;
- check1.Text = base.Text;
- if (base.OnClientClick != null)
- check1.Attributes.Add("onclick", OnClientClick);
- container.Controls.Add(check1);
- }
- }
- }
复制代码
- protected void Page_Load(object sender, EventArgs e)
- {
- FpSpread1.MenuItemClicked += new FarPoint.Web.Spread.MenuItemClickedHandler(FpSpread1_MenuItemClicked);
- if (this.IsPostBack) return;
- FpSpread1.EnableContextMenu = true;
- BuildMenu();
- }
- protected void FpSpread1_MenuItemClicked(object sender, FarPoint.Web.Spread.MenuItemClickEventArgs eventArgs)
- {
- FpSpread1.Sheets[0].Cells[0, 0].Text = eventArgs.SelectedItem.CommandName.ToString();
- FpSpread1.Sheets[0].Cells[0, 1].Text = eventArgs.SelectedItem.CommandArgument.ToString();
- BuildMenu();
- }
- private void BuildMenu()
- {
- FpSpread1.ContextMenus.Clear();
- FarPoint.Web.Spread.ContextMenu viewportMenu = new FarPoint.Web.Spread.ContextMenu();
- viewportMenu.Type = FarPoint.Web.Spread.ContextMenuType.Viewport;
- string itemText = string.Empty;
- string itemCmdName = string.Empty;
- string itemCmdArg = string.Empty;
- for (int i = 0; i < 3; i++)
- {
- itemText = string.Format("Menu item {0}", i + 1);
- itemCmdName = string.Format("cmd{0}", i + 1);
- itemCmdArg = string.Format("arg{0}", i + 1);
- FarPoint.Web.Spread.MenuItem item = new FarPoint.Web.Spread.MenuItem(itemText, itemCmdName, itemCmdArg);
- if (i == 1)
- {
- MyItemTemplate myItem = new MyItemTemplate(itemText, itemCmdName, itemCmdArg);
- myItem.OnClientClick = string.Format("javascript:alert('cmd = {0}, checked = ' + this.checked); return;", itemCmdName);
- item.ItemTemplate = myItem;
- }
- else if (i == 2)
- {
- for (int j = 0; j < 3; j++)
- {
- itemText = string.Format("Menu item {0}.{1}", i + 1, j + 1);
- itemCmdName = string.Format("cmd{0}${1}", i + 1, j + 1);
- itemCmdArg = string.Format("arg{0}${1}", i + 1, j + 1);
- FarPoint.Web.Spread.MenuItem newItem = new FarPoint.Web.Spread.MenuItem(itemText, itemCmdName, itemCmdArg);
- if (j == 0)
- {
- MyItemTemplate myItem = new MyItemTemplate(itemText, itemCmdName, itemCmdArg);
- myItem.OnClientClick = string.Format("javascript:alert('cmd = {0}, checked = ' + this.checked); return;", itemCmdName);
- newItem.ItemTemplate = myItem;
- }
- item.ChildItems.Add(newItem);
- }
- }
- viewportMenu.Items.Add(item);
- }
- FpSpread1.ContextMenus.Add(viewportMenu);
- }
复制代码
|