FindControl in RowDataBound-Event fails [still not resolved]

Mine TemplateFieldin mine GridViewwas created as follows:

<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>

As you can see, I have DropDownList, called DropDownListDienstleitungin my EditItemTemplate-Field.

I also created this event:

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

Now, if this event fires. This error occurs:

The index was out of range. Must be non-negative and smaller than the size of the collection. Parameter Name: Index

Any suggestions?

+4
source share
2 answers

Try using the following code snippet:

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

. DataRow, . , . , , FindControl .

0

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

Source: https://habr.com/ru/post/1656571/


All Articles