Welcome Guest Search | Active Topics | Sign In | Register

CheckBox CustomItem InstantiateIn Options
Ram Thatikonda
Posted: Tuesday, August 10, 2010 4:04:07 PM
Rank: Member
Groups: Member

Joined: 9/8/2009
Posts: 10
Hi All,

I would like to add a CheckBox as a ToolBarItem to the ToolBar control programmatically, maintain its state across postbacks and should be able to implement server side CheckedChanged event.

Could somebody please share some sample code or guide me the right direction? I would really appreciate!!

Thanks
eo_support
Posted: Tuesday, August 10, 2010 4:08:57 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

You will need to create an object that implements ITemplate, then assign that object to the ToolBarItem's CustomItem property. Make sure you do this at the early stage of the page's life cycle so that your controls are already there when view state kicks in.

Thanks!
Ram Thatikonda
Posted: Tuesday, August 10, 2010 4:14:58 PM
Rank: Member
Groups: Member

Joined: 9/8/2009
Posts: 10
I tried this code but it does not work on postbacks -

public class CustomToolBarItemTemplate : ITemplate
{
private string _Label;

public CustomToolBarItemTemplate(string label)
{
_Label = label;
}

public void InstantiateIn(Control container)
{
CheckBox cb = new CheckBox();
cb.AutoPostBack = true;
cb.Text = _Label
cb.TextAlign = TextAlign.Right;
container.Controls.Add(cb);
}
}

In the page load event within (!IsPostBack) -

EO.Web.ToolBarItem menuItem = new EO.Web.ToolBarItem();
menuItem.Type = EO.Web.ToolBarItemType.Custom;
menuItem.CustomItem = new CustomToolBarItemTemplate("Ring Rollup");
ItemHeaderToolBar.Items.Add(menuItem);

How do I maintain the state between postbacks and where do I implement the checkbox CheckedChanged event? I would appreciate sharing some sample code.

Thanks
eo_support
Posted: Tuesday, August 10, 2010 4:23:18 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

Your code is quite right except that you are creating your toolbar item too late. Try to move the code into Page_Init and see if that works for you. You do not have to do anything special for server event to work correctly as long as you create your control early enough and create them exactly the same way every time. In fact this is exactly what .aspx does: ASP.NET creates the same control tree every time the same way based on the same .aspx file, then hand over that tree to your code to perform additional logics. Everything else just works because the control tree was created at very early stage of the page's life cycle and created the same way every time.

Thanks!
Ram Thatikonda
Posted: Tuesday, August 10, 2010 4:35:59 PM
Rank: Member
Groups: Member

Joined: 9/8/2009
Posts: 10
Between postbacks the regular ToolBarItem works both in page_init and page_load events. The CustomItem does now work in neither events.

I would appreciate if you could quickly build a sample to test it out.

Initially, I thought of building the whole toolbar at design time in the ASPX page but my requirement is to show and hide options dynamically based on a condition. Is there any alternative to build such kind of requirement?
eo_support
Posted: Tuesday, August 10, 2010 5:32:35 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

We are not able to get it to work either. We will look into it and get back to you as soon as possible.

Thanks!
eo_support
Posted: Tuesday, August 10, 2010 6:39:02 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

Please use the following code:

Code: C#
public void InstantiateIn(System.Web.UI.Control container)
{
    //Add a DIV around the checkbox and also calls
    //eo_CancelBubble when the div is clicked
    container.Controls.Add(
        new LiteralControl("<div onclick='eo_CancelBubble(event);'>"));

    CheckBox cb = new CheckBox();
    cb.AutoPostBack = true;
    cb.Text = _Label;
    cb.TextAlign = TextAlign.Right;
    container.Controls.Add(cb);

    //Close the div element
    container.Controls.Add(
        new LiteralControl("</div>"));
}


Code: C#
//Override OnInit to create the custom item
protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    EO.Web.ToolBarItem menuItem = new EO.Web.ToolBarItem();
    menuItem.Type = EO.Web.ToolBarItemType.Custom;
    menuItem.CustomItem = new CustomToolBarItemTemplate("Ring Rollup");
    ToolBar1.Items.Add(menuItem);
}


