nishantv
nishantv

Reputation: 781

Adding an item of a repeater programatically in asp.net

I know that Repeater Control is used to show data from a data source dynamically on a web page.
However I want to use repeater control to take input from the user and generate new input fields on user request. Could anyone help me how to add a new item of repeater through code behind.
I have a following repeater:

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

I want to add rows corresponding to item template of repeater on the Item command property of button in the header template of repeater.

Upvotes: 1

Views: 21571

Answers (4)

Gabriela Rojas
Gabriela Rojas

Reputation: 21

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

add the Onclick event

<asp:Button runat="server" ID="btn" Text="+" CssClass="btn btn-primary active" OnClick="btn_Click" />

Upvotes: 0

Anjith K P
Anjith K P

Reputation: 2158

My requirement was to show add row inside repeater. Me included a blank row as the last item by doing a small checking, in all other rows, blank row made hidden.

Used <%# (((IList)((Repeater)Container.Parent).DataSource).Count).ToString() == (Container.ItemIndex + 1).ToString() %> checking to decide whether to show or hide blank row.

Full code of view:

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

Upvotes: 0

ani07
ani07

Reputation: 158

In ASPX page

you should include

OnItemCommand="RepeaterDetailsRow_ItemCommand" in the Repeater Tag which would look like

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

and a CommandName to the button AddNewRow

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

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

Upvotes: 2

Brian Mains
Brian Mains

Reputation: 50728

You'd essentially have to rebind the list and add in an empty item. That is the common way things are done with data-bound controls. You can't dynamically add an item to the repeater that I am aware of.

Upvotes: 0

Related Questions