Menol
Menol

Reputation: 1348

Set UserControl Property value From ASP.NET markup

I need to set a property value of a user control from the markup.

I need to set the property called "ItemIndex" in my user control from the markup.

for some unfortunate reason, "<%: x %>" part of ItemIndex="<%: x %>" doesn't get resolved.

Basically the value of ItemIndex becomes "<%: x %>" rather than becoming the actual value of x.

Below is the code (please note the comments in CAPS).

<%@ Register TagPrefix="DDLControls" TagName="MainMenuItem" Src="~/Views/Header/MainMenuItemControl.ascx" %>
<div id="MainMenu">
    <table cellpadding="0" cellspacing="0" border="0">
    <tr>
        <%   
            foreach (MenuItem mi in Model.Items)
            {
                string x = Model.Items.IndexOf(mi).ToString();
        %>
        <td>
            <%= x %>  <<-- THIS GETS RESOLVED TO 0,1,2,3,4,...
            <DDLControls:MainMenuItem ItemIndex="<% x %>" runat="server" />  <<-- THIS DOESN'T GET RESOLVED
        </td>
        <%

            } 
        %>
    </tr>
</table>
</div>

Upvotes: 2

Views: 2402

Answers (1)

James Johnson
James Johnson

Reputation: 46047

You need to add the Bindable attribute to your property:

[System.ComponentModel.Bindable(true)]
public string SomeValue 
{
    get
    {
        return someValue;
    }
    set
    {
        someValue = value;
    }
}

Upvotes: 2

Related Questions