开发者

ASP.NET DetailsView return as blank after Insert

开发者 https://www.devze.com 2023-02-22 00:48 出处:网络
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.

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>
                    &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" 
                 开发者_StackOverflow       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>


Can you try this and let me know ?

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

Edit: Replace this

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

with

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

as you forgot to attach the event to your DetailsView OnItemInserted="DetailsView1_ItemInserted"


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消