【问题标题】:FindControl in RowDataBound-Event ends in errorRowDataBound-Event 中的 FindControl 以错误结束
【发布时间】:2017-02-11 07:29:38
【问题描述】:

我的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 中有一个名为DropDownListDienstleitungDropDownList

我也创建了这个活动:

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

现在,如果此事件触发。发生此错误:

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

有什么建议吗?

【问题讨论】:

    标签: c# asp.net findcontrol


    【解决方案1】:

    尝试使用以下代码:

    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 的控件。

    【讨论】:

    • 正是我搜索的内容。 THX
    【解决方案2】:

    您在标题中找不到下拉控件,因此您需要检查当前行是否datarow

    试试这个。

    protected void GridViewLehrling_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (GridViewLehrling.Rows.Count > 0)
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        DropDownList DropDownListDienstleistungBackEnd = (DropDownList)GridViewLehrling.Rows[GridViewLehrling.SelectedIndex].FindControl("DropDownListDienstleistung");
                        HiddenField HiddenFieldGutscheinartIDBackEnd = (HiddenField)GridViewLehrling.Rows[GridViewLehrling.EditIndex].FindControl("HiddenFieldGutscheinartID");
                    }
                }
            }
    

    【讨论】:

    • 对我不起作用。错误仍然相同。桑克斯
    猜你喜欢
    • 1970-01-01
    • 2011-11-12
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多