The key is the additional DIV element placed around the checkbox. Without it the post back does occur but the "check" event would canceled by the toolbar.

Thanks!
Ram Thatikonda
Posted: Tuesday, August 10, 2010 6:55:48 PM
Rank: Member
Groups: Member

Joined: 9/8/2009
Posts: 10
I am still having issues.
1) I had to put the check (!IsPostBack) around regular ToolBarItem items and add the CustomItem always. Otherwise the regular ToolBarItem items get added on every postback.
2) My html markup has right padding to the ToolBarItem. On every postback, the items are shifted to the left by the number of pixels mentioned in the padding.
3) How do I attach the event handler in the ITemplate class and how should the event be implemented in the aspx page?

Note: I have changed the name of the class CustomToolBarItemTemplate to CheckBoxToolBarItemTemplate.

Here is the markup and the code -

<eo:ToolBar ID="ItemHeaderToolBar" runat="server" Width="300px" AutoPostBack="True"
TextAlign="Right">
<NormalStyle CssText="padding-right:10px;" />
<HoverStyle CssText="cursor:pointer;padding-right:10px;" />
</eo:ToolBar>

public class CheckBoxToolBarItemTemplate : ITemplate
{
private string _Label;

public CheckBoxToolBarItemTemplate(string label)
{
_Label = label;
}

public void InstantiateIn(Control container)
{
//Add a DIV around the checkbox and also calls
//eo_CancelBubble when the div is clicked
container.Controls.Add(
new LiteralControl("<div onclick='eo_CancelBubble(event);'>"));

CheckBox cb = new CheckBox();
cb.AutoPostBack = true;
cb.Text = _Label;
cb.TextAlign = TextAlign.Right;
container.Controls.Add(cb);

//Close the div element
container.Controls.Add(
new LiteralControl("</div>"));
}
}

public bool ShowCreateRings
{
get
{
return ViewState["ShowCreateRings"] == null ? false : Convert.ToBoolean(ViewState["ShowCreateRings"]);
}
set
{
ViewState["ShowCreateRings"] = value;
}
}

public bool ShowLikeSku
{
get
{
return ViewState["ShowLikeSku"] == null ? false : Convert.ToBoolean(ViewState["ShowLikeSku"]);
}
set
{
ViewState["ShowLikeSku"] = value;
}
}

public bool ShowRetrieveItems
{
get
{
return ViewState["ShowRetrieveItems"] == null ? false : Convert.ToBoolean(ViewState["ShowRetrieveItems"]);
}
set
{
ViewState["ShowRetrieveItems"] = value;
}
}

public bool ShowDefaultSort
{
get
{
return ViewState["ShowDefaultSort"] == null ? false : Convert.ToBoolean(ViewState["ShowDefaultSort"]);
}
set
{
ViewState["ShowDefaultSort"] = value;
}
}

public bool ShowRingRollup
{
get
{
return ViewState["ShowRingRollup"] == null ? false : Convert.ToBoolean(ViewState["ShowRingRollup"]);
}
set
{
ViewState["ShowRingRollup"] = value;
}
}

