Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DetailsView:索引超出范围_C#_.net_Asp.net_Detailsview - Fatal编程技术网

C# DetailsView:索引超出范围

C# DetailsView:索引超出范围,c#,.net,asp.net,detailsview,C#,.net,Asp.net,Detailsview,我正在尝试从DetailsView获取数据键值,并将其粘贴到表单中。我已经包含了所有的DataKeyName,但仍然无法获取将其粘贴到我的formview中的值,但我遇到了以下问题: 索引超出范围。必须是 非负数且小于 收藏。参数名称:索引 我尝试了SelectedValue和SelectedRow,但它只检索主键 附件是我的密码。非常感谢您的帮助 提前谢谢 protected void FormView1_DataBound(object sender, EventArgs e) {

我正在尝试从
DetailsView
获取数据键值,并将其粘贴到表单中。我已经包含了所有的DataKeyName,但仍然无法获取将其粘贴到我的formview中的值,但我遇到了以下问题:

索引超出范围。必须是 非负数且小于 收藏。参数名称:索引

我尝试了
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>

...
...
图书编号:

雇员ID:
部门:
借用日期:
返回日期:

您确定您的详细信息视图1.DataKey[1]不是从[0]开始的吗?

这是访问
数据项的值的更好方法

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

你到底在哪里得到了异常?在哪一行抛出了异常?我的意思是,索引从零开始,所以你可能没有索引2…我已经编辑了我的帖子,请看一看。我只想获取数据键值'bookid'、'EmployeeID'和'reservedate',并将其粘贴到FormView中。编辑您的问题后,我已经编辑了我的答案。只要试一下,你就会看到魔术:)是的,因为我只需要里面的其他数据,而不是主键。在我的表中,我有一个主键,一个bookid,一个employeeid,所以我想得到bookid和employeeid。@Loupi;你有两个数据键吗?你能告诉我你提到的来源吗?我已经编辑了我的帖子。请看一看。我需要检索三个datakey值'bookid'、'EmployeeID'和'reservedate'时出错:对象引用未设置为对象的实例。我会像往常一样尝试修复它。请进行空检查。直接对检索到的值调用ToString从来都不是一个好主意。。它也可以为null。。