DetailsView Update / Insert适用于localhost而非Web服务器

时间:2015-02-25 18:41:44

标签: asp.net vb.net detailsview

我发现的所有研究都在讨论那些无法使用它的人(在DetailsView中插入/更新)。我的代码在通过VS2012运行时有效,但在我将网站复制到我的网络服务器时没有。

DetailsView链接按钮及其工作地点:

  • 编辑:开发和Web服务器
  • 更新:仅限开发
  • 取消:开发和Web服务器
  • 删除:开发和Web服务器
  • 新:开发和Web服务器
  • 插入:仅限开发

当我将文件复制到我的网络服务器并通过我的电脑上的IE运行时,更新插入链接按钮在我点击它们时什么也不做,就好像它们一样没有任何关系。通过点击修改链接按钮,然后点击取消链接按钮, DetailsView 将进入和退出修改模式 。通过点击新建链接按钮,然后点击取消链接按钮, DetailsView 将进入和退出插入模式

如果我在VS 2012中运行调试模式,并且只是使用托管在我本地IIS上的localhost运行站点,则上述所有工作都能很好地工作。

我检查了本地IIS和服务器的IIS之间的设置,它们看起来是一样的。

  • 的web.config
  • 默认应用
  • 网站设置,如身份验证,应用程序设置等。

在下面的代码中,您会注意到我有两组链接按钮。在DetailsView标签中设置的那些,如AutoGenerateDeleteButton =“true”等。我还在DetalsView的末尾附近定义了按钮,因为我想要更多地控制它们,如确认删除。如果我使用任何一种方式(一组注释掉)或两者都被定义,我得到相同的结果。

这是我的代码:

