我的GridView中的TemplateField创建如下:
<asp:TemplateField HeaderText="Dienstleistung" SortExpression="gutscheinbezeichnung" HeaderStyle-Width="20px"> <EditItemTemplate> <asp:HiddenField runat="server" Value='<%# Bind("gutscheinart_id")%>' ID="HiddenFieldGutscheinartID"/> <asp:DropDownList ID="DropDownListDienstleistung" ClientIDMode="Static" runat="server" DataSourceID="ObjectDataSourceDropDown" DataValueField="gutscheinbezeichnung"> </asp:DropDownList> <asp:ObjectDataSource ID="ObjectDataSourceDropDown" runat="server" SelectMethod="GetGutscheinArt" TypeName="Gmos.Halbtax.Admin.Client.WebGui.DataManager"></asp:ObjectDataSource> </EditItemTemplate> <ItemTemplate> <asp:Label ID="LabelGutscheinbezeichnung" runat="server" Text='<%# Bind("gutscheinbezeichnung") %>'></asp:Label> </ItemTemplate> <HeaderStyle Width="20px" /> </asp:TemplateField>
如您所见,我的EditItemTemplate-Field中有一个名为DropDownListDienstleitung的DropDownList.
我也创建了这个活动:
protected void GridViewLehrling_RowDataBound(object sender,GridViewRowEventArgs e) { DropDownList DropDownListDienstleistungBackEnd = (DropDownList)GridViewLehrling.Rows[GridViewLehrling.Selectedindex].FindControl("DropDownListDienstleistung"); HiddenField HiddenFieldGutscheinartIDBackEnd = (HiddenField)GridViewLehrling.Rows[GridViewLehrling.EditIndex].FindControl("HiddenFieldGutscheinartID"); }
现在,如果这个事件发生了.发生此错误:
Index was out of range. Must be non-negative and less than the size of
the collection. Parameter name: index
有什么建议?
解决方法
尝试使用以下代码:
protected void GridViewLehrling_RowDataBound(object sender,GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DaTarow) { if (e.Row.RowState == DataControlRowState.Edit) { DropDownList ddlBackEnd = (DropDownList)e.Row.FindControl("DropDownListDienstleistung"); HiddenField hdnBackEnd = (HiddenField)e.Row.FindControl("HiddenFieldGutscheinartID"); } } }
代码首先检查行的type.它必须是DaTarow,以便排除页脚和标题行.然后代码检查该行是否实际处于编辑模式.如果是这种情况,那么代码将获取在实际行上执行FindControl的控件.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。