简体   繁体   English

使用转发器内部的枚举,复选框的值是空的(ASP.NET Web窗体)

[英]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. 我有一个中继器,我用枚举的值设置了html复选框控件的值,而不是硬编码一个幻数。 When I try to access the html check box control in the repeater's ItemCreated event handler, the value is an empty string. 当我尝试访问转发器的ItemCreated事件处理程序中的html复选框控件时,该值为空字符串。 Why is this and how can I fix it? 为什么会这样,我该如何解决?

C# Code C#代码

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. 即使在不再重新绑定Repater的情况下,也会在ItemDataBound之前以及每个回ItemCreated中触发ItemCreated来重新创建他控制的内容。 So i would not use ItemCreated if you need to access the DataSource of any databound WebControl like Repeater . 因此,如果您需要访问任何数据绑定WebControlDataSource (如Repeater我将不使用ItemCreated。

Apart from that, make the checkbox runat=server (or use a ASP.NET CheckBox ) if you want to find it on the server. 除此之外,如果要在服务器上find它,请选中复选框runat=server (或使用ASP.NET CheckBox )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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