开发者

Accesing a CheckBox that's inside a Repeater

开发者 https://www.devze.com 2023-03-25 06:09 出处:网络
In my repeater\'s ItemTemplate I\'ve a CheckBox and a disabled TextBox, I need to implement this idea: TextBox only gets enabled if the the CheckBox is checked .. so I set the CheckBox AutoPostBack to

In my repeater's ItemTemplate I've a CheckBox and a disabled TextBox, I need to implement this idea: TextBox only gets enabled if the the CheckBox is checked .. so I set the CheckBox AutoPostBack to true and I tried to put this code in ItemDataBound. but I can't find my control which is weird because I use the same code but in loop "MyRptr.Item[i].FindControl...." and it works! .. I don't want to loop through all the Items, I just wish If I can know the Item number or location in which the CheckBox was created. and I've also tried to create an event handles for the CheckBox's CheckedChanged event but I can't find the CheckBox either!

protected void MyRptr_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            CheckBox ChkBx = e.Item.FindControl("IsSelected_ChkBx") as CheckBox;
            if (ChkBx.Checked == true)
            {
                TextBox TxtBx = e.Item.FindControl("Value_TxtBx") as TextBox;
                TxtBx.Enabled = true;
            }
        }


<asp:Repeater ID="MyRptr" runat="server" 
            onitemdatabound="MyRptr_ItemDataBound">
                <ItemTemplate>
                    <asp:CheckBox ID="IsSelected_ChkBx" runat="server" Text='<%# Eval("Item") %>' AutoPostBack="True" OnCheckedChanged="IsSelected_ChkBx_CheckedChanged" />
                    &nbsp;<asp:TextBox ID="Value_TxtBx" runat="server" Enabled="false"></asp:TextBox>
                    <asp:HiddenField ID="ID_HdnFld" runat="server" Value='<%# Eval("ID") %>' />
                </ItemTemplate>
                <SeparatorTemplate>
                    <br></br>
                </SeparatorTemplate>
            </asp:Repeater>

So basic开发者_高级运维ally I need a clean and simple way to implement my logic and If I could get an explanation for what's happening it would be great, so any ideas =) ?


You can find your textbox as follow, but I think its better use the jQuery instead of server-side event

protected void IsSelected_ChkBx_CheckedChanged(object sender, EventArgs e)
{
     var ch = (CheckBox)sender;
     var txt = ch.Parent.FindControl("Value_TxtBx") as TextBox;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消