【问题标题】:Can't get Formview to update, what am I doing wrong?无法让 Formview 更新,我做错了什么?
【发布时间】:2009-07-30 20:55:07
【问题描述】:

我在 asp.net 中使用 Formview。我知道需要指定 UpdateCommand 以便 Formview 知道要运行什么 SQL 查询。

为了正确触发 SQL 查询,我是否需要在 Formview 设置的代码之外编写一个事件

此外,我没有收到 SQL 错误或任何东西,只是没有绑定回数据库。

<EditItemTemplate>
        ProductID:
        <asp:Label ID="ProductIDLabel1" runat="server" 
            Text='<%# Eval("ProductID") %>' />
        <br />
        ProductName:
        <asp:TextBox ID="ProductNameTextBox" runat="server" 
            Text='<%# Bind("ProductName") %>' />
        <br />
        UnitPrice:
        <asp:TextBox ID="UnitPriceTextBox" runat="server" 
            Text='<%# Bind("UnitPrice") %>' />
        <br />
        <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 
            CommandName="Update" Text="Update" />
        &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" 
            CausesValidation="False" CommandName="Cancel" Text="Cancel" />
    </EditItemTemplate>

【问题讨论】:

  • 您的表单视图在更新面板中吗?

标签: asp.net data-binding formview


【解决方案1】:

确保设置 DataKeys 属性(通常设置为主键的列)。如果您需要更多帮助,您需要发布您的代码。

【讨论】:

  • DataKeyName 设置为 ProductID,用户从 DDL 中选择一个选项,该 DDL 加载带有所选产品详细信息的表单视图。 ASP.net 新手在这里,所以任何帮助表示赞赏。
  • 您发布的内容看起来不错,但我们看不到您是如何绑定到数据源的。看看这个参考 MSDN 示例,看看您是否能发现任何差异:msdn.microsoft.com/en-us/library/ms227970.aspx 祝你好运。
【解决方案2】:

我遇到了类似的问题,然后我研究了 MSDN,它说使用 UpdateCommand 和 SqlDataSource,并且它有效。
这是我的代码:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:conString %>"
      SelectCommand="SELECT DISTINCT * FROM [employees] WHERE ([username] = @username)"
      UpdateCommand="UPDATE [employees] SET first_name = @first_name WHERE username = @username">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 1970-01-01
    相关资源
    最近更新 更多