Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

In the default.aspx page

1. Added the Add more button

<asp:ImageButton ID="ImgAddItem" runat="server" Height="30px" 
                     ImageUrl="~/Images/AddItem.png" onclick="ImgAddItem_Click" Width="30px" 
                     EnableViewState="False" />

2.Added the repeater control which contains three controls (dropdown and 2 textboxes )
<asp:repeater id="Repeater1" runat="server" >
        <itemtemplate>    
        <tr>
        <td style="padding-removed10px; width:130px;">
            <asp:Label ID="lblitems" runat="server" Font-Names="Verdana" Font-Size="Small" 
                Text="Items" Width="100px">
        </td>
        <td>
            <asp:DropDownList ID="ddlitems" runat="server" AppendDataBoundItems="True" 
                AutoPostBack="True" CssClass="ddl" Width="145px" DataTextField="ItemsName" 
                DataValueField="ItemsId" DataSourceID="SDSItems">
                <asp:ListItem Selected="True" Value="0">--Select--
            
            <asp:RequiredFieldValidator ID="RFV_cash_sfirm1" runat="server" 
                ControlToValidate="ddlitems" Display="Dynamic" 
                ErrorMessage="Please Select Firm" InitialValue="0" SetFocusOnError="True" 
                ValidationGroup="Submit">*
        </td>
        <td style="padding-removed10px; width:130px;">
            <asp:Label ID="lblQuantity" runat="server" Font-Names="Verdana" 
                Font-Size="Small" Text="Quantity" Width="100px">
        </td>
        <td>
            <asp:TextBox ID="txtQuantity" runat="server" AutoPostBack="True" 
                ontextchanged="txtQuantity_TextChanged" Width="140px">
           
        </td>
        <td style="padding-removed10px; width:130px;">
            <asp:Label ID="lblprice" runat="server" Font-Names="Verdana" Font-Size="Small" 
                Text="Price" Width="100px">
        </td>
        <td>
            <asp:TextBox ID="txtprice" runat="server" style="text-align:right" 
                Width="140px">           
        </td>
        <td>
            <asp:ImageButton ID="ImgRemoveItem" runat="server" 
                    ImageUrl="~/Images/cancel.gif" onclick="ImgRemoveItem_Click" Visible="True" />
            
        </td>
    </tr>
       </itemtemplate>

3.When we click on the add button new row will be added ..by using the following code

protected void ImgAddItem_Click(object sender, ImageClickEventArgs e)
    {
        List<int32> _tmpList = ControlsList;
        _tmpList.Add(_tmpList.Count);
        ViewState["ControlsList"] = _tmpList;
        BindTemplate();
    }
private void BindTemplate()
    {
        Repeater1.DataSource = ControlsList;
        Repeater1.DataBind();
    }

private List<int32> ControlsList
    {
        get
        {
            if (ViewState["ControlsList"] != null)
            {
                return ViewState["ControlsList"] as List<int32>;
            }
            else
            {
                List<int32> _tmpList = new List<int32>();
                _tmpList.Add(_tmpList.Count);
                ViewState["ControlsList"] = _tmpList;
                return _tmpList;
            }
        }
        set
        {
            ViewState["ControlsList"] = value;
        }
    }

protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!Page.IsPostBack)
        {
            BindTemplate();
          
        }      
    }

Its working Fine..But the problem is,

When we entered the data in First Row and clicked on the add more button,The new row is creating successfully at the same time ,we are losing the data from the controls of first row which i have entered before..


Pls help me..Thanks in Advance
Posted
Updated 16-Apr-12 1:44am
v2
Comments
sravani.v 16-Apr-12 7:44am    
Added <pre> tags

1 solution

add update panel and triger on design page

exa-
<asp:
XML
UpdatePanel runat="server" ID="PurchaseUpdate" UpdateMode="Conditional" ChildrenAsTriggers="true">
 <Triggers > </Triggers>
<ContentTemplate

>
and close the tag after end withen content place holder
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900