开发者

DetailsView: Index was out of range

I'm trying to get the datakey value from DetailsView and paste it in a form. I've included all the datakeynames but still couldn't get the value to paste it in my formview but I've encountered this probles:

Index was out of range. Must be non-negative and less than 开发者_开发问答the size of the collection. Parameter name: index

I've tried SelectedValue and SelectedRow but it only retrieve the primary keys.

Attached here is my code-behind. Help would be much appreciated!

Thanks in advance.

protected void FormView1_DataBound(object sender, EventArgs e)
{
    if(FormView1.CurrentMode == FormViewMode.Insert)
    {
        TextBox bookid = FormView1.FindControl("bookidTextBox") as TextBox;
        bookid.Text = DetailsView1.DataKey[1].ToString();

        TextBox employee = FormView1.FindControl("EmployeeID") as TextBox;
        employee.Text = DetailsView1.DataKey[2].ToString();
    }
}

Heres the code from the control

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
            DataKeyNames="reservationid,bookid,EmployeeID,reservedate" DataSourceID="booklendingDataSource" Height="50px" 
            Width="300px" >
            <Fields>

                <asp:CommandField ButtonType="Button" ShowSelectButton="true" SelectText="Lend" />
                <asp:BoundField DataField="reservationid, bookid, EmployeeID" HeaderText="reservationid" 
                    InsertVisible="False" ReadOnly="True" SortExpression="reservationid" 
                    Visible="false" />

                <asp:BoundField DataField="bookid" HeaderText="bookid" 
                    Visible="false" SortExpression="bookid" />

                <asp:BoundField DataField="booktitle" HeaderText="Title" 
                    SortExpression="booktitle" />

                <asp:BoundField DataField="EmployeeID" HeaderText="Emp PIN" 
                    SortExpression="EmployeeID" />

                <asp:BoundField DataField="reservedate" HeaderText="Reserve date" 
                    SortExpression="reservedate" />

                <asp:CheckBoxField DataField="isdeleted" HeaderText="Deleted" 
                    SortExpression="isdeleted" />

            </Fields>
        </asp:DetailsView>
...

<asp:FormView ID="FormView1" runat="server" DataKeyNames="lenid" 
            DataSourceID="lendformDataSource" DefaultMode="Insert" OnDataBound="FormView1_DataBound" >
            <EditItemTemplate>

        ...

            </EditItemTemplate>
            <InsertItemTemplate>
                bookid:
                <asp:TextBox ID="bookidTextBox" runat="server" 
                    Text='<%# Bind("bookid") %>' />
                <br />
                EmployeeID:
                <asp:TextBox ID="EmployeeIDTextBox" runat="server" 
                    Text='<%# Bind("EmployeeID") %>' />
                <br />
                department:
                <asp:TextBox ID="departmentTextBox" runat="server" 
                    Text='<%# Bind("department") %>' />
                <br />
                dateborrowed:
                <asp:TextBox ID="dateborrowedTextBox" runat="server" 
                    Text='<%# Bind("dateborrowed") %>' />
                <br />
                expdateofreturn:
                <asp:TextBox ID="expdateofreturnTextBox" runat="server" 
                    Text='<%# Bind("expdateofreturn") %>' />
                <br />

                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
                    CommandName="Insert" Text="Insert" />
                &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" 
                    CausesValidation="False" CommandName="Cancel" Text="Cancel" />
            </InsertItemTemplate>
            <ItemTemplate>


This is an better way to access the values of DataItem

bookid.Text = ((DataRowView)DetailsView1.DataItem)["bookid"].ToString();
employee.Text = ((DataRowView)DetailsView1.DataItem)["EmployeeID"].ToString();


Are you sure your DetailsView1.DataKey[1] not starts at [0]?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