加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – RowDataBound中的FindControl – 事件以错误结束

发布时间:2020-12-15 21:02:22 所属栏目:百科 来源:网络整理
导读:我的GridView中的TemplateField创建如下: asp:TemplateField HeaderText="Dienstleistung" SortExpression="gutscheinbezeichnung" HeaderStyle-Width="20px" EditItemTemplate asp:HiddenField runat="server" Value='%# Bind("gutscheinart_id")%' ID="Hi
我的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的控件.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读