【问题标题】:How to refresh a DataBind() after Postback回发后如何刷新 DataBind()
【发布时间】:2013-08-23 07:47:35
【问题描述】:

我正在开发一个向用户显示新闻的 asp.net 网站。新闻通过用户控件中的中继器显示在网站上。现在我正在尝试使用插入到中继器的 ItemTemplate 中的按钮删除这些新闻(因此按钮是动态创建的):

    <asp:Button ID="DelBtn" runat="server" OnClientClick="if(!confirm('Really want to delete?')){return false;}" OnClick="cmdDeleteNews_Click" CommandArgument='<%# Eval("NewsID") %>'  />

从数据库中删除不是问题。但是之后我如何使用户控件刷新,所以被删除的新闻不再显示在网站上?简单地使用 Java 脚本删除元素是可能的,但由于会有很多用户在网站上更新、插入和删除新闻,死锁很容易发生。 Reloading the Page 使用 window.location.reload() 也是可能的,但实际上没有意义。因此,我正在寻找一种解决方案来更新 PostBack 上新闻转发器的数据绑定。但是由于 DataBind()-Method 不能用于 Postback,我希望你有一个类似的解决方案来解决这个问题,或者可能是一个解决方法。 新闻中继器的 DataBind() 是在 Usercontrol 的 Codebehind 中完成的

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ' Databind on PostBack is not permitted (InvalidPostbackException)
       If Not IsPostBack Then
          NewsRepeater.DataSource = Items
          NewsRepeater.DataBind()
          ContentRepeater.DataSource = Items
          ContentRepeater.DataBind()
       End If
    End Sub

【问题讨论】:

    标签: asp.net data-binding


    【解决方案1】:

    cmdDeleteNews_Click 中重新加载数据源。然后DataBind 数据绑定控件。

    Protected Sub cmdDeleteNews_Click(sender As Object, EventArgs As e)
      ' Delete logic here .... '
    
      ' Then reload and DataBind all:
      Dim items = GetItems()
      NewsRepeater.DataSource = items 
      NewsRepeater.DataBind()
      ContentRepeater.DataSource = Items
      ContentRepeater.DataBind()
    End Sub
    

    【讨论】:

    • 非常感谢...你真的帮了我大忙。没想到解决办法这么简单。
    • @TimSchmelter - 但 cappi 的问题听起来更像是为其他用户刷新数据(他的问题中的一句话 - 但因为会有很多用户
    猜你喜欢
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多