2011-04-04 114 views
2

我有兩個頁面:第一個是綁定到DataSet的gridview,第二個具有綁定到DataSet的DetailsView,用於編輯和插入。ASP.NET DetailsView在插入後返回空白

一旦插入DetailsView頁面,我想重定向到GridView頁面。

if (Request.QueryString["Id"] == null) 
{ 
    //prevPage = Request.UrlReferrer.ToString(); 
    if (!IsPostBack) 
    { 
     DetailsView1.ChangeMode(DetailsViewMode.Insert); 
    } 

詳細信息頁面允許正確插入新記錄,但在保存之後返回空白頁;我如何獲得返回到更新的GridView頁面?

我試圖使用DetailsView的ItemInserted事件,但它根本不會觸發。

更新:

的建議我添加以下代碼:

protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e) 
{ 
    Response.Redirect("ms_Ticket.aspx"); 
} 

(與ms_Ticket是在GridView頁面),但插入繼續返回空白DetailsView控件。

更新:

這裏的詳細信息頁面的代碼隱藏:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace Rise_cs.Ticket 
{ 
    public partial class WebForm4 : System.Web.UI.Page 
    { 
     static string prevPage = String.Empty; 

     protected void Page_Load(object sender, EventArgs e) 
     { 

      if (!IsPostBack) 
      { 
       if (!(Request.UrlReferrer == null)) 
       { 
        prevPage = Request.UrlReferrer.ToString(); 
       } 
      } 

      if (Request.QueryString["Id"] == null) 
      { 
       //prevPage = Request.UrlReferrer.ToString(); 
       if (!IsPostBack) 
       { 
        DetailsView1.ChangeMode(DetailsViewMode.Insert); 
       } 



      } 


     } 

     protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e) 
     { 
      Response.Redirect("ms_Ticket.aspx"); 
     } 




    } 
} 

這裏的設計:

//------------------------------------------------------------------------------ 
// <generato automaticamente> 
//  Codice generato da uno strumento. 
// 
//  Le modifiche a questo file possono causare un comportamento non corretto e verranno perse se 
//  il codice viene rigenerato. 
// </generato automaticamente> 
//------------------------------------------------------------------------------ 

namespace Rise_cs.Ticket { 


    public partial class WebForm4 { 

     /// <summary> 
     /// Controllo DetailsView1. 
     /// </summary> 
     /// <remarks> 
     /// Campo generato automaticamente. 
     /// Per la modifica, spostare la dichiarazione di campo dal file di progettazione al file code-behind. 
     /// </remarks> 
     protected global::System.Web.UI.WebControls.DetailsView DetailsView1; 

     /// <summary> 
     /// Controllo SqlDataSource1. 
     /// </summary> 
     /// <remarks> 
     /// Campo generato automaticamente. 
     /// Per la modifica, spostare la dichiarazione di campo dal file di progettazione al file code-behind. 
     /// </remarks> 
     protected global::System.Web.UI.WebControls.SqlDataSource SqlDataSource1; 
    } 
} 

更新:

