我正在尝试通过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>
的SqlDataSource:
<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">
但是,当我点击插入按钮时,没有任何反应 - 似乎有一个回发,但是没有任何条目进入我的表格(我打入页面的值仍然在文本框中)
答案 0 :(得分:4)
我刚刚为您做了一个完整的工作示例,在这种情况下,我创建了一个列表来模拟主 - 详细信息场景:(我正在使用 PUBS 数据库示例)
<asp:SqlDataSource runat="server" ID="sqds"
ConnectionString="<%$ ConnectionStrings: ds %>"
DataSourceMode="DataSet"
SelectCommand="select * from jobs"
>
</asp:SqlDataSource>
<asp:GridView runat="server" DataSourceID="sqds" ID="gv" DataKeyNames="job_id">
<Columns>
<asp:CommandField ShowSelectButton="true" />
</Columns>
<SelectedRowStyle Font-Bold="true" />
</asp:GridView>
<asp:SqlDataSource runat="server" ID="sqds2"
ConnectionString="<%$ ConnectionStrings: ds %>"
DataSourceMode="DataSet"
SelectCommand="select * from jobs where job_id = @JobID"
InsertCommand="insert into jobs (job_desc, min_lvl, max_lvl) values (@job_desc, @min_lvl, @max_lvl)"
>
<SelectParameters>
<asp:ControlParameter ControlID="gv" DefaultValue="0" Name="JobID" PropertyName="SelectedValue" Type="Int16" />
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="job_desc" Type="String" />
<asp:Parameter Name="min_lvl" Type="Int16" />
<asp:Parameter Name="max_lvl" Type="Int16" />
</InsertParameters>
</asp:SqlDataSource>
<asp:FormView runat="server" ID="fv" DataSourceID="sqds2" DefaultMode="Insert"
oniteminserted="fv_ItemInserted">
<ItemTemplate>
<b>Job Description:</b> <asp:Label Text='<%# Eval("job_desc") %>' runat="server" />
<asp:Button Text="New" runat="server" CommandName="New" />
</ItemTemplate>
<InsertItemTemplate>
<div>
<b>Job Description</b>: <asp:TextBox ID="jobDescription" runat="server" Text='<%# Bind("job_desc") %>' />
</div>
<div>
<b>Minimum level</b>: <asp:TextBox ID="minimumLevel" runat="server" Text='<%# Bind("min_lvl") %>' />
</div>
<div>
<b>Maximum level</b>: <asp:TextBox ID="maximumLevel" runat="server" Text='<%# Bind("max_lvl") %>' />
</div>
<asp:Button Text="Insert" runat="server" CommandName="Insert" />
</InsertItemTemplate>
</asp:FormView>
protected void fv_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
this.gv.DataBind();
}
答案 1 :(得分:0)
您需要查看使用FormView1_ItemCommand
请参阅此处的示例代码
http://www.codeproject.com/Articles/43780/Taming-the-FormView-Understanding-how-to-Implement