Hi guys,
I've not worked with anything but simply controls for a year or two and so am a bit rusty and need some help.
I want to allow a usder to create a product categories structure, with a parent child relationship which can be any depth.
To thsi end I want to create a control to represent a level with a listbox to show the sections and buttons to add,edit and delete. These controls will be dynmicaly added and removed depending. I have some basic code to test the eventing side of things which is not working. The structure is.
Page -> ProductCategoryCollection -> ProductCategoryView
I want to fire an event with a command argument and have it receivied on the page. Please can you let me know why this code is not working.
Cheers
Steve
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WhiteLabelCMS.Products
{
public partial class ManageProductCategories : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
var pcv = new ProductCategoryCollection();
pcv.ButtonClick += HandleEvent;
phProductCatViews.Controls.Add(pcv);
}
public void HandleEvent(object sender,CommandEventArgs e)
{
Response.Write("Button " + e.CommandArgument +" clicked");
}
}
public class ProductCategoryCollection : WebControl
{
public event CommandEventHandler ButtonClick;
public ProductCategoryCollection()
{
for (int count = 0; count < 3; count++)
{
var pcv = new ProductCategoryView(count);
pcv.EditButtonClick+=(PcvEditButtonClick);
Controls.Add(pcv);
}
}
public void PcvEditButtonClick(object sender, CommandEventArgs e)
{
ButtonClick(sender, e);
}
public int ProductLevelCount
{
get
{
if (ViewState["ProductLevelCount"] != null)
return (int)ViewState["ProductLevelCount"];
return 1;
}
set { ViewState["ProductLevelCount"] = value; }
}
}
public class ProductCategoryView : WebControl
{
public event CommandEventHandler EditButtonClick;
protected Button _editButton;
public ProductCategoryView(int level)
{
Level = level;
CreateControls();
}
private void CreateControls()
{
_editButton = new Button { Text = "Edit", CommandArgument = Level.ToString(), ID = "btnEdit_" + Level };
_editButton.Command+=(EditButtonClick);
Controls.Add(_editButton);
}
public int Level
{
get
{
if (ViewState["LevelCount"] != null)
return (int)ViewState["LevelCount"];
return 1;
}
set { ViewState["LevelCount"] = value; }
}
}
}