這裏是完整的表單代碼:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="dt_TicketRiga.aspx.cs" Inherits="Rise_cs.Ticket.WebForm4" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
     DataKeyNames="Id" DataSourceID="SqlDataSource1" Height="50px" Width="125px"> 
     <Fields> 
      <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" 
       ReadOnly="True" SortExpression="Id" /> 
      <asp:TemplateField HeaderText="TicketId" SortExpression="TicketId"> 
       <EditItemTemplate> 
        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("TicketId") %>'></asp:TextBox> 
       </EditItemTemplate> 
       <InsertItemTemplate> 


        <asp:Label ID="Label3" runat="server" 
         Text='<%# Request.QueryString["TicketId"] %>'></asp:Label> 

       </InsertItemTemplate> 
       <ItemTemplate> 
        <asp:Label ID="Label1" runat="server" Text='<%# Bind("TicketId") %>'></asp:Label> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:BoundField DataField="Data" HeaderText="Data" SortExpression="Data" /> 
      <asp:BoundField DataField="Ora" HeaderText="Ora" SortExpression="Ora" /> 
      <asp:BoundField DataField="OperatoreId" HeaderText="OperatoreId" 
       SortExpression="OperatoreId" /> 
      <asp:BoundField DataField="Referente" HeaderText="Referente" 
       SortExpression="Referente" /> 
      <asp:BoundField DataField="ModalitaContattoId" HeaderText="ModalitaContattoId" 
       SortExpression="ModalitaContattoId" /> 
      <asp:BoundField DataField="Descrizione" HeaderText="Descrizione" 
       SortExpression="Descrizione" /> 
      <asp:TemplateField ShowHeader="False"> 
       <EditItemTemplate> 
        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
         CommandName="Update" Text="Aggiorna"></asp:LinkButton> 
        &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
         CommandName="Cancel" Text="Annulla"></asp:LinkButton> 
       </EditItemTemplate> 
       <InsertItemTemplate> 
        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
         CommandName="Insert" onclick="LinkButton1_Click" Text="Inserisci"></asp:LinkButton> 
        &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
         CommandName="Cancel" Text="Annulla"></asp:LinkButton> 
       </InsertItemTemplate> 
       <ItemTemplate> 
        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
         CommandName="Edit" Text="Modifica"></asp:LinkButton> 
        &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
         CommandName="New" Text="Nuovo"></asp:LinkButton> 
        &nbsp;<asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" 
         CommandName="Delete" Text="Elimina"></asp:LinkButton> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Fields> 
    </asp:DetailsView> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
     ConnectionString="<%$ ConnectionStrings:GONGConnectionString1 %>" 
     DeleteCommand="DELETE FROM [TicketRighe] WHERE [Id] = @Id" 
     InsertCommand="INSERT INTO [TicketRighe] ([TicketId], [Data], [Ora], [OperatoreId], [Referente], [ModalitaContattoId], [Descrizione]) VALUES (@TicketId, @Data, @Ora, @OperatoreId, @Referente, @ModalitaContattoId, @Descrizione)" 
     SelectCommand="SELECT * FROM [TicketRighe] WHERE (([Id] = @Id) AND ([TicketId] = @TicketId))" 
     UpdateCommand="UPDATE [TicketRighe] SET [TicketId] = @TicketId, [Data] = @Data, [Ora] = @Ora, [OperatoreId] = @OperatoreId, [Referente] = @Referente, [ModalitaContattoId] = @ModalitaContattoId, [Descrizione] = @Descrizione WHERE [Id] = @Id"> 
     <DeleteParameters> 
      <asp:Parameter Name="Id" Type="Int32" /> 
     </DeleteParameters> 
     <InsertParameters> 
      <asp:QueryStringParameter Name="TicketId" QueryStringField="TicketId" 
       Type="Int32" /> 
      <asp:Parameter Name="Data" Type="DateTime" /> 
      <asp:Parameter Name="Ora" Type="DateTime" /> 
      <asp:Parameter Name="OperatoreId" Type="Int32" /> 
      <asp:Parameter Name="Referente" Type="String" /> 
      <asp:Parameter Name="ModalitaContattoId" Type="Int32" /> 
      <asp:Parameter Name="Descrizione" Type="String" /> 
     </InsertParameters> 
     <SelectParameters> 
      <asp:QueryStringParameter Name="Id" QueryStringField="Id" Type="Int32" /> 
      <asp:QueryStringParameter Name="TicketId" QueryStringField="TicketId" 
       Type="Int32" /> 
     </SelectParameters> 
     <UpdateParameters> 
      <asp:Parameter Name="TicketId" Type="Int32" /> 
      <asp:Parameter Name="Data" Type="DateTime" /> 
      <asp:Parameter Name="Ora" Type="DateTime" /> 
      <asp:Parameter Name="OperatoreId" Type="Int32" /> 
      <asp:Parameter Name="Referente" Type="String" /> 
      <asp:Parameter Name="ModalitaContattoId" Type="Int32" /> 
      <asp:Parameter Name="Descrizione" Type="String" /> 
      <asp:Parameter Name="Id" Type="Int32" /> 
     </UpdateParameters> 
    </asp:SqlDataSource> 
</asp:Content> 

回答

2

你可以試試這個,讓我知道嗎?

protected void dtv_ItemInserted(object sender, DetailsViewInsertedEventArgs e) 
{ 
    Response.Redirect("page.aspx"); 
} 

編輯:替換此

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
    DataKeyNames="Id" DataSourceID="SqlDataSource1" Height="50px" Width="125px"> 

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="Id" 
      DataSourceID="SqlDataSource1" Height="50px" Width="125px" OnItemInserted="DetailsView1_ItemInserted"> 
     </asp:DetailsView> 

,你忘了事件附加到您的DetailsView OnItemInserted="DetailsView1_ItemInserted"

+0

謝謝,但即使如此,它只是在UpdatePanel中返回Detailsview的空白詳細信息頁面 – 2011-04-04 05:43:37

+0

?請將您的代碼和DetailsView代碼放在後面。 – 2011-04-04 05:54:14

+0

我發佈了代碼,謝謝 – 2011-04-04 06:02:15

1

我有同樣的問題!經過一些調試後,我意識到在發佈後DetailViewMode返回插入模式(我在aspx文件中定義的默認模式)。爲了保證保存的值在更新的操作後可見,我必須調用Changemode來保證DetailView停留在EditMode上。