DetailsView:索引超出范围

时间:2011-05-31 05:28:25

标签: c# .net asp.net detailsview

我正在尝试从DetailsView获取datakey值并将其粘贴到表单中。我已经包含了所有的datakeynames,但仍然无法将值粘贴到我的formview中,但我遇到了这个问题:

  

指数超出范围。一定是   非负和小于的大小   集合。参数名称:index

我尝试了SelectedValue和SelectedRow,但它只检索主键。

这里附有我的代码隐藏。非常感谢帮助!

提前致谢。

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();
    }
}

来自控件的代码

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

2 个答案:

答案 0 :(得分:1)

这是访问DataItem

值的更好方法
bookid.Text = ((DataRowView)DetailsView1.DataItem)["bookid"].ToString();
employee.Text = ((DataRowView)DetailsView1.DataItem)["EmployeeID"].ToString();

答案 1 :(得分:0)

你确定你的DetailsView1.DataKey [1]不是从[0]开始吗?