开发者

Get value from DetailsView to FormView Textbox in insert mode

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>
开发者_如何学JAVA                    <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.


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