ItemTemplate中的asp.net文本框,更新sqldatasource

时间:2012-05-12 22:49:43

标签: asp.net sqldatasource itemtemplate

我是asp.net的初学者。我想更新数据库中的数据。但没有任何反应。我使用谷歌,发现问题可以通过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>

1 个答案:

答案 0 :(得分:0)

您可以在此处使用SqlDatasource而不会出现任何问题,您甚至不必担心UpdateParameters - 只需向EditItemTemplate和{{1}添加GridView即可你的专栏:

Bind()

请注意,我确实将<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,将true设置为false。