0

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

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

getDataFromDatabase功能getDataFromSession

/// <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();
    }
}
4

1 回答 1

1

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

if(e.CommandArgument=="Delete")
{
  try
  {
    // Delete Logic
    // Binding repeaters again
  }
  catch
  {
    // exception handling code
  }
}
于 2013-05-20T14:33:43.157 回答