0

I am using a Telerik RadGrid and using an EditItemTemplate as shown in my ASP 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 Entity Framework with 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 am getting an "Object reference not set to an instance of an object." Since I am relatively new to programming I am stumped by this error message it appears to me I am not able to find that Roles control. I have attempted to use the Control ID put get the same results. I have spent a lot of time trying to solve this problem so any help with this would be great.

Perry
  • 1,277
  • 2
  • 17
  • 39
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – mrogal.ski Sep 15 '17 at 12:19
  • On what line do you get the error? – Darren Sep 15 '17 at 12:26
  • @EmptyWaterHoles I get the error message on the line That begins with DropDownList – Perry Sep 15 '17 at 12:42

2 Answers2

0

I am not sure what you have got as the sender to this method. But for example if it was a button you could replace this line

GridEditableItem item = e.Item as GridEditableItem;

with this line

Button item = (Button) sender;

and this line

DropDownList list = item.FindControl("Roles") as DropDownList;

with this line

DropDownList list = item.NamingContainer.FindControl("Roles") as DropDownList;

because item is null you can not use it with .FindControl

Replace Button with what ever is calling the method

Darren
  • 1,352
  • 5
  • 19
  • 49
  • the sender is the ItemDataBound it fires as the giridview is being rendered. I changed the code to what you recommended but got the same error message – Perry Sep 15 '17 at 13:09
0

I have figured out the issue. The problem was the code was being run at the wrong time. I was attempting to access a control in the EditItemTemplate which is only available when the item is in editmode. After changing my code to check the mode it now works. Here is the corrected code

protected void gvMembers_ItemDataBound(object sender, GridItemEventArgs e)
    {

        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            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("List1") as DropDownList;
            list.DataSource = roles;
            list.DataBind();
        }

    }
Perry
  • 1,277
  • 2
  • 17
  • 39