2013-05-20 52 views
0

我意識到這是一個愚蠢的問題,但仍然存在。更新數據集時不更新ASP.NET中繼器

我有一個頁面,其中填充了數據的一個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; 
} 

這裏是與中繼行動處理器

<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

海事組織,你應該這樣編寫你的中繼的ItemCommand事件:

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

好吧,我同意這一點。但這意味着我必須更新我的刪除邏輯和重新綁定中繼器之間的數據集。我希望有一個更集中的代碼。我會看看 – DerpyNerd

+1

據我的經驗說,是的。同時你也應該只重新綁定受影響的中繼器。 –

+0

我忘了,謝謝。我會回報 – DerpyNerd