How do I reference it so that the data can be loaded dynamically in the RadListBox

1 Answer 86 Views
UI for ASP.NET AJAX in ASP.NET MVC
Oscar Ivan
Top achievements
Rank 1
Oscar Ivan asked on 26 Dec 2022, 01:42 PM | edited on 29 Dec 2022, 08:13 AM
Hello,

I have a RadListBox called RadListBoxDestination inside the controls <EditFormSettings EditFormType="Template">
             <FormTemplate> (file wucMantenedorVentasPriorizadas.ascx)

What happens is that when I try to call RadListBoxDestination ma I get the following error message:
"Object reference not set to an instance of an object"

For me the problem is that my RadLisBox called RadListBoxDestination is inside the controls <EditFormSettings EditFormType="Template">
             <FormTemplate> and I'm not calling it correctly.

For this I have been doing 2 possible logics but I get the same error, these logics are (file wucMantenedorVentasPriorizadas,ascx.cs):

Option 1

  logger.Info("RadListBoxDestination: " + RadListBoxDestination);
                 RadListBoxDestination.DataSource = AGAgents.Select(a => a.firstname + " " + a.lastname);
                 RadListBoxDestination.DataBind();

Option 2

if (e.Item is GridEditFormItem && e.Item.IsInEditMode)//editform
                 {
                     GridEditFormItem editItem = (GridEditFormItem)e.Item;
                     RadListBox RadListBoxDestination = (RadListBox)editItem.FindControl("RadListBoxDestination");
                     RadListBoxDestination.DataSource = AGAgents.Select(a => a.firstname + " " + a.lastname);
                     RadListBoxDestination.DataBind();
                 }

Can you help me how is that How do I reference it so that the data can be loaded dynamically since I put a log to see but no data appears in the RadListBox

Regards
Oscar

1 Answer, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 29 Dec 2022, 08:37 AM

Hi Oscar,

The ItemCommand event is too early for binding data to Controls inside the Grid. It is also inappropriate since the event will trigger multiple actions (Filter, Sort, Paging, etc..). Use the ItemCommand event only when you need to capture an event for a specific Command. See How to Fire Command Events and ItemCommand Event articles.

 

To bind data to Controls in the Grid, use the ItemDataBound event. This event will only trigger once when data is bound to items. It does not trigger on every postback and every command, thus making the code more efficient and performance friendly.

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    // If the event is triggered for the Edit Item (GridEditFormItem, GridEditFormInsertItem, GridDataInsertItem, Form template, etc..)
    if (e.Item.IsInEditMode)
    {
        // Cast the item to a Generic Type (GridEditableItem)
        GridEditableItem editItem = (GridEditableItem)e.Item;

        // find the ListBox in the editItem
        RadListBox rlbDestination = editItem.FindControl("RadListBoxDestination") as RadListBox;

        rlbDestination.DataSource = SomeDataSourceHere();
        rlbDestination.DataBind();
    }
}

For more details, please refer to the Accessing Controls in RadGrid article.

 

You will also need to review the rest of the code and make sure there are no JS errors as they have the potential to stop the Telerik components from working.

More specifically, there is one JS trying to gain access to the ListBox on the client side and call a method that is only available for RadWindows.

 

Regards,
Attila Antal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
UI for ASP.NET AJAX in ASP.NET MVC
Asked by
Oscar Ivan
Top achievements
Rank 1
Answers by
Attila Antal
Telerik team
Share this question
or