protected override void OnInit(EventArgs e)
{
base.OnInit(e);

ItemHeaderToolBar.Items.Clear();

if (!IsPostBack)
{
if (ShowCreateRings)
{
EO.Web.ToolBarItem menuItem = new EO.Web.ToolBarItem();
menuItem.Text = "[Create Rings]";
menuItem.ImageUrl = "~/Images/add.gif";
menuItem.CommandName = "CreateRings";

ItemHeaderToolBar.Items.Add(menuItem);
}

if (ShowLikeSku)
{
EO.Web.ToolBarItem menuItem = new EO.Web.ToolBarItem();
menuItem.Text = "[Like Sku]";
menuItem.ImageUrl = "~/Images/add.gif";
menuItem.CommandName = "LikeSku";

ItemHeaderToolBar.Items.Add(menuItem);
}

if (ShowRetrieveItems)
{
EO.Web.ToolBarItem menuItem = new EO.Web.ToolBarItem();
menuItem.Text = "[Retrieve Items]";
menuItem.ImageUrl = "~/Images/add.gif";
menuItem.CommandName = "RetrieveItems";

ItemHeaderToolBar.Items.Add(menuItem);
}

if (ShowDefaultSort)
{
EO.Web.ToolBarItem menuItem = new EO.Web.ToolBarItem();
menuItem.Text = "[Default Sort]";
menuItem.ImageUrl = "~/Images/DefaultSort.gif";
menuItem.CommandName = "DefaultSort";

ItemHeaderToolBar.Items.Add(menuItem);
}
}

if (ShowRingRollup)
{
EO.Web.ToolBarItem menuItem = new EO.Web.ToolBarItem();
menuItem.Type = EO.Web.ToolBarItemType.Custom;
menuItem.CustomItem = new CheckBoxToolBarItemTemplate("Ring Rollup");
//menuItem.CommandName = "RingRollup";

ItemHeaderToolBar.Items.Add(menuItem);
}
}
eo_support
Posted: Tuesday, August 10, 2010 7:42:23 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

There does seem to be some problems with adding toolbar items dynamically. The code that dynamically creates and assign template does work fine. So an easy workaround might be to put the toolbar items there, then either set its Visible to show/hide it, or set its CustomItem to assign a custom item. It will be something like this:

Code: HTML/ASPX
<eo:ToolBar ....>
    <Items>
        <%-- assume the following item is a regular item --%>
        <eo:ToolBarItem ......  />
        <%-- assume the following item is the dynamic item --%>
        <eo:ToolBarItem ......  />
    </Items>
</eo:ToolBar>


In order to show/hide the second toolbar item, you would do:

Code: C#
ToolBar1.Items[1].Visible = true/false;


In order to use a dynamic template, you would use the following code the same way as you were doing:

Code: C#
ToolBar1.Items[1].CustomItem = CheckBoxToolBarItemTemplate();


This avoids dynamically creating toolbar items all together and should resolve issue #1 and issue #2 for you.

To handle event in your dynamically created template you would simply use a delegate:

Code: C#
//Your original code that creates the checkbox
CheckBox cb = new CheckBox();
cb.AutoPostBack = true;
cb.Text = _Label;
cb.TextAlign = TextAlign.Right;
container.Controls.Add(cb);

//New code to add the event handler
cb.CheckedChanged += new EventHandler(cb_CheckedChanged);


Code: C#
//Event handler
void cb_CheckedChanged(object sender, EventArgs e)
{
    //handle your event here...
    .....
}


Note this handler would be on your CheckBoxToolBarItemTemplate instead of the Page class. If you wish to call something on your Page class (almost certainly), you can pass the Page object in and store it as a member in your CheckBoxToolBarItemTemplate class's constructor.

Hope this helps. Let us know how it goes.

Thanks!
Ram Thatikonda
Posted: Tuesday, August 10, 2010 9:33:59 PM
Rank: Member
Groups: Member

Joined: 9/8/2009
Posts: 10
Hi,

I don't get the Visible property on the ToolBarItem.

ItemHeaderToolBar.Items[1].Visible = false;

Are you sure this property is available? This was my original idea but since the property was not available I thought of building the toolbar dynamically.

Thanks
eo_support
Posted: Tuesday, August 10, 2010 10:03:20 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

Please check the version that you are running. The latest version does have this property:

http://doc.essentialobjects.com/library/1/eo.web.toolbaritem.visible.aspx

Thanks!
Ram Thatikonda
Posted: Tuesday, August 10, 2010 10:30:10 PM
Rank: Member
Groups: Member

Joined: 9/8/2009
Posts: 10
I am using EO 2008, assembly version 6.0.49.2.

