简体   繁体   中英

The Value Of A Check Box Is Empty Using An Enumeration Inside A Repeater (ASP.NET Web Forms)

I have a repeater and I set the value of an html check box control with the value of an enumeration instead of hard-coding a magic number. When I try to access the html check box control in the repeater's ItemCreated event handler, the value is an empty string. Why is this and how can I fix it?

C# Code

protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
{
    var myObject = e.Item.DataItem as MyObject;
    if (myObject != null)
    {
        var checkBox = e.Item.FindControl("checkbox1") as HtmlInputCheckBox         

        // The value is empty!
        var value = checkBox.Value;
    }
}

Not Working

<asp:Repeater ID="Repeater1" OnItemCreated="Repeater1_ItemCreated" runat="server">
    <ItemTemplate>      
        <input type="checkbox" id="checkbox1" value='<%# SomeEnum.Value %>' />
    </ItemTemplate>
</asp:Repeater>

Working

<asp:Repeater ID="Repeater1" OnItemCreated="Repeater1_ItemCreated" runat="server">
    <ItemTemplate>      
        <input type="checkbox" id="checkbox1" value="1" />
    </ItemTemplate>
</asp:Repeater>

ItemCreated is triggered before ItemDataBound and also on every postback to recreate he controls even when the Repater is not databound again. So i would not use ItemCreated if you need to access the DataSource of any databound WebControl like Repeater .

Apart from that, make the checkbox runat=server (or use a ASP.NET CheckBox ) if you want to find it on the server.

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