Get value from DetailsView to FormView Textbox in insert mode

情到浓时终转凉″ 提交于 2019-12-11 07:35:30

问题


I'm trying to get the value from detailsview to formview. I want to set the Book ID/ISBN in formview insert textbox to Book ID/ISBN value from detailsview.

Here's a sample of my code:

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

So what I'm trying to do is when a user click the Insert/Reserve book linkbutton the Book ID/ISBN is already set to the Book ID/ISBN in formview insert mode as default.

Any help would be much appreciated ;)

Thanks in advance.


回答1:


Can you try and check the DetailsView1.SelectedValue

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

Edit: Now if you want to bind that value to your Insert function of your DetailsView, it will not behave like the other control, since you are using the Bind method for the other control and it provides two way binding.

Now you need to pass that value to inserting evet of DetailsView like as we are assigning values using DetailsView1.SelectedValue but not binding the value.

I hope you understand this theory.

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


来源:https://stackoverflow.com/questions/5879034/get-value-from-detailsview-to-formview-textbox-in-insert-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!