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

本文关键字:FormView 文本 获取 DetailsView 插入 模式 | 更新日期: 2023-09-27 17:54:28

我试图从detailsview到formview的值。我想将formview插入文本框中的Book ID/ISBN设置为detailsview中的Book 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>

所以我要做的是当用户单击插入/预订图书链接按钮时,图书ID/ISBN已经在formview插入模式中默认设置为图书ID/ISBN。

任何帮助都将非常感激;)

提前感谢。

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

你能不能试着检查一下DetailsView1.SelectedValue

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

现在,如果您想将该值绑定到DetailsView的Insert函数,则它的行为将不像其他控件,因为您正在为其他控件使用Bind method,并且它提供了双向绑定。

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

我希望你能理解这个理论。

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