【问题标题】:ASP.NET repeater not updating when dataset is updated更新数据集时 ASP.NET 转发器未更新
【发布时间】:2013-05-20 14:09:56
【问题描述】:

我意识到这是一个愚蠢的问题,但仍然如此。

我有一个页面,其中的DataSet 填充了数据。 所有数据集都绑定到Repeater。在该中继器内,我有一个 ImageButton,它有一个 onCommand 事件,负责从数据库中删除所选项目。 这确实意味着发生了PostBack 对吗?所以Page_PreRender 被解雇了,还是我错了?

我想要实现的是,当回发发生并进行更改时,我希望转发器获得最新信息。我检查是否在页面的Load 事件中进行了更改,并在PreRendering 开始之前获取新数据,但在回发发生后,我仍然在屏幕上显示相同的信息。

Page_Load 事件发生在 Page_PreRender 事件之前,这意味着转发器是在收集到新数据之后绑定的,对吧?

谁能帮助或解释我的逻辑哪里错了?

这里是Page_PreRender 事件

/// <summary>
/// Making sure that every repeater rebinds their datasets on every postback.
/// If not, then the onCommand events of the buttons will break.
/// </summary>
/// <param name="sender">The page</param>
/// <param name="e"></param>
protected void Page_PreRender(object sender, EventArgs e)
{
    if (Page.IsPostBack) //These repeaters are only visible after going to step 2 in the wizard
                         //(thus after at least one postback)
    {
        rptDeleteGroups.DataSource = dsGroupsPerFestival;
        rptDeleteGroups.DataBind();

        rptDeleteCampSites.DataSource = dsCampSitesPerFestival;
        rptDeleteCampSites.DataBind();

        rptDeleteTickets.DataSource = dsTicketsPerFestival;
        rptDeleteTickets.DataBind();
    }
    rptBandOverview.DataSource = dsGroupsPerFestival;
    rptBandOverview.DataBind();

    rptCampSiteOverview.DataSource = dsCampSitesPerFestival;
    rptCampSiteOverview.DataBind();

    rptTicketOverview.DataSource = dsTicketsPerFestival;
    rptTicketOverview.DataBind();
}

这是Page_OnLoad 函数

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        strFormIdFest = Request["hidden_fest_id"]; //Getting the submitted festival id
        if (strFormIdFest == null) //if this string doesn't exist, then user was redirected from group details page
        {
            strFormIdFest = Session["fest_id"].ToString(); //Then get the id from the predefined session variable.
            Session.Remove("fest_id"); //Remove this variable from the session
        }
        Session["festId"] = strFormIdFest; //Placing the festival id in a sessionVariable

        overViewEditing.Visible = false;
    }
    else
    {
        strFormIdFest = Session["festId"].ToString();
    }

    if (Session["EditsMade"] == null || (Boolean)Session["EditsMade"])
    {
        Session["EditsMade"] = false;
        getDataFromDatabase();
    }
    else
    {
        getDataFromSession();
    }
}

getDataFromDatabasegetDataFromSession 函数:

/// <summary>
/// Fills all the datasets required for this page to run properly with data from the database
/// </summary>
protected void getDataFromSession()
{
    dsFestival = (DataSet)Session["Festival"];
    dsGroupsPerFestival = (DataSet)Session["GroupsPerFestival"];
    dsCampSitesPerFestival = (DataSet)Session["CampSitesPerFestival"];
    dsTicketsPerFestival = (DataSet)Session["TicketsPerFestival"];

    dsGroupsAll = (DataSet)Session["GroupsAll"];
    dsCamSitesAll = (DataSet)Session["CampSitesAll"];
    dsTicketsAll = (DataSet)Session["TicketsAll"];
    dsStagesAll = (DataSet)Session["StagesAll"];

    dsGroupsNotOnFestival = (DataSet)Session["GroupsNotOnFestival"];
    dsCampSitesNotOnFestival = (DataSet)Session["CampSitesNotOnFestival"];
    dsTicketsNotOnFestival = (DataSet)Session["TicketsNotOnFestival"];
}

/// <summary>
/// Fills all the datasets required for this page to run properly with data from the database
/// </summary>
protected void getDataFromDatabase()
{
    //Collecting details about the chosen festival
    dsFestival = webService.Festivals_GetFestival(strFormIdFest);
    Session["Festival"] = dsFestival;

    //Collecting all bands performing on the chosen festival
    dsGroupsPerFestival = webService.Festivals_GetBandsOfFestival(strFormIdFest);
    Session["GroupsPerFestival"] = dsGroupsPerFestival;

    dsCampSitesPerFestival = webService.Festivals_GetCampingsOfFestival(strFormIdFest);
    Session["CampSitesPerFestival"] = dsCampSitesPerFestival;

    dsTicketsPerFestival = webService.Festivals_GetTicketTypesOfFestival(strFormIdFest);
    Session["TicketsPerFestival"] = dsTicketsPerFestival;

    //Filling all datasets with all available groups, tickets and campsites
    dsGroupsAll = webService.Festivals_GetBandsNotOfFestival(strFormIdFest);
    Session["GroupsAll"] = dsGroupsAll;

    dsTicketsAll = webService.Festivals_GetTicketTypesNotOfFestival(strFormIdFest);
    Session["TicketsAll"] = dsTicketsAll;

    dsCamSitesAll = webService.Festivals_GetCampingsNotOfFestival(strFormIdFest);
    Session["CampSitesAll"] = dsTicketsAll;

    dsStagesAll = webService.Festivals_GetStages();
    Session["StagesAll"] = dsStagesAll;

    //Filling dataset with groups, campsites and tickets not on this festival
    dsGroupsNotOnFestival = webService.Festivals_GetBandsOfFestival("%");

    Session["GroupsNotOnFestival"] = dsGroupsNotOnFestival;

    dsCampSitesNotOnFestival = webService.Festivals_GetCampingsNotOfFestival(strFormIdFest);
    Session["CampSitesNotOnFestival"] = dsCampSitesNotOnFestival;

    dsTicketsNotOnFestival = webService.Festivals_GetTicketTypesNotOfFestival(strFormIdFest);
    Session["TicketsNotOnFestival"] = dsTicketsNotOnFestival;
}

这是带有btnDeleteGroup_Command 动作处理程序的中继器

<asp:Repeater ID="rptDeleteGroups" runat="server">
    <ItemTemplate>
        <asp:UpdatePanel ID="DeleteGroupUpdatePanel" runat="server">
            <ContentTemplate>
                <li>
                    <asp:Label ID="lblGroupName" runat="server" Text='<%# Eval("band_naam") %>' /></li>
                <li style="border-bottom: 1px solid white;">
                    <asp:Label ID="lblStageD" runat="server" Text='<%# Eval("pod_omschr") %>' />
                    <asp:ImageButton ID="btnDeleteGroup" runat="server" ImageUrl="~/Images/minus.png" Width="20px"
                        OnCommand="btnDeleteGroup_Command" CommandName='<%# Eval("band_id") + "-" + Eval("pod_id") %>' meta:resourcekey="btnDeleteGroupResource1" />
                </li>
            </ContentTemplate>
        </asp:UpdatePanel>
    </ItemTemplate>
</asp:Repeater>

/// <summary>
/// Deletes a group based on the group id and stage id contained in the commandname
/// </summary>
/// <param name="sender"></param>
/// <param name="e">Stats about the event, like the commandname</param>
protected void btnDeleteGroup_Command(object sender, CommandEventArgs e)
{
    String[] arguments = e.CommandName.Split(new char[] { '-' }, StringSplitOptions.None);

    try
    {
        int intResult = webService.Festivals_DeleteBandFestival(strFormIdFest, arguments[0], arguments[1]);

        divResult.Visible = true;
        lblResult.ForeColor = System.Drawing.Color.White;
        if (intResult == 1)
        {
            lblResult.Text = GetLocalResourceObject("SaveSuccess").ToString();
            Session["EditsMade"] = true;
        }
        else if (intResult == -1)
        {
            lblResult.ForeColor = System.Drawing.Color.LightSalmon;
            lblResult.Text = GetLocalResourceObject("ErrorNoDeleted").ToString();
        }
        else if (intResult > 1)
        {
            lblResult.Text = GetLocalResourceObject("ErrorMultipleDeleted").ToString();
            Session["EditsMade"] = true;
        }
    }
    catch (Exception fatal)
    {
        divResult.Visible = true;
        lblResult.ForeColor = System.Drawing.Color.LightSalmon;
        lblResult.Text = GetLocalResourceObject("Error").ToString();
    }
}

【问题讨论】:

    标签: c# asp.net data-binding repeater


    【解决方案1】:

    IMO,您应该像这样编写转发器的 ItemCommand 事件:

    if(e.CommandArgument=="Delete")
    {
      try
      {
        // Delete Logic
        // Binding repeaters again
      }
      catch
      {
        // exception handling code
      }
    }
    

    【讨论】:

    • 好吧,我可以同意。但这意味着我必须在删除逻辑和重新绑定中继器之间更新数据集。我希望有一个更集中的代码。我会调查的
    • 就我的经验而言,是的。而且你应该一次只重新绑定受影响的中继器。
    • 这完成了工作...使用最新数据集更新会话有助于很好地绑定中继器!我没有使用您提供的 ItemCommand 方法,因为 imagebutton 有它自己的 onCommand 事件,它也可以工作。
    猜你喜欢
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    • 1970-01-01
    相关资源
    最近更新 更多