Public Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack = False Then
Dim ds1 As New List(Of Link)
For i = 1 To x
Dim h As New Link
ds1.Add(h)
h.Text = "ssss"
h.URL = "yyyyy"
Next
rptMenu.DataSource = (ds1)
Else
rptMenu.DataSource = ViewState("ds1")
End If
rptMenu.DataBind()
End Sub
Protected Sub addFeild()
Dim ds1 As List(Of Link) = rptMenu.DataSource
Dim h As New Link
ds1.Add(H)
rptMenu.DataBind()
End Sub
Private Sub Menu_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
ViewState("ds1") = rptMenu.DataSource
End Sub
End Class
<asp:Repeater ID="rptMenu" runat="server">
<ItemTemplate>
<tr>
<td><asp:TextBox CssClass="txtBox" ID="txtText" runat="server" Text='<%# Eval("Text") %>'></asp:TextBox></td>
<td><asp:TextBox CssClass="txtBox" ID="txtLocation" runat="server" Text='<%# Eval("URL") %>'></asp:TextBox></td>
<td><asp:Button ID="btnDelete" runat="server" Text="Delete" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr><td colspan="3">
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="addFeild" />
</td></tr></table>
</FooterTemplate>
</asp:Repeater>
This code adds row whin i click Add
However, it removes all changes.
How can I add rows to repeater without removing changes?
To solve this issue I used Reflector to confirm what was done by the
Repeater
when it handles adding rows itself, creating anExtensibleRepeater
that inherits fromRepeater
and allows one row to be added for each update. Its only issue is you can't use the footer concept because an added row appears after the footer when it is updated.Deleting a row is handled by setting the relevant
RepeaterItem
'sVisible
toFalse
.IIRC the reason why you're losing data on callback is because you're calling
DataBind
even on callback, which resets all existing data, including user form data.The code is proprietary, but as it is based upon Reflector's display of the .NET 2.0
Repeater
, I can show you an edited version: