简体   繁体   中英

Retrieving dynamic asp.net checkbox controls - controls are added to page in repeater, but can't retrieve in postback (C#)

I have a set of checkboxes on my page that are generated using a repeater. The checkboxes are for each database in my web config, and the idea is to use them to select which databases to search. I have them generated by a repeater because I want my code to be dynamic and work in any project- therefore, database names cannot be hardcoded, and I do not have hardcoded IDs.

    protected void rptDatabases_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        var databaseName = e.Item.DataItem as string;
        CheckBox checkbox = e.Item.FindControl("checkboxDatabase") as CheckBox;
        if (checkbox != null)
        {
            checkbox.Attributes["data-id"] = databaseName;
            checkbox.Text = databaseName;
        }
    }

<div class="databases">
    <asp:Repeater runat="server" ID="rptDatabases" OnItemDataBound="rptDatabases_OnItemDataBound">
        <ItemTemplate>
            <asp:CheckBox runat="server" ID="checkboxDatabase"/>
        </ItemTemplate>
    </asp:Repeater>

However, I can't figure out how to retrieve the data I need from them in the postback. First I tried this:

 public List<string> GetSelectedDatabases()
{
    var selectedDatabases = new List<string>();
    foreach (var ctrl in form1.Controls)
    {
        var checkbox = ctrl as CheckBox;
        if (checkbox != null)
        {
            if (checkbox.Checked)
            {
                selectedDatabases.Add(checkbox.Text);
            }
        }
    }
    return selectedDatabases;
} 

but this didn't find any checkboxes, and when I stepped through, I found that it wasn't retrieving any checkbox controls at all. So I changed it from form1.Controls to rptDatabases.Controls to get the controls within the repeater. However, although this retrieves all of the checkbox controls, it is returning them as RepeaterItem objects. On this line:

var checkbox = ctrl as CheckBox;

checkbox ends up being null. I can't cast the RepeaterItem as a CheckBox, and I can't access the Text property, which I need in order to figure out which value to add.

I also tried (var ctrl in rptDatabases.Controls.OfType<CheckBox>()) but that returns no items.

With foreach (var ctrl in form1.Controls) you are getting all the controls on the page, but those checkboxes are in another control so that won't work.

You need to loop the RepeaterItem items in the Repeater and locate the checkbox within that item.

foreach (RepeaterItem item in rptDatabases.Items)
{
    CheckBox checkbox = item.FindControl("checkboxDatabase") as CheckBox;

    if (checkbox != null && checkbox.Checked)
    {
        selectedDatabases.Add(checkbox.Text);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM