在asp.net中以编程方式添加转发器的项目

时间:2013-01-03 11:26:13

标签: c# asp.net repeater

我知道Repeater Control用于在网页上动态显示来自data source的数据。
但是,我想使用转发器控件从用户获取输入并根据用户请求生成新的输入字段。谁能帮助我如何通过代码后面添加一个新的转发器项目 我有一个以下中继器:

<asp:Repeater ID="RepeaterDetailsRow" runat="server">
          <HeaderTemplate>
            <div class="divSection">
            <div class="divFieldContent" style="width:auto;">
                <asp:CheckBox ID="CheckBoxDetails" runat="server" AutoPostBack="True" 
                    oncheckedchanged="CheckBoxDetails_CheckedChanged" />
            </div>
            <div class="divFieldContent">
                <asp:Label ID="lblDetails" runat="server" Text="Enter Details" 
                    ForeColor="Coral" Font-Bold="True" Enabled="False"></asp:Label>
            </div>
              <asp:Button ID="AddNewRow" runat="server" Text="Button" />
            </div> 
          </HeaderTemplate>
          <ItemTemplate>
            <div class="divSectionContent">
            <div class="divFieldContent">
                <asp:Label ID="lblName" runat="server" 
                    Text="Name"></asp:Label>
            </div>
            <div class="divFieldContent">
              <div>
                <asp:TextBox ID="txtName" CssClass="boxes"runat="server">                    </asp:TextBox>               
              </div>
              </div>                    
          </div>              
            <div class="divSectionContent">
            <div class="divFieldContent">
                <asp:Label ID="lblSubject" runat="server" 
                    Text="Subject" Enabled="False"></asp:Label>
            </div>
            <div class="divFieldContent">
                <div>
                  <asp:DropDownList ID="ddl_RejectReasonCode" CssClass="boxes" 
                   runat="server">
                  <asp:ListItem>Select Subject</asp:ListItem>
            <asp:ListItem>Subject1</asp:ListItem>
            <asp:ListItem>Subject2</asp:ListItem>
            <asp:ListItem>Subject3</asp:ListItem>      
                  </asp:DropDownList>
                </div>
                                </div>
          </div>
          </ItemTemplate>
        </asp:Repeater>   

我想在转发器的标头模板中的按钮的Item命令属性中添加与repeater的item模板对应的行。

4 个答案:

答案 0 :(得分:2)

在ASPX页面

你应该包括

Repeater标签中的

OnItemCommand =“RepeaterDetailsRow_ItemCommand” 看起来像

<asp:Repeater ID="RepeaterDetailsRow" runat="server" OnItemCommand="RepeaterDetailsRow_ItemCommand">

和一个CommandName到按钮AddNewRow

<asp:Button ID="AddNewRow" runat="server" Text="Button" CommandName="Add"/>

在CodeBehind

protected void RepeaterDetailsRow_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "Add")
    {
        //save the data to the database 
        LoadData(); //again rebind the repeater with data from db
    }    
}

答案 1 :(得分:0)

你必须重新绑定列表并添加一个空项。这是数据绑定控件完成的常用方法。您无法将项目动态添加到我所知道的转发器中。

答案 2 :(得分:0)

我的要求是在转发器中显示添加行。我通过做一个小的检查包括一个空白行作为最后一项,在所有其他行中,空白行被隐藏。

用于  <%# (((IList)((Repeater)Container.Parent).DataSource).Count).ToString() == (Container.ItemIndex + 1).ToString() %> 检查以决定是否显示或隐藏空白行。

完整的观点代码:

<table>
    <asp:Repeater ID="repeater1" OnItemCommand="repeater_user_Itemcommand" runat="server">
        <HeaderTemplate>
            <tr>
                <td>
                    Name
                </td>
                <td>
                    Email
                </td>
                <td>
                    Delete
                </td>
            </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <asp:Label ID="lblName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Name") %>'></asp:Label>
                </td>
                <td>
                    <asp:Label ID="lblEmail" runat="server" Text='<%# Eval("Email") %>'></asp:Label>
                </td>
                <td>
                    <asp:LinkButton ID="btnDelete" runat="server" CommandArgument='<%# Eval("ID") %>'
                        CommandName="delete">Delete</asp:LinkButton>
                </td>
            </tr>
            <tr id="Tr1" runat="server" visible='<%# (((IList)((Repeater)Container.Parent).DataSource).Count).ToString() == (Container.ItemIndex + 1).ToString() %>'>
                <td>
                    <asp:TextBox ID="txtName_add" runat="server" Enabled="True" Text='' Visible="false"></asp:TextBox>
                </td>
                <td>
                    <asp:TextBox ID="txtEmail_add" runat="server" Text='' Visible="false"></asp:TextBox>
                </td>
                <td>
                    <asp:LinkButton ID="btnShowAdd" runat="server" CommandName="add">Add</asp:LinkButton>
                </td>
            </tr>
        </ItemTemplate>
    </asp:Repeater>
</table>

答案 3 :(得分:0)

Deaths=Deaths.merge(df1,left_on='States',right_on=df1.index)

添加Onclick事件

you could try with this:

    Protected Sub btn_Click(sender As Object, e As EventArgs)
        Dim r As Integer = TablaRepeater.Rows.Count
        Dim t As Integer = r + 1
        TablaRepeaterCF.Rows.Add(t)
        Repeater.DataSource = TablaRepeater
        Repeater.DataBind()
    End Sub