2

我正在尝试通过 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">

尽管如此,当我单击插入按钮时,什么也没有发生 - 似乎有回发,但没有条目进入我的表格(我在页面中打孔的值仍在文本框中)

4

2 回答 2

4

我刚刚为您做了一个完整的工作示例,在这种情况下,我创建了一个列表来模拟主从场景:(我正在使用PUBS数据库示例)

输出

在此处输入图像描述

ASPX

    <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>

ASPX 代码背后

    protected void fv_ItemInserted(object sender, FormViewInsertedEventArgs e)
    {
        this.gv.DataBind();
    }
于 2012-08-12T17:21:56.533 回答
0

您需要查看使用 FormView1_ItemCommand

在此处查看示例代码

http://www.codeproject.com/Articles/43780/Taming-the-FormView-Understanding-how-to-Implement

于 2012-08-12T15:50:19.483 回答