0

我是asp.net的初学者。我想更新数据库中的数据。但什么也没有发生。我用google,发现问题可以通过ObjectDataSource解决,但不确定。

 <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    SelectCommand="SELECT * FROM [content]" UpdateCommand="UPDATE [content] SET [title] = @title, [article] = @article WHERE [id] = @id">
    <UpdateParameters>
        <asp:ControlParameter ControlID="txtTitle" Name="title" Type="String" PropertyName="Text" />
        <asp:ControlParameter ControlID="txtArticle" Name="article" Type="String" PropertyName="Text" />
        <asp:Parameter Name="id" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>
<asp:DataList ID="DataList2" runat="server" DataKeyField="id" DataSourceID="SqlDataSource1">
    <ItemTemplate>
        <asp:TextBox ID="txtTitle" runat="server" Text='<%# Eval("title") %>'></asp:TextBox><br>
        <asp:TextBox ID="txtArticle" runat="server" Text='<%# Eval("article") %>'></asp:TextBox><br>
        <asp:Button ID="Button1" runat="server" Text="go" />
    </ItemTemplate>
</asp:DataList>
4

1 回答 1

0

您可以SqlDatasource在这里毫无问题地使用,甚至不必费心UpdateParameters——只需在EditItemTemplateGridViewBind()您的列中添加一个:

<asp:SqlDataSource ID="SqlDataSource2" runat="server"
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    SelectCommand="SELECT * FROM [content]"
    UpdateCommand="UPDATE [content] SET [title] = @title, [article] = @article WHERE [id] = @id" />

<asp:DataList ID="DataList2" runat="server"
  DataKeyField="id" DataSourceID="SqlDataSource1"
  AutoGenerateColumns="false" AutoGenerateEditButton="true">

    <ItemTemplate>
        <asp:Label ID="lblTitle" runat="server" Text='<%# Eval("title") %>' /><br/>
        <asp:Lable ID="lblArticle" runat="server" Text='<%# Eval("article") %>' /><br/>
    </ItemTemplate>

    <EditItemTemplate>
        <asp:TextBox ID="txtTitle" runat="server" Text='<%# Bind("title") %>' /><br/>
        <asp:TextBox ID="txtArticle" runat="server" Text='<%# Bind("article") %>' /><br/>
    </EditItemTemplate>

请注意,我确实设置为AutoGenerateEditButton假。trueAutoGenerateColumns

于 2012-05-14T10:25:53.407 回答