el_pup_le
el_pup_le

Reputation: 12179

WebControl with ID not exist in code behind context

I have this in a grid view:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MyViewTemplate.ascx.cs" Inherits="usercontrols_MyViewTemplate" %>

    ...

<asp:TemplateField HeaderText="Estado" ItemStyle-Width="50px">
  <ItemTemplate>
    <asp:DropDownList ID="RiskWorkDropDownList" runat="server">
    </asp:DropDownList>
  </ItemTemplate>
</asp:TemplateField>

And the RiskWorkDropDownList does not exist in the code behind context. Why doesn't it exist in the code behind?

Upvotes: 0

Views: 82

Answers (1)

Hakunamatata
Hakunamatata

Reputation: 1275

It is nested within the GridView that's why it is not accessible directly. You needs to do something like below

foreach(GridViewRow row in grdView.Rows)
{
       if(row.FindControl("RiskWorkDropDownList")!=null)
       {
              DropDownList ddlRisk = (DropDownList)row.FindControl("RiskWorkDropDownList");
        }
}

Upvotes: 2

Related Questions