Welcome Guest Search | Active Topics | Sign In | Register

CustomItem programmatic example Options
Scott L
Posted: Thursday, May 31, 2007 12:05:38 AM
Rank: Member
Groups: Member

Joined: 5/30/2007
Posts: 15
The CustomItem demo (link below) shows how to create a menu in the .aspx/.ascx file. I want to programmatically build the custom items in the code-behind but I can't figure out the syntax. Please post an example of this.

Thanks...

http://www.essentialobjects.com/demo/default.aspx?path=Menu\menu_programming\_i0\binding_template
eo_support
Posted: Thursday, May 31, 2007 6:51:52 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,088
Hi Scott,

Thanks for posting your question here. You will need to do this by handling ItemDataBound event:

http://www.essentialobjects.com/Doc/EO.Web.BaseNavigator.ItemDataBound.html

Inside that event handler, you can then check and modify the CustomItem object provided to you through the event arguments. Note you must first use the syntax demonstrated in the above sample to declare the CustomItem, otherwise CustomItem will be null.

For example, you can use such code to change a Label's text inside the CustomItem:

Code: C#
Label label = (Label)e.CustomItem.FindControl("Label1");
label.Text = "some text";


ItemDataBound event is called every time a new menu item is created during data binding, so you also have access to the data item used to create the current item. The type of the data item depends on your data source. You can find a chart explaining the correlation of the two's type at here:

http://www.essentialobjects.com/Doc/MenuCommon/DataBinding/populate_customitem.html

Once you know your data item's type, you can access it in ItemDataBound event. For example, if your data source is a DataTable, you can do:

Code: C#
DataRowView dataItem = (DataRowView)Menu1.DataItem;
Label label = (Label)e.CustomItem.FindControl("Label1");
label.Text = dataItem["some_field"];


This way you will be able to modify your custom item based on your data source.

Please reply if you still have more questions.

Thanks
Scott L
Posted: Thursday, May 31, 2007 8:56:33 AM
Rank: Member
Groups: Member

Joined: 5/30/2007
Posts: 15
I don't think I've been clear.

I am creating a user control containing a databound menu which will be re-used throughout my application so I don't know what the datafields of a particular instance will be until runtime. Each time I instantiate the control I will be passing in a different datatable and datafields to populate it, so I cannot hard-code the menu in the .ascx file. All I want in my.ascx file is:

<eo:Menu ID="Menu1" runat="server">
</eo:Menu>

Everything else (TopGroup, TemplateItems, CustomItems, etc.) I want to add programmatically in the code-behind file and, once the menu is populated, I will then be ready to handle the CustomItem event arguments as you show.

So what I need is an example of building an entire databound menu in the code-behind file...

Thanks!
eo_support
Posted: Thursday, May 31, 2007 9:16:51 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,088
CustomItem and ItemTemplate make use of ASP.NET templated control architecture and works closely with ASP.NET's page compiler, so the purpose of this control is for declarative definition in your .aspx/.ascx file. Using them in code-behind is possible but that would requires in-depth knowledge of the internal work of ASP.NET page compiler, which can change from version to version so it is something neither you nor we want to count on.

Instead you should consider using simple basic HTML code rather than CustomItem. For example, if you want to have a button and a text in your CustomItem, instead of using CustomItem, you would want to assign some HTML like this to your menu item's Text.Html property:

Code: HTML/ASPX
<input type="button" value="Test Button" /> menu item text


Once you settle that, you can get the rest of the code from this sample:

http://www.essentialobjects.com/demo/default.aspx?path=Menu\menu_programming\_i0\binding_datatable

A number of things that you want to replace in the sample:

1. CreateDataTable(). You will replace it with your code that returns your DataTable object;
2. Menu1.DataFields = "Country|State|City". Obviously you want to replace this as well;
3. Custom DataBinding object. This is optional. The following topic explained DataBinding object extensively:

http://www.essentialobjects.com/doc/MenuCommon/DataBinding/populate_table.html
Scott L
Posted: Thursday, May 31, 2007 11:09:14 AM
Rank: Member
Groups: Member

Joined: 5/30/2007
Posts: 15
My goal is (in certain situations) to get checkboxes into a dynamically-built, databound ContextMenu using .NET server controls rather than HTML inputs. This is not possible? I could create a separate user control for each different menu in my application with its datafields hard-coded in the .ascx page but that is not nearly as elegant as being able to have one dynamic menu user control. Frankly, neither is mixing in HTML inputs.
eo_support
Posted: Thursday, May 31, 2007 11:20:36 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,088
Not if you only want this to appear in your .aspx/.ascx file:

Code: HTML/ASPX
<eo:Menu ID="Menu1" runat="server">
</eo:Menu>


You will have to do what we do in our sample code to delcare the ItemTemplate and CustomItem in your .aspx/.ascx file.

If you want to construct everything by yourself, it would be much easier for you to go to HTML input route. The simple reason behind this is, you can't easily construct a templated control with code (A TemplateBuilder object is required during the process and it is generated by ASP.NET page compiler), but you can easily construct any HTML with code.



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.