ASP.NET Formview无法正确更新

时间:2011-10-07 22:15:30

标签: c# asp.net

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

我正在尝试更新formview中的记录,每次执行时,都会出现以下错误:“必须声明标量变量”@FirstName“”。

我用Google搜索并搜索,看不出我做错了什么。我确信我做错了一些事情。

      <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>

1 个答案:

答案 0 :(得分:1)

不是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:Tableasp:TableRow代码等转换为其HTML等效代码。自从我上次使用ASP.NET以来已经有一段时间了,但是如果我没记错的话,当你将它们放在其他服务器控件中时,这些控件在某种程度上不能很好地与数据源配合使用。