0

I have a repeater that populates a list of 3 columns and has a Checkbox next to each row. I am trying to create a scenario in which a person checks a row, the page locates the "Portion Name" text box inside of the repeaters row that corresponds with the row where the checkbox has been clicked, and once that checkbox is selected, it sends the Portion Name to another textbox outside the repeater called "testTextBox.Text". I have my code below, and am sure I am missing something as I have not done a "OnCheckChanged" event before, I am only familiar with onTextChanged events.

Below is the code:

<asp:Repeater ID="rptAccount" runat="server" OnItemCommand="rptAccount_ItemCommand">
             <HeaderTemplate>
                            <table>
                                <tr>
                                    <th>Account
                                    </th>
                                    <th>Portion ID
                                    </th>
                                    <th>Portion Name
                                    </th>                                                                                                
                                </tr>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <tr>
                                <td>
                                   <asp:TextBox ID="Account"  runat="server" Width ="50px" Text='<%#Eval("Account") %>' ></asp:TextBox>
                                </td>
                                <td>
                                   <asp:TextBox ID="PortionID"  runat="server" Width ="90px" Text='<%#Eval("Portion ID") %>' ></asp:TextBox>
                                </td>
                                <td>
                                    <asp:TextBox ID="PortionName" runat="server" Width ="340px" Text='<%#Eval("Portion Name") %>'></asp:TextBox>
                                </td>    
                                <td>
                                    <asp:CheckBox ID="Name" runat="server" OnCheckedChanged = "TbName_CheckedChanged" Checked='<%# Eval("Name").ToString() == "True" %>' ></asp:CheckBox>
                                    </td>                                                                            
                               </tr>
                        </ItemTemplate>
                        <FooterTemplate>
                            </table>
                        </FooterTemplate>     
        </asp:Repeater>

cs code:

protected void TbName_CheckedChanged(object sender, EventArgs e)
    {
        var PortionName = (sender as TextBox).Parent;
        var rptAccount = (sender as TextBox).Parent;
        var checkedd = rptAccount.FindControl("Name") as CheckBox;
        var PortionNamee = rptAccount.FindControl("PortionName") as TextBox;


        if (checkedd.Checked)
        {
            testTextBox.Text = PortionNamee.Text;
            }

    }

Thank you for any help you can offer.

  • You should use the rptAccount_ItemCommand – hardkoded Sep 29 '17 at 18:23
  • where would that cmd be inserted it on the aspx page? still under the OnCheckChanged event? – Michael Felchlin Sep 29 '17 at 18:25
  • Possible duplicate of [Check box inside repeater , How to get command name value in the check changed function](https://stackoverflow.com/questions/18439197/check-box-inside-repeater-how-to-get-command-name-value-in-the-check-changed-f) – hardkoded Sep 29 '17 at 18:52

1 Answers1

1

The repeater ends up having a ton of controls with the name Name. Remember the template is repeated many times. you need to find the index of the current item. An easier way its to attach an attribute to the checkbox to hold the text value, that way you can extract it straight from the sender without having to worry about parent and index. Try this:

<asp:Repeater ID="rptAccount" runat="server" OnItemCommand="rptAccount_ItemCommand">
             <HeaderTemplate>
                            <table>
                                <tr>
                                    <th>Account
                                    </th>
                                    <th>Portion ID
                                    </th>
                                    <th>Portion Name
                                    </th>                                                                                                
                                </tr>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <tr>
                                <td>
                                   <asp:TextBox ID="Account"  runat="server" Width ="50px" Text='<%#Eval("Account") %>' ></asp:TextBox>
                                </td>
                                <td>
                                   <asp:TextBox ID="PortionID"  runat="server" Width ="90px" Text='<%#Eval("Portion ID") %>' ></asp:TextBox>
                                </td>
                                <td>
                                    <asp:TextBox ID="PortionName" runat="server" Width ="340px" Text='<%#Eval("Portion Name") %>'></asp:TextBox>
                                </td>    
                                <td>
                                    <asp:CheckBox ID="Name" runat="server" OnCheckedChanged = "TbName_CheckedChanged" CommandName='<%#Eval("Portion Name") %>' Checked='<%# Eval("Name").ToString() == "True" %>' ></asp:CheckBox>
                                    </td>                                                                            
                               </tr>
                        </ItemTemplate>
                        <FooterTemplate>
                            </table>
                        </FooterTemplate>     
        </asp:Repeater>

cs code:

protected void TbName_CheckedChanged(object sender, EventArgs e)
    {   
        var checkedd = sender as Checkbox;     

        if (checkedd.Checked)
            testTextBox.Text = checkedd.Attributes["CommandName"];
    }
DonO
  • 1,030
  • 1
  • 13
  • 27