在插入模式下从DetailsView获取值到FormView文本框

时间:2011-05-04 05:34:59

标签: c# asp.net detailsview formview

我正在尝试从detailsview获取值到formview。我想将formview插入文本框中的图书ID / ISBN设置为详细信息视图中的图书ID / ISBN值。

以下是我的代码示例:

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
            CellPadding="4" DataKeyNames="bookid" DataSourceID="detailsDataSource" 
            ForeColor="#333333" GridLines="None" Height="50px" Width="">
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />
            <RowStyle BackColor="#EFF3FB" />
            <FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <FooterTemplate>
                <asp:GridView ID="GridView2" runat="server" AllowPaging="True" 
                    AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
                    DataKeyNames="reservationid" DataSourceID="reserveDataSource" 
                    ForeColor="#333333" GridLines="None">
                    <RowStyle BackColor="#EFF3FB" />
                    <Columns>
                        <asp:BoundField DataField="EmployeeID" HeaderText="Reserved by" 
                            SortExpression="EmployeeID" />
                        <asp:BoundField DataField="reservedate" HeaderText="Reserved date" 
                            SortExpression="reservedate" />
                    </Columns>
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <EditRowStyle BackColor="#2461BF" />
                    <AlternatingRowStyle BackColor="White" />
                </asp:GridView>
                <asp:FormView ID="FormView1" runat="server" DataKeyNames="reservationid" 
                    DataSourceID="reserveDataSource">
                    <EditItemTemplate>
                        reservationid:
                        <asp:Label ID="reservationidLabel1" runat="server" 
                            Text='<%# Eval("reservationid") %>' />
                        <br />
                        bookid:
                        <asp:TextBox ID="bookidTextBox" runat="server" Text='<%# Bind("bookid") %>' />
                        <br />
                        EmployeeID:
                        <asp:TextBox ID="EmployeeIDTextBox" runat="server" 
                            Text='<%# Bind("EmployeeID") %>' />
                        <br />
                        reservedate:
                        <asp:TextBox ID="reservedateTextBox" runat="server" 
                            Text='<%# Bind("reservedate") %>' />
                        <br />
                        <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 
                            CommandName="Update" Text="Update" />
                        &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" 
                            CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        Book ID/ISBN:
                        <asp:TextBox ID="bookidTextBox" runat="server" 
                        Text='<%# Bind("bookid") %>'/>
                        <br />
                        Employee ID:
                        <asp:TextBox ID="EmployeeIDTextBox0" runat="server" 
                            Text='<%# Bind("EmployeeID") %>' />
                        <br />
                        Reserve date:
                        <asp:TextBox ID="reservedateTextBox0" runat="server" 
                            Text='<%# Bind("reservedate") %>' />
                        <br />
                        <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
                            CommandName="Insert" Text="Reserve" />
                        &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" 
                            CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                    </InsertItemTemplate>

所以我要做的就是当用户点击插入/预约图书链接按钮时,图书馆身份证/ ISBN已经在formview插入模式中设置为图书ID / ISBN作为默认设置。

非常感谢任何帮助;)

提前致谢。

1 个答案:

答案 0 :(得分:1)

您可以尝试查看DetailsView1.SelectedValue

<asp:TextBox ID="bookidTextBox" runat="server" 
                    Text='<%# DetailsView1.SelectedValue %>'/>

修改 现在,如果要将该值绑定到DetailsView的Insert函数,它将不会像其他控件那样运行,因为您将Bind method用于其他控件,并且它提供了双向绑定。

现在您需要将该值传递给插入DetailsView的evet,就像我们使用DetailsView1.SelectedValue分配值但不绑定值一样。

我希望你理解这个理论。

protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
    e.Values["bookid"] = ((TextBox)DetailsView1.FindControl("bookidTextBox")).Text;
}