gpt4 book ai didi

c# - 插入后 ASP.NET DetailsView 返回空白

转载 作者:行者123 更新时间:2023-11-30 22:39:49 28 4
gpt4 key购买 nike

我有两个页面:第一个是绑定(bind)到 DataSet 的 gridview,第二个是绑定(bind)到 DataSet 的 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 页面)但插入继续返回空白的详细信息 View 。

更新:

这是详细信息页面的代码隐藏:

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>

最佳答案

你能试试这个并告诉我吗?

 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"

关于c# - 插入后 ASP.NET DetailsView 返回空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5534907/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com