如果在转发器控制器中选中复选框,如何获取隐藏字段值?

时间:2013-02-27 19:59:25

标签: c# asp.net

我有asp.net转发器控制器,我试图在CheckBox1_CheckedChanged

时获取隐藏字段的值
<ul id="sortable">
    <asp:Repeater runat="server" ID="rep_sortable">
        <ItemTemplate>
            <li class="ui-state-default">
               <span class="ui-icon ui-icon-arrowthick-2-n-s"></span>
               <%# DataBinder.Eval(Container.DataItem, "Body") %> 
               <%# DataBinder.Eval(Container.DataItem, "Completed") %> 
               <asp:CheckBox ID="chb_com"  runat="server" AutoPostBack="True" Checked='<%# DataBinder.Eval(Container.DataItem, "Completed")  %>' oncheckedchanged="CheckBox1_CheckedChanged"/>
               <asp:HiddenField ID="hf_reminderID" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "ReminderID")  %>'/>
            </li>
        </ItemTemplate>
    </asp:Repeater>
</ul>

C#

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
   foreach()
}

2 个答案:

答案 0 :(得分:0)

您可以使用NamingContainer查找同级控件。

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    var checkBox = (CheckBox) sender;
    var reminderHiddenField = (HiddenField)checkBox.NamingContainer
        .FindControl("hf_reminderID");
}

答案 1 :(得分:0)

此代码应该有效:

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            var checkBox = sender as CheckBox;
            var hiddenField = checkBox.Parent.FindControl("hf_reminderID") as HiddenField;
            var value = hiddenField.Value;
        }