ASP.. NET Formview未正确更新

本文关键字:更新 NET Formview ASP | 更新日期: 2023-09-27 18:10:47

我正在将我的网站从经典ASP转换为ASP.net,除了一些颠簸之外,直到今天我还没有遇到任何问题。但是我在忙活中学习。

我试图在formview中更新记录,每次我这样做,我得到以下错误:"必须声明标量变量"@FirstName"。

我已经谷歌和搜索,不能看到我做错了什么。我敢肯定有什么地方我做错了。

      <asp:FormView ID="formRecruit" runat="server" DataSourceID="sourceRecruit" 
        EnableModelValidation="True" DataKeyNames="ID">
    <ItemTemplate>
        <asp:Table ID="Table1" Width="100%" runat="server">         
            <asp:TableRow ID="TableRow1" runat="server">
                <asp:TableCell Width="50%">
                <asp:Table ID="Table2" Width="100%" HorizontalAlign="Center" runat="server">
                <asp:TableRow ID="TableRow2" runat="server">
                <asp:TableCell>
                <asp:Table ID="Table3" Width="100%" runat="server">  
                    <asp:TableRow ID="TableRow3" runat="server">
                        <asp:TableCell>Name:</asp:TableCell>
                        <asp:TableCell><%#Eval("FirstName") %></asp:TableCell>
                        <asp:TableCell><asp:LinkButton runat="server" Text="Edit" CommandName="Edit" ID="LinkButton1" /></asp:TableCell>
                    </asp:TableRow>
                </asp:Table> 
    </ItemTemplate>
    <EditItemTemplate>
        <asp:Table ID="Table1" Width="100%" runat="server">         
            <asp:TableRow ID="TableRow1" runat="server">
                <asp:TableCell Width="50%">
                <asp:Table ID="Table2" Width="100%" HorizontalAlign="Center" runat="server">
                <asp:TableRow ID="TableRow2" runat="server">
                <asp:TableCell>
                <asp:Table ID="Table3" Width="100%" runat="server">  
                    <asp:TableRow ID="TableRow3" runat="server">
                        <asp:TableCell>Name:</asp:TableCell>
                        <asp:TableCell><asp:TextBox ID="txtFirstName" runat="server" Text='<%#Bind("FirstName") %>'/></asp:TableCell>
                        <asp:TableCell><asp:LinkButton runat="server" Text="Update" CommandName="Update" ID="LinkButton1" /><asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" ID="LinkButton2" CausesValidation="false" /></asp:TableCell>
                    </asp:TableRow>
                </asp:Table> 
</EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="sourceRecruit" runat="server" 
    ProviderName="System.Data.SqlClient"
    ConnectionString="<%$ ConnectionStrings:DBCS %>" 
    SelectCommand="SELECT * FROM [Players] WHERE ([ID] = @ID)" 
    UpdateCommand="UPDATE [Players] SET [FirstName] = @FirstName WHERE [ID] = @ID"> 
    <SelectParameters>
        <asp:QueryStringParameter QueryStringField="ID" Name="ID" Type="Int32" />
    </SelectParameters>
    </asp:SqlDataSource>

ASP.. NET Formview未正确更新

不是100%确定,但试着让SqlDataSource这样做,看看它是否有效:

<asp:SqlDataSource ID="sourceRecruit" runat="server" 
    ProviderName="System.Data.SqlClient"
    ConnectionString="<%$ ConnectionStrings:DBCS %>" 
    SelectCommand="SELECT * FROM [Players] WHERE ([ID] = @ID)" 
    UpdateCommand="UPDATE [Players] SET [FirstName] = @FirstName WHERE [ID] = @ID"> 
    <SelectParameters>
        <asp:QueryStringParameter QueryStringField="ID" Name="ID" Type="Int32" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="FirstName" Type="String" />
    </UpdateParameters>
</asp:SqlDataSource>

您可能还想尝试将asp:Table, asp:TableRow标签等转换为其HTML等效。我上次使用ASP已经有一段时间了。. NET,但是如果我没记错的话,当您将控件放入其他服务器控件中时,这些控件不知怎的不能很好地处理数据源。