Question

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)?

Was it helpful?

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

OTHER TIPS

I have quite dirty solution. My problem was to fire a postback of checkbox in a repeater but i also need the ID of the record. The best way to archive the ID is using ValidationGroup attribute:

<asp:CheckBox ValidationGroup='<%# DataBinder.Eval(Container.DataItem,"id") %>' ID="YOUR_ID" runat="server" AutoPostBack="true" OnCheckedChanged="checkbox_Changed" />

and in code behind:

CheckBox theCheckBox = (CheckBox)sender; 
int id = Convert.ToInt16(theCheckBox.ValidationGroup);

if someone has the same needs can use this solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top