插入后 ASP.NET DetailsView 返回空白
我有两个页面:第一个是绑定到 DataSet 的 gridview,第二个页面是绑定到用于编辑和插入的 DataSet 的 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 页面),但插入继续返回空白详细信息视图。
更新:
这是详细信息页面的代码隐藏:
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>
<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>
<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>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="New" Text="Nuovo"></asp:LinkButton>
<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>
I have two pages: the first it's a gridview that bind to DataSet, the second one have DetailsView that bind to DataSet that is used for editing and inserting.
Upon insertion on DetailsView Page, I want to redirect to GridView Page.
if (Request.QueryString["Id"] == null)
{
//prevPage = Request.UrlReferrer.ToString();
if (!IsPostBack)
{
DetailsView1.ChangeMode(DetailsViewMode.Insert);
}
The detail page allow a correct insert of the new record but after saving it return a blank page; how I can obtain to return to an updated gridview page?
I tried to use the ItemInserted event of the DetailsView but it simply don't fire.
Update:
as suggested I add the following code:
protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
Response.Redirect("ms_Ticket.aspx");
}
(with ms_Ticket being the gridview page) but the insert continue to return the blank detailsview.
Update:
here's the codebehind of the detail page:
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");
}
}
}
here's the design:
//------------------------------------------------------------------------------
// <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;
}
}
Update:
here's the complete form code:
<%@ 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>
<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>
<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>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="New" Text="Nuovo"></asp:LinkButton>
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你能试试这个并让我知道吗?
编辑:将此替换
为
,因为您忘记将事件附加到详细信息视图
OnItemInserted="DetailsView1_ItemInserted"
Can you try this and let me know ?
Edit: Replace this
with
as you forgot to attach the event to your DetailsView
OnItemInserted="DetailsView1_ItemInserted"
我有同样的问题!经过一些调试后,我意识到在发布后 DetailViewMode 又回到了插入模式(我在 aspx 文件中定义的默认模式)。为了保证保存的值在更新操作后可见,我必须调用 Changemode 来保证 DetailView 保持在 EditMode 上。
I had the same problem ! After some debug i realized that after the post the DetailViewMode was back to Insert mode ( Default mode that i defined in the aspx file). To garantee that the values saved were visible after the updated action i had to call the Changemode to garantee that the DetailView stayed on the EditMode.