Can we have your new version and my old version running on the same machine? Where can I get your version for download? I am not sure if we have license for the latest version. Please advise.
eo_support
Posted: Tuesday, August 10, 2010 10:41:45 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

Ah. No. You can not have both versions running on the same machine. Also you will not be able to use 2010 (version 8) without purchasing an upgrade. We currently offers 50% off discount for upgrading from your version (the further behind your version is, the more expensive it is, for example, the discount for version 7 is 70% off). The upgrade will upgrade all your existing controls to the current version and also give you new controls in 2009 (version 7) and 2010 (version 8).

If you can not upgrade, you will just have to always have the items there (since you don't have Visible property), but because you are able to dynamically modify its contents by replacing its CustomItem property, you still should be able to make it "invisible" using an empty custom item template even it's there. You will also want to set your custom item’s ItemStyle and HoverStyle so that they are exactly the same, this way nothing visually changes when you hover over the custom item, which makes them “invisible” to end user.

Thanks!
Ram Thatikonda
Posted: Wednesday, August 11, 2010 9:24:03 AM
Rank: Member
Groups: Member

Joined: 9/8/2009
Posts: 10
We bought our license sometime in 11/2008 with 12 month free upgrades. I believe we are eligible for 2009 version. Could you please provide the link to download the 2009 version?
eo_support
Posted: Wednesday, August 11, 2010 9:32:07 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

Yes. You can use 2009 version. Please see your private message for the download location.

Thanks!
Ram Thatikonda
Posted: Wednesday, August 11, 2010 9:50:16 AM
Rank: Member
Groups: Member

Joined: 9/8/2009
Posts: 10
How can the same be done for default ToolBarItem added in design time?

This would work for ToolBarItem of type Custom.
ToolBar1.Items[1].CustomItem = CheckBoxToolBarItemTemplate();

Thanks!

eo_support wrote:
Hi,

There does seem to be some problems with adding toolbar items dynamically. The code that dynamically creates and assign template does work fine. So an easy workaround might be to put the toolbar items there, then either set its Visible to show/hide it, or set its CustomItem to assign a custom item. It will be something like this:

Code: HTML/ASPX
<eo:ToolBar ....>
    <Items>
        <%-- assume the following item is a regular item --%>
        <eo:ToolBarItem ......  />
        <%-- assume the following item is the dynamic item --%>
        <eo:ToolBarItem ......  />
    </Items>
</eo:ToolBar>


In order to show/hide the second toolbar item, you would do:

Code: C#
ToolBar1.Items[1].Visible = true/false;


In order to use a dynamic template, you would use the following code the same way as you were doing:

Code: C#
ToolBar1.Items[1].CustomItem = CheckBoxToolBarItemTemplate();


This avoids dynamically creating toolbar items all together and should resolve issue #1 and issue #2 for you.

To handle event in your dynamically created template you would simply use a delegate:

Code: C#
//Your original code that creates the checkbox
CheckBox cb = new CheckBox();
cb.AutoPostBack = true;
cb.Text = _Label;
cb.TextAlign = TextAlign.Right;
container.Controls.Add(cb);

//New code to add the event handler
cb.CheckedChanged += new EventHandler(cb_CheckedChanged);


Code: C#
//Event handler
void cb_CheckedChanged(object sender, EventArgs e)
{
    //handle your event here...
    .....
}


Note this handler would be on your CheckBoxToolBarItemTemplate instead of the Page class. If you wish to call something on your Page class (almost certainly), you can pass the Page object in and store it as a member in your CheckBoxToolBarItemTemplate class's constructor.

Hope this helps. Let us know how it goes.

Thanks!
eo_support
Posted: Wednesday, August 11, 2010 9:57:49 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

I am not sure whether I got your question correctly. The previous code is all about items created at design time since dynamically creating items from code is not working properly.

You would just set whatever property you wish to set. For example, if your version has Visible property, you set it's Visible property. If it has a Text property, you set the Text property. There isn't anything particular about setting these properties. The only thing special about CustomItem is how you create your CustomItem, not how you set that property.

Thanks


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.