<asp:DetailsView
    ID="dtvEmployee"
    runat="server"
    AutoGenerateRows="false"
    DataKeyNames="VHRID"
    DataSourceID="SqlEmployee"
    DefaultMode="ReadOnly"
    HeaderStyle-Font-Bold="true"
    HeaderStyle-Font-Underline="true"
    HeaderText="View Item..."
    GridLines="Both" AutoGenerateDeleteButton="true" AutoGenerateEditButton="true" AutoGenerateInsertButton="true">
    <Fields>
        <%-- VHRID --%>
        <asp:BoundField DataField="VHRID" HeaderText="VHRID:" ReadOnly="true" SortExpression="VHRID" Visible="true" InsertVisible="false" />
        <%-- Firstname --%>
        <asp:TemplateField HeaderText="Firstname:" SortExpression="Firstname">
            <ItemTemplate>
                <asp:TextBox ID="txtFirstname" runat="server" Text='<%# Bind("Firstname")%>' MaxLength="50" />
                <asp:RequiredFieldValidator ControlToValidate="txtFirstname" runat="server"
                    Display="Dynamic" ForeColor="Red" ID="rfvFirstname"
                    ErrorMessage="Firstname is a required field." />
            </ItemTemplate>
        </asp:TemplateField>
        <%-- Lastname --%>
        <asp:TemplateField HeaderText="Lastname:" SortExpression="Lastname">
            <ItemTemplate>
                <asp:TextBox ID="txtLastname" runat="server" Text='<%# Bind("Lastname")%>' MaxLength="50" />
                <asp:RequiredFieldValidator ControlToValidate="txtLastname" runat="server"
                    Display="Dynamic" ForeColor="Red" ID="rfvLastname"
                    ErrorMessage="Lastname is a required field." />
            </ItemTemplate>
        </asp:TemplateField>
        <%-- Username --%>
        <asp:TemplateField HeaderText="Username:" SortExpression="Username">
            <ItemTemplate>
                <asp:TextBox ID="txtUsername" runat="server" Text='<%# Bind("Username")%>' MaxLength="50" />
                <asp:RequiredFieldValidator ControlToValidate="txtUsername" runat="server"
                    Display="Dynamic" ForeColor="Red" ID="rfvUsername"
                    ErrorMessage="Username is a required field." />
            </ItemTemplate>
        </asp:TemplateField>
        <%-- Email --%>
        <asp:TemplateField HeaderText="Email:" SortExpression="Email">
            <ItemTemplate>
                <asp:TextBox ID="txtEmail" runat="server" Text='<%# Bind("Email")%>' MaxLength="50" />
                <asp:RequiredFieldValidator ControlToValidate="txtEmail" runat="server"
                    Display="Dynamic" ForeColor="Red" ID="rfvEmail"
                    ErrorMessage="Email is a required field." />
            </ItemTemplate>
        </asp:TemplateField>
        <%-- Title --%>
        <asp:TemplateField HeaderText="Title:" SortExpression="Title">
            <ItemTemplate>
                <asp:TextBox ID="txtTitle" runat="server" Text='<%# Bind("EmpTitle")%>' MaxLength="50" />
                <asp:RequiredFieldValidator ControlToValidate="txtTitle" runat="server"
                    Display="Dynamic" ForeColor="Red" ID="rfvTitle"
                    ErrorMessage="Title is a required field." />
            </ItemTemplate>
        </asp:TemplateField>
        <%-- Location --%>
        <asp:TemplateField HeaderText="Location:" SortExpression="Location">
            <ItemTemplate>
                <asp:TextBox ID="txtLocation" runat="server" Text='<%# Bind("Location")%>' MaxLength="50" Visible="false" />
                <asp:RequiredFieldValidator ControlToValidate="txtLocation" runat="server"
                    Display="Dynamic" ForeColor="Red" ID="rfvLocation"
                    ErrorMessage="Location is a required field." />
                <asp:DropDownList ID="ddlLocation" runat="server"
                    OnSelectedIndexChanged="ddlLocation_SelectedIndexChanged"
                    AutoPostBack="true"
                    Width="300">
                    <asp:ListItem Text="PHX" Value="PHX" />
                    <asp:ListItem Text="DEN" Value="DEN" />
                    <asp:ListItem Text="LAS" Value="LAS" />
                    <asp:ListItem Text="NOG" Value="NOG" />
                    <asp:ListItem Text="RENO" Value="RENO" />
                    <asp:ListItem Text="TUC" Value="TUC" />
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
        <%-- Office Number --%>
        <asp:TemplateField HeaderText="Office Number:" SortExpression="OfficeNumber">
            <ItemTemplate>
                <asp:TextBox ID="txtOfficeNumber" runat="server" Text='<%# Bind("OfficeNumber")%>' MaxLength="50" Visible="false" />
                <asp:RequiredFieldValidator ControlToValidate="txtOfficeNumber" runat="server"
                    Display="Dynamic" ForeColor="Red" ID="rfvOfficeNumber"
                    ErrorMessage="Office Number is a required field." />
                <asp:DropDownList ID="ddlOfficeNumber" runat="server"
                    OnSelectedIndexChanged="ddlOfficeNumber_SelectedIndexChanged"
                    AppendDataBoundItems="true"
                    AutoPostBack="true"
                    CausesValidation="false"
                    DataSourceID="sqlOfficeNumbers"
                    DataTextField="OfficeNumber"
                    DataValueField="Room_Num"
                    Width="300">
                    <asp:ListItem Text="None" Value="0"></asp:ListItem>
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
        <%-- Office Phone --%>
        <asp:TemplateField HeaderText="Office Phone:" SortExpression="OfficePhone">
            <ItemTemplate>
                <asp:TextBox ID="txtOfficePhone" runat="server" Text='<%# Bind("OfficePhone")%>' MaxLength="50"  />
                <asp:RequiredFieldValidator ControlToValidate="txtOfficePhone" runat="server"
                    Display="Dynamic" ForeColor="Red" ID="rfvOfficePhone"
                    ErrorMessage="Office Phone is a required field." />
                <asp:RegularExpressionValidator ControlToValidate="txtOfficePhone" runat="server"
                    Display="Dynamic" ForeColor="Red" ID="revOfficePhone"
                    ValidationExpression="^[2-9]\d{2}\d{3}\d{4}$" EnableClientScript="false"
                    ErrorMessage="Office Phone must be 9 numeric digits." />
            </ItemTemplate>
        </asp:TemplateField>
        <%-- Show Photo --%>
        <asp:TemplateField HeaderText="Show Photo:" SortExpression="ShowPhoto">
            <ItemTemplate>
                <asp:CheckBox ID="chkShowPhoto" runat="server" Checked='<%# Bind("ShowPhoto")%>' />
            </ItemTemplate>
        </asp:TemplateField>
        <%-- Status --%>
        <asp:TemplateField HeaderText="Status:" SortExpression="Status">
            <ItemTemplate>
                <asp:TextBox ID="txtStatus" runat="server" Text='<%# Bind("Status")%>' MaxLength="50" Visible="false" />
                <asp:RequiredFieldValidator ControlToValidate="txtStatus" runat="server"
                    Display="Dynamic" ForeColor="Red" ID="rfvStatus"
                    ErrorMessage="Status is a required field." />
                <asp:DropDownList ID="ddlStatus" runat="server"
                    OnSelectedIndexChanged="ddlStatus_SelectedIndexChanged"
                    AutoPostBack="true"
                    Width="300">
                    <asp:ListItem Text="Active" Value="Active" />
                    <asp:ListItem Text="Terminated" Value="Terminated" />
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
        <%-- LinkButtons and the modes they show up in --%>
        <asp:TemplateField>
            <ItemTemplate>
                <%-- DetailsView - ReadOnly Mode --%>
                <asp:linkbutton ID="lnkEdit" Text="Edit" runat="server" CommandName="Edit" CausesValidation="false"></asp:linkbutton>
                <asp:LinkButton ID="lnkDelete" Text="Delete" runat="server" CommandName="Delete" CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this record?');"></asp:LinkButton>
                <asp:linkbutton ID="lnkNew" Text="New" runat="server" CommandName="New" CausesValidation="false"></asp:linkbutton>
            </ItemTemplate>
            <EditItemTemplate>
                <%-- DetailsView - Edit Mode --%>
                <asp:linkbutton ID="lnkUpdate" Text="Update" runat="server" CommandName="Update"></asp:linkbutton>
                <asp:linkbutton ID="lnkCancel" Text="Cancel" runat="server" CommandName="Cancel" CausesValidation="false"></asp:linkbutton>
            </EditItemTemplate>
            <InsertItemTemplate>
                <%-- DetailsView - Insert Mode --%>
                <asp:linkbutton ID="lnkInsert" Text="Insert" runat="server" CommandName="Insert"></asp:linkbutton>
                <asp:linkbutton ID="lnkCancel" Text="Cancel" runat="server" CommandName="Cancel" CausesValidation="false"></asp:linkbutton>
            </InsertItemTemplate>
        </asp:TemplateField>
    </Fields>
</asp:DetailsView>

这是SQLDatasource:

<%-- For DetailsView --%>
<asp:SqlDataSource ID="SqlEmployee" runat="server"
    ConnectionString="<%$ ConnectionStrings:DWStageConnectionString %>"
    DeleteCommand="UPDATE [MasterTempEmployeeStage] SET
        [IsDeleted] = 1
        WHERE [VHRID] = @VHRID"
    InsertCommand="INSERT INTO [MasterTempEmployeeStage] (
        [Firstname], [Lastname]
        , [Location], [Username]
        , [Email], [EmpTitle]
        , [OfficeNumber], [OfficePhone]
        , [ShowPhoto], [Status]
        ) VALUES (
        @Firstname, @Lastname
        , @Location, @Username
        , @Email, @EmpTitle
        , @OfficeNumber, @OfficePhone
        , @ShowPhoto, @Status);
        SELECT @VHRID = SCOPE_IDENTITY();"
    SelectCommand="SELECT [VHRID]
        , [Firstname], [Lastname]
        , [Location], [Username]
        , [Email], [EmpTitle]
        , [OfficeNumber], [OfficePhone]
        , [ShowPhoto], [Status]
        FROM [MasterTempEmployeeStage]
        WHERE [VHRID] = @VHRID
        ORDER BY [Lastname], [Firstname]"
    UpdateCommand="UPDATE [MasterTempEmployeeStage] SET
        [Firstname] = @Firstname
        , [Lastname] = @Lastname
        , [Location] = @Location
        , [Username] = @Username
        , [Email] = @Email
        , [EmpTitle] = @EmpTitle
        , [OfficeNumber] = @OfficeNumber
        , [OfficePhone] = @OfficePhone
        , [ShowPhoto] = @ShowPhoto
        , [Status] = @Status
        WHERE [VHRID] = @VHRID">
    <SelectParameters>
        <asp:ControlParameter ControlID="ddlEmployees" PropertyName="SelectedValue" Name="VHRID" />
    </SelectParameters>
    <DeleteParameters>
        <asp:ControlParameter ControlID="ddlEmployees" PropertyName="SelectedValue" Name="VHRID" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="VHRID" Direction="Output" Type="Int32" />
        <asp:Parameter Name="Firstname" Type="String" />
        <asp:Parameter Name="Lastname" Type="String" />
        <asp:Parameter Name="Location" Type="String" />
        <asp:Parameter Name="Username" Type="String" />
        <asp:Parameter Name="Email" Type="String" />
        <asp:Parameter Name="EmpTitle" Type="String" />
        <asp:Parameter Name="OfficeNumber" Type="String" />
        <asp:Parameter Name="OfficePhone" Type="String" />
        <asp:Parameter Name="ShowPhoto" Type="String" />
        <asp:Parameter Name="Status" Type="String" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="Firstname" Type="String" />
        <asp:Parameter Name="Lastname" Type="String" />
        <asp:Parameter Name="Location" Type="String" />
        <asp:Parameter Name="Username" Type="String" />
        <asp:Parameter Name="Email" Type="String" />
        <asp:Parameter Name="EmpTitle" Type="String" />
        <asp:Parameter Name="OfficeNumber" Type="String" />
        <asp:Parameter Name="OfficePhone" Type="String" />
        <asp:Parameter Name="ShowPhoto" Type="String" />
        <asp:Parameter Name="Status" Type="String" />
        <%--<asp:Parameter Name="VHRID" Type="Int32" />--%>
        <asp:ControlParameter ControlID="ddlEmployees" PropertyName="SelectedValue" Name="VHRID" />
    </UpdateParameters>
</asp:SqlDataSource>

1 个答案:

答案 0 :(得分:0)

找到解决方案,我不知道为什么它可以在我的开发电脑上工作,而不是开始时的实际网络服务器,但现在就是。

我必须在“插入和更新”链接按钮上设置CausesValidation =“false”。如果在DetailsView定义中使用AutoGenerateEditButton =“true”*和其他内容,则无法设置此项。

作为测试我:

  • 删除了我在DetailsView底部附近定义的链接按钮
  • 将详细信息留在DetailsView标记
  • 删除了所有验证器

将上述内容复制到Web服务器,一切正常。因此,在Update和Insert链接按钮上不将CausesValidation设置为false会阻止它们触发。既然我想要验证器,我就这样做了......

  • 从DetailsView
  • 中删除了自动生成链接按钮
  • 使用DetailsView底部附近的链接按钮
  • 在更新和插入链接按钮上将CausesValidation设置为false
  • 放回验证器

现在一切正常。

请参阅下面的新代码:

<asp:DetailsView
    ID="dtvEmployee"
    runat="server"
    AutoGenerateRows="false"
    DataKeyNames="VHRID"
    DataSourceID="SqlEmployee"
    DefaultMode="ReadOnly"
    HeaderStyle-Font-Bold="true"
    HeaderStyle-Font-Underline="true"
    HeaderText="View Item..."
    GridLines="Both">
    <Fields>
        <%-- VHRID --%>
        <asp:BoundField DataField="VHRID" HeaderText="VHRID:" ReadOnly="true" SortExpression="VHRID" Visible="true" InsertVisible="false" />
        <%-- Firstname --%>
        <asp:TemplateField HeaderText="Firstname:" SortExpression="Firstname">
            <ItemTemplate>
                <asp:TextBox ID="txtFirstname" runat="server" Text='<%# Bind("Firstname")%>' MaxLength="50" />
                <asp:RequiredFieldValidator ControlToValidate="txtFirstname" runat="server"
                    Display="Dynamic" ForeColor="Red" ID="rfvFirstname"
                    ErrorMessage="Firstname is a required field." />
            </ItemTemplate>
        </asp:TemplateField>
        <%-- Lastname --%>
        <asp:TemplateField HeaderText="Lastname:" SortExpression="Lastname">
            <ItemTemplate>
                <asp:TextBox ID="txtLastname" runat="server" Text='<%# Bind("Lastname")%>' MaxLength="50" />
                <asp:RequiredFieldValidator ControlToValidate="txtLastname" runat="server"
                    Display="Dynamic" ForeColor="Red" ID="rfvLastname"
                    ErrorMessage="Lastname is a required field." />
            </ItemTemplate>
        </asp:TemplateField>
        <%-- Username --%>
        <asp:TemplateField HeaderText="Username:" SortExpression="Username">
            <ItemTemplate>
                <asp:TextBox ID="txtUsername" runat="server" Text='<%# Bind("Username")%>' MaxLength="50" />
                <asp:RequiredFieldValidator ControlToValidate="txtUsername" runat="server"
                    Display="Dynamic" ForeColor="Red" ID="rfvUsername"
                    ErrorMessage="Username is a required field." />
            </ItemTemplate>
        </asp:TemplateField>
        <%-- Email --%>
        <asp:TemplateField HeaderText="Email:" SortExpression="Email">
            <ItemTemplate>
                <asp:TextBox ID="txtEmail" runat="server" Text='<%# Bind("Email")%>' MaxLength="50" />
                <asp:RequiredFieldValidator ControlToValidate="txtEmail" runat="server"
                    Display="Dynamic" ForeColor="Red" ID="rfvEmail"
                    ErrorMessage="Email is a required field." />
            </ItemTemplate>
        </asp:TemplateField>
        <%-- Title --%>
        <asp:TemplateField HeaderText="Title:" SortExpression="Title">
            <ItemTemplate>
                <asp:TextBox ID="txtTitle" runat="server" Text='<%# Bind("EmpTitle")%>' MaxLength="50" />
                <asp:RequiredFieldValidator ControlToValidate="txtTitle" runat="server"
                    Display="Dynamic" ForeColor="Red" ID="rfvTitle"
                    ErrorMessage="Title is a required field." />
            </ItemTemplate>
        </asp:TemplateField>
        <%-- Location --%>
        <asp:TemplateField HeaderText="Location:" SortExpression="Location">
            <ItemTemplate>
                <asp:TextBox ID="txtLocation" runat="server" Text='<%# Bind("Location")%>' MaxLength="50" Visible="false" />
                <asp:RequiredFieldValidator ControlToValidate="txtLocation" runat="server"
                    Display="Dynamic" ForeColor="Red" ID="rfvLocation"
                    ErrorMessage="Location is a required field." />
                <asp:DropDownList ID="ddlLocation" runat="server"
                    OnSelectedIndexChanged="ddlLocation_SelectedIndexChanged"
                    AutoPostBack="true"
                    Width="300">
                    <asp:ListItem Text="PHX" Value="PHX" />
                    <asp:ListItem Text="DEN" Value="DEN" />
                    <asp:ListItem Text="LAS" Value="LAS" />
                    <asp:ListItem Text="NOG" Value="NOG" />
                    <asp:ListItem Text="RENO" Value="RENO" />
                    <asp:ListItem Text="TUC" Value="TUC" />
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
        <%-- Office Number --%>
        <asp:TemplateField HeaderText="Office Number:" SortExpression="OfficeNumber">
            <ItemTemplate>
                <asp:TextBox ID="txtOfficeNumber" runat="server" Text='<%# Bind("OfficeNumber")%>' MaxLength="50" Visible="false" />
                <asp:RequiredFieldValidator ControlToValidate="txtOfficeNumber" runat="server"
                    Display="Dynamic" ForeColor="Red" ID="rfvOfficeNumber"
                    ErrorMessage="Office Number is a required field." />
                <asp:DropDownList ID="ddlOfficeNumber" runat="server"
                    OnSelectedIndexChanged="ddlOfficeNumber_SelectedIndexChanged"
                    AppendDataBoundItems="true"
                    AutoPostBack="true"
                    CausesValidation="false"
                    DataSourceID="sqlOfficeNumbers"
                    DataTextField="OfficeNumber"
                    DataValueField="Room_Num"
                    Width="300">
                    <asp:ListItem Text="None" Value="0"></asp:ListItem>
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
        <%-- Office Phone --%>
        <asp:TemplateField HeaderText="Office Phone:" SortExpression="OfficePhone">
            <ItemTemplate>
                <asp:TextBox ID="txtOfficePhone" runat="server" Text='<%# Bind("OfficePhone")%>' MaxLength="50"  />
                <asp:RequiredFieldValidator ControlToValidate="txtOfficePhone" runat="server"
                    Display="Dynamic" ForeColor="Red" ID="rfvOfficePhone"
                    ErrorMessage="Office Phone is a required field." />
                <asp:RegularExpressionValidator ControlToValidate="txtOfficePhone" runat="server"
                    Display="Dynamic" ForeColor="Red" ID="revOfficePhone"
                    ValidationExpression="^[2-9]\d{2}\d{3}\d{4}$" EnableClientScript="false"
                    ErrorMessage="Office Phone must be 9 numeric digits." />
            </ItemTemplate>
        </asp:TemplateField>
        <%-- Show Photo --%>
        <asp:TemplateField HeaderText="Show Photo:" SortExpression="ShowPhoto">
            <ItemTemplate>
                <asp:CheckBox ID="chkShowPhoto" runat="server" Checked='<%# Bind("ShowPhoto")%>' />
            </ItemTemplate>
        </asp:TemplateField>
        <%-- Status --%>
        <asp:TemplateField HeaderText="Status:" SortExpression="Status">
            <ItemTemplate>
                <asp:TextBox ID="txtStatus" runat="server" Text='<%# Bind("Status")%>' MaxLength="50" Visible="false" />
                <asp:RequiredFieldValidator ControlToValidate="txtStatus" runat="server"
                    Display="Dynamic" ForeColor="Red" ID="rfvStatus"
                    ErrorMessage="Status is a required field." />
                <asp:DropDownList ID="ddlStatus" runat="server"
                    OnSelectedIndexChanged="ddlStatus_SelectedIndexChanged"
                    AutoPostBack="true"
                    Width="300">
                    <asp:ListItem Text="Active" Value="Active" />
                    <asp:ListItem Text="Terminated" Value="Terminated" />
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
        <%-- LinkButtons and the modes they show up in --%>
        <asp:TemplateField>
            <ItemTemplate>
                <%-- DetailsView - ReadOnly Mode --%>
                <asp:linkbutton ID="lnkEdit" Text="Edit" runat="server" CommandName="Edit" CausesValidation="false"></asp:linkbutton>
                <asp:LinkButton ID="lnkDelete" Text="Delete" runat="server" CommandName="Delete" CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this record?');"></asp:LinkButton>
                <asp:linkbutton ID="lnkNew" Text="New" runat="server" CommandName="New" CausesValidation="false"></asp:linkbutton>
            </ItemTemplate>
            <EditItemTemplate>
                <%-- DetailsView - Edit Mode --%>
                <asp:linkbutton ID="lnkUpdate" Text="Update" runat="server" CommandName="Update" CausesValidation="false"></asp:linkbutton>
                <asp:linkbutton ID="lnkCancel" Text="Cancel" runat="server" CommandName="Cancel" CausesValidation="false"></asp:linkbutton>
            </EditItemTemplate>
            <InsertItemTemplate>
                <%-- DetailsView - Insert Mode --%>
                <asp:linkbutton ID="lnkInsert" Text="Insert" runat="server" CommandName="Insert" CausesValidation="false"></asp:linkbutton>
                <asp:linkbutton ID="lnkCancel" Text="Cancel" runat="server" CommandName="Cancel" CausesValidation="false"></asp:linkbutton>
            </InsertItemTemplate>
        </asp:TemplateField>
    </Fields>
</asp:DetailsView>