Webcontrol and custom button

I'm trying to set custom button in a webcontrol embedding the gridweb.

Here is my scenario:

  1. I have two custom buttons (DISPLAY and CHOOSE)
  2. Each button bind the grid to a different dataset
  3. visibility feature: When DISPLAY is shown, CHOOSE should not be visible (and inversely)

The event of the button are correctly trigger but, the visibility feature is not working and the grid doesn't load the right data (event if the event is triggered).

Your help will be greatly appreciate.
The scenario is a bit fuzzy, but i couldn't simplify further as this is the core goal of my project.
The code example is very simple tough.

Hi,

Thanks for the project files.

We will check and get back to you soon.

Thanks for being patient!

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);
}
}

Thank you very very much.
You are really something at Aspose!
It is working perfectly as always.