none
Why Can I not Access This Control RRS feed

  • Question

  • I am using a Telerik RadGrid and have an ASP DropDownList within an EditItemTemplate as shown in the code below.

    <telerik:GridTemplateColumn UniqueName="TemplateColumn" HeaderText="Role">
                    <ItemTemplate>
                        <asp:Label ID="lblRole" runat="server"
                            Text='<%# DataBinder.Eval(Container.DataItem, "Role") %>'>
                        </asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddl1" runat="server" UniqueName="Roles"/>
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>

    I am attempting to populate the DropDown using this C# code.

    protected void gvMembers_ItemDataBound(object sender, GridItemEventArgs e)
            {
                var roles = (from c in DbContext.roles
                             select new { c.Role1, c.RoleID }).ToList();
                GridEditableItem item = e.Item as GridEditableItem;
    
                //// access/modify the edit item template settings here
                DropDownList list = item.FindControl("Roles") as DropDownList;
                list.DataTextField = "Role1";
                list.DataValueField = "RoleID";
                list.DataBind();
    
            }

    I keep getting an "Object reference not set to an instance of an object" error message when I run the code.  I am Jr. Programmer and just can't seem to figure out what is causing the error message and what the fix might be.  I am assuming the code is not finding the ASP control.  I have attempted calling the code using the ID rather then the name but I get the same results.

    Any help would be appreciated.

    • Moved by CoolDadTx Friday, September 15, 2017 2:10 PM ASP.NET related
    Friday, September 15, 2017 12:12 PM

All replies