【问题标题】:Have Checkbox inside repeater send data from Textbox inside repeater to another Textbox outside of repeater让中继器内的复选框将数据从中继器内的文本框发送到中继器外的另一个文本框
【发布时间】:2017-09-29 18:04:53
【问题描述】:

我有一个中继器,它填充 3 列的列表,并且每行旁边都有一个复选框。我正在尝试创建一个场景,其中一个人检查一行,页面在中继器行内找到与单击复选框的行相对应的“部分名称”文本框,一旦选中该复选框,它将“部分名称”发送到中继器外部名为“testTextBox.Text”的另一个文本框。我在下面有我的代码,并且我确信我错过了一些东西,因为我之前没有做过“OnCheckChanged”事件,我只熟悉 onTextChanged 事件。

下面是代码:

<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代码:

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;
            }

    }

感谢您提供的任何帮助。

【问题讨论】:

标签: c# asp.net repeater


【解决方案1】:

中继器最终拥有大量名称为 Name 的控件。记住模板重复了很多次。您需要找到当前项目的索引。一种更简单的方法是将属性附加到复选框以保存文本值,这样您就可以直接从发件人那里提取它,而不必担心父级和索引。试试这个:

<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代码:

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

        if (checkedd.Checked)
            testTextBox.Text = checkedd.Attributes["CommandName"];
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    相关资源
    最近更新 更多