我正在尝试从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" />
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
答案 0 :(得分:1)
这是访问DataItem
bookid.Text = ((DataRowView)DetailsView1.DataItem)["bookid"].ToString();
employee.Text = ((DataRowView)DetailsView1.DataItem)["EmployeeID"].ToString();
答案 1 :(得分:0)
你确定你的DetailsView1.DataKey [1]不是从[0]开始吗?