asp.netcheckboxrepeatersitefinitysitefinity-5

ASP.NET checkbox value when postback inside of an repeater


I have a ASP Repeater which contains a list of tags and I'd like to see which one of the tag was selected (checked).

Like:

<HeaderTemplate>
        <ul class="tags-list">
    </HeaderTemplate>
    <ItemTemplate>
        <asp:CheckBox ID="tag" runat="server" AutoPostBack="true" Text='' />
    </ItemTemplate>
    <FooterTemplate>
        </ul>
    </FooterTemplate>
</asp:Repeater>

The problem that I face is that

protected override void InitializeControls(Telerik.Sitefinity.Web.UI.GenericContainer container, Telerik.Sitefinity.Web.UI.ContentUI.Contracts.IContentViewDefinition definition)

method is fired before and:

    void tagList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            var item = e.Item.DataItem as Taxon;
            var checkbox = e.Item.FindControl("tag") as CheckBox;
            checkbox.Text = item.Title;
            checkbox.CheckedChanged += new EventHandler(this.checkbox_Changed);

        }
    }

gets fired everytime before my callback method:

protected void checkbox_Changed(object sender, EventArgs e)
{
    CheckBox tagCheckbox = (CheckBox)sender;

    if (tagCheckbox.Checked)
    {

    }
}

Can anyone guide me what would be the best practice to get the state of the checkbox(es)?


Solution

  • could you try defining the "CheckedChanged" method inline with the checkbox control inside the repeater:

    <asp:CheckBox ID="tag" runat="server" AutoPostBack="true" Text='' CheckedChanged="checkbox_Changed" />
    

    Then you don't have to worry about binding the handler it in code behind. If you're not getting the correct state of the checkbox, also make sure that you have "EnableViewState" on the page properties.

    I hope this is helpful, let me know if I've misunderstood the question