我正在尝试通过 Formview 将数据插入到我的数据库中:
<asp:FormView ID="FormView3" runat="server" DataSourceID="SqlDataSource2" RenderOuterTable="False"
DefaultMode="Insert">
<InsertItemTemplate>
Name:
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
<br />
E-mail:
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox>
<br />
Website:
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Website") %>'></asp:TextBox>
<br />
Comment:
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Comment") %>'></asp:TextBox>
<br />
<asp:Button ID="postcomment" runat="server" Text="Button" OnClick="postcomment_Click" />
</InsertItemTemplate>
</asp:FormView>
我尝试创建一个要插入的按钮,这是我的事件:
protected void postcomment_Click(object sender, EventArgs e)
{
FormView3.InsertItem(true);
}
但这似乎没有做任何事情。我究竟做错了什么?我只有使用其他自动为我构建插入/取消的数据绑定控件的经验。
编辑:
这是我在遵循 Jupaol 的建议后得到的:
<asp:FormView ID="FormView3" runat="server" DataSourceID="SqlDataSource4" RenderOuterTable="False" DefaultMode="Insert">
<InsertItemTemplate>
Name:
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
<br />
E-mail:
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox>
<br />
Website:
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Website") %>'></asp:TextBox>
<br />
Comment:
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Comment") %>'></asp:TextBox>
<br />
pid:
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("PostId") %>'></asp:TextBox>
<br />
<asp:Button ID="postcomment" runat="server" Text="Button" CommandName="Insert" />
</InsertItemTemplate>
</asp:FormView>
SQL 数据源:
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:orangefreshConnectionString1 %>"
DeleteCommand="DELETE FROM [Comment] WHERE [Id] = @Id"
InsertCommand="INSERT INTO [Comment] ([Name], [Email], [Website], [Comment], [PostId]) VALUES (@Name, @Email, @Website, @Comment, @PostId)"
SelectCommand="SELECT [Name], [Email], [Website], [Comment], [PostId], [Id] FROM [Comment]"
UpdateCommand="UPDATE [Comment] SET [Name] = @Name, [Email] = @Email, [Website] = @Website, [Comment] = @Comment, [PostId] = @PostId WHERE [Id] = @Id">
尽管如此,当我单击插入按钮时,什么也没有发生 - 似乎有回发,但没有条目进入我的表格(我在页面中打孔的值仍在文本框中)