ASP.NET - 在运行时向转发器添加行
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>
此代码在我单击添加
时添加行,
但是,它会删除所有更改。
如何在不删除更改的情况下向转发器添加行?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了解决这个问题,我使用 Reflector 来确认
Repeater
在处理添加行本身时所做的事情,创建一个继承自Repeater
的ExtensibleRepeater
并允许为每次更新添加一行。它唯一的问题是您无法使用页脚概念,因为更新时页脚后面会出现添加的行。通过将相关
RepeaterItem
的Visible
设置为False
来处理删除行。IIRC 您在回调时丢失数据的原因是因为您甚至在回调时调用
DataBind
,这会重置所有现有数据,包括用户表单数据。该代码是专有的,但由于它基于 Reflector 对 .NET 2.0
Repeater
的显示,我可以向您展示编辑后的版本: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: