Hi,
The CustomCommandButton doesn't support Visible property. I think you may handle each click event and change its Command in this senario.
And your code have some wrong control flow. Here is the modified code, and it runs fine at my machine.
[WebCustomControl1.cs]
using System;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aspose.Grid.Web;
using Aspose.Grid.Web.Data;
using System.Data;
namespace testwebcontrol
{
public class WebCustomControl1 : WebControl, INamingContainer
{
private GridWeb grid;
private bool needInit;
public WebCustomControl1()
{
grid = new GridWeb();
grid.ID = "InnerGrid";
grid.CustomCommand += new CustomCommandEventHandler(grid_CustomCommand);
needInit = false;
}
public bool NeedInit
{
get { return needInit; }
set { needInit = value; }
}
protected override void CreateChildControls()
{
base.CreateChildControls();
Controls.Add(grid);
if (needInit)
InitGrid();
}
private void InitGrid()
{
grid.EditMode = false;
//creating custom buttons
CustomCommandButton cmdChoose = new CustomCommandButton();
cmdChoose.Command = "Choose";
cmdChoose.ToolTip = "Choose";
grid.CustomCommandButtons.Add(cmdChoose);
//init the grid with dataset 0
WebWorksheet sheet = grid.WebWorksheets[0];
DataTable data0 = new DataTable();
data0.Columns.Add("data0");
data0.Rows.Add(new object[]{"0"});
data0.Rows.Add(new object[]{"0"});
sheet.DataSource = data0;
sheet.CreateAutoGenratedColumns();
sheet.DataBind();
}
private void grid_CustomCommand(object sender, string command)
{
string key = null;
if (grid.ActiveCell != null )
key = (string)grid.ActiveCell.Value;
else
key="data0";
WebWorksheet sheet = grid.WebWorksheets[0];
//command cases:
if (command == "Choose")
{
//display a choice of dataset for the user to select one
DataTable choice = new DataTable();
sheet.Cells.Clear();
choice.Columns.Add("select one");
choice.Rows.Add(new object[]{"data0"});
choice.Rows.Add(new object[]{"data1"});
sheet.DataSource = choice;
sheet.CreateAutoGenratedColumns();
sheet.DataBind();
grid.CustomCommandButtons[0].Command = "Display";
grid.CustomCommandButtons[0].ToolTip = "Display";
}
if( command=="Display" & key== "data0")
{
//display datset 0
DataTable data0 = new DataTable();
sheet.Cells.Clear();
data0.Columns.Add("data0");
data0.Rows.Add(new object[]{"0"});
data0.Rows.Add(new object[]{"0"});
sheet.DataSource = data0;
sheet.CreateAutoGenratedColumns();
sheet.DataBind();
grid.CustomCommandButtons[0].Command = "Choose";
grid.CustomCommandButtons[0].ToolTip = "Choose";
}
if (command == "Display" & key == "data1")
{
//display dataset 1
DataTable data1 = new DataTable();
sheet.Cells.Clear();
data1.Columns.Add("data1");
data1.Rows.Add(new object[]{"1"});
data1.Rows.Add(new object[]{"1"});
sheet.DataSource = data1;
sheet.CreateAutoGenratedColumns();
sheet.DataBind();
grid.CustomCommandButtons[0].Command = "Choose";
grid.CustomCommandButtons[0].ToolTip = "Choose";
}
}
}
}
[test.aspx.cs]
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using testwebcontrol;
public class test : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlForm form;
private void Page_Load(object sender, System.EventArgs e)
{
WebCustomControl1 ctrl = new WebCustomControl1();
ctrl.ID = "Grid1";
ctrl.NeedInit = !IsPostBack;
form.Controls.Add(ctrl);
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
}