简体   繁体   中英

Insert and update won't push with a Master page

We are currently updating our asp classic pages to ASP.net and are limited in our toolset (only tools available with Asp.net in VS 2013). Our web apps are a front end for MSSQL databases (SQL Server 2008). We are running into an issue with Insert and Update commands from a FormView. When we do not use a master page everything works perfectly, but when we apply a master page and content placeholders, the data does not push. We can delete records fine, but we cannot get data to change/insert into the tables. When the insert command runs it creates a new record with no data. We've tried using all variations of the ClientID setting, and view state modes (which is where we think the problem is). Any help would be appreciated.

Page Header

<%@ Page Title="TEST" Language="C#" MasterPageFile="~/MASTERSTYLE/MXGWeb.master" AutoEventWireup="True" CodeFile="Default2.aspx.cs" Inherits="ETO_Trax_Default2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Head" Runat="Server"></asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server" ClientIDMode="Static">

Form View

<asp:FormView ID="ETOFormView" runat="server" DataSourceID="ETOFormSQLDataSource" OnItemUpdated="ETOFormView_ItemUpdated" OnItemDeleted="ETOFormView_ItemDeleted"  DataKeyNames="ID">
<EditItemTemplate>
ID:<asp:Label Text='<%# Eval("ID") %>' runat="server" ID="IDLabel1" /><br />
ETOType: <asp:TextBox Text='<%# Bind("ETOType") %>' runat="server" ID="ETOTypeTextBox"/><br />
<asp:LinkButton runat="server" Text="Update" CommandName="Update" ID="UpdateButton" CausesValidation="True" />&nbsp;<asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" ID="UpdateCancelButton" CausesValidation="False" />
</EditItemTemplate>
<InsertItemTemplate>
ETOType:<asp:TextBox Text='<%# Bind("ETOType") %>' runat="server" ID="ETOTypeTextBox" /><br />    
<asp:LinkButton runat="server" Text="Insert" CommandName="Insert" ID="InsertButton" CausesValidation="True" />&nbsp;<asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" ID="InsertCancelButton" CausesValidation="False" />
</InsertItemTemplate>
<ItemTemplate>
ID:<asp:Label Text='<%# Eval("ID") %>' runat="server" ID="IDLabel" /><br />
ETOType:<asp:Label Text='<%# Bind("ETOType") %>' runat="server" ID="ETOTypeLabel" /><br />
<asp:LinkButton runat="server" Text="Edit" CommandName="Edit" ID="EditButton" CausesValidation="False" />&nbsp;<asp:LinkButton runat="server" Text="Delete" CommandName="Delete" ID="DeleteButton" CausesValidation="False" />&nbsp;<asp:LinkButton runat="server" Text="New" CommandName="New" ID="NewButton" CausesValidation="False" />
</ItemTemplate>
</asp:FormView>

SQL Data Source:

<asp:SqlDataSource ID="ETOFormSQLDataSource" runat="server" ConnectionString='<%$ ConnectionStrings:SQLConnectionString %>’
    DeleteCommand="DELETE FROM [TABLE] WHERE [ID] = @ID"
    InsertCommand="INSERT INTO [TABLE] ([VALUE]) VALUES (@VALUE) SELECT @ID = SCOPE_IDENTITY()"
    SelectCommand="SELECT * FROM [TABLE] WHERE [ID] = @ID"
    UpdateCommand="UPDATE TABLE SET VALUE=@VALUE WHERE ID=@ID" 
    OnInserted="ETOFormSQLDataSource_Inserted">
<SelectParameters>
        <asp:Parameter Name="ID" Type="Int32" DefaultValue="0" />
    </SelectParameters>
    <DeleteParameters>
        <asp:Parameter Name="ID" Type="Int32"></asp:Parameter>
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="ID" Direction="Output" Type="int32"></asp:Parameter>
    </InsertParameters>
</asp:SqlDataSource>

the InsertParameters collection has only one parameter that is an output parameter so it cannot insert any value because does not know how to pass that information:

<InsertParameters>
 <asp:Parameter Name="ID" Direction="Output" Type="int32"></asp:Parameter>
 <asp:Parameter Name="ETOType" Type="WhateverItIs"></asp:Parameter>
</InsertParameters>

notice that the new parameter is named ETOType because that's the name used in the view to bind the controls.

even adding the required parameter i think would not work because of the INSERT sql command that should become:

INSERT INTO [TABLE] ([VALUE]) VALUES (@ETOType) SELECT @ID = SCOPE_IDENTITY()

that's because in view code ETOType is the name used inside Bind , assigned to the parameter also and that's the value expected by the binder.

not sure about it but may be that the correct syntax for the INSERT query should be with statement terminators:

INSERT INTO [TABLE] ([VALUE]) VALUES (@ETOType); SELECT @ID = SCOPE_IDENTITY();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM