更新数据集时,ASP.NET转发器不会更新

时间:2013-05-20 14:09:57

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

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

我有一个页面,其中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();
    }
}

1 个答案:

答案 0 :(得分:1)

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

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