This is a migrated thread and some comments may be shown as answers.

ListBox not being found in EditForm

5 Answers 169 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kyle
Top achievements
Rank 1
Kyle asked on 02 Apr 2012, 03:48 PM
I have a ListBox on my EditForm that is populated based on the CommandArgument from the ItemCommand. The data is being accessed, but when I go to add the data to the ListBox, the control isn't being found. Here's what I have: 

if (e.CommandName == "Edit") {
                GridEditFormItem item = (GridEditFormItem)(e.Item as GridDataItem).EditFormItem;
                ListBox lb = (ListBox)item.FindControl("lstMembers");

the ListBox is null, the control is named "lstMembers" (I already checked that) my suspicion is something in the previous line of code. When I put the same two lines in another block (different command name) it works fine, so I think it might have something to do with the fact that the control itself isn't loaded yet. Can someone confirm/clarify this for me, and if so give me a work around?

5 Answers, 1 is accepted

Sort by
0
Elliott
Top achievements
Rank 2
answered on 02 Apr 2012, 04:59 PM
the listbox isn't in the item - it's actually in a table cell within the item
you need to find the right tablecell (column) then pull your listbox out of that
I'm not going to include code because I "cheat" - reference the tablecell by hardcoding the index
0
Kyle
Top achievements
Rank 1
answered on 02 Apr 2012, 05:01 PM
If that's the case then why does it work elsewhere? i.e. why does it work in the "View" command but not the "Edit?" And could you post some of your code so I can kinda see the general idea of what you do?
0
Elliott
Top achievements
Rank 2
answered on 02 Apr 2012, 05:10 PM
can't answer that (sorry)
it's a dumb question but...
when the CommandName is "Edit" what is the type of the e.Item?

I would still try something like
if (e.CommandName == "Edit" && typeof e.Item is EditFormItem) // syntax may be wrong, I'm a VB programmer
{
     GridEditFormItem item = (GridEditFormItem)(e.Item as GridDataItem).EditFormItem;
     TableCell TC = item.TableCell[5];
     ListBox lb = (ListBox)TC.FindControl("lstMembers");
}
0
Kyle
Top achievements
Rank 1
answered on 02 Apr 2012, 05:39 PM
Tried that, still didn't work. item doesn't have a TableCell[] property, so I had to just use Cells. That might just be a VB/C# thing though. 
TC, which I set to be Cells[1] (corresponds to the location of the form in my Grid) appears to have no controls, as confirmed in a breakpoint.  e.Item is a GridDataItem, even when the CommandName is Edit, that's why I have to 1) extract the EditFormItem, and 2)cast it as a GridEditFormItem. 
0
Shinu
Top achievements
Rank 2
answered on 03 Apr 2012, 05:26 AM
Hello Kyle,

Since the EditCommand is too early to access the edit form items, you can access the items in the ItemDataBound event of the grid as shown below.
C#:
protected void grid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
 if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
 {
   GridEditFormItem item = (GridEditFormItem)e.Item;
   RadListBox list = (RadListBox)item.FindControl("lstMembers");
 }
}

Thanks,
Shinu.
Tags
Grid
Asked by
Kyle
Top achievements
Rank 1
Answers by
Elliott
Top achievements
Rank 2
Kyle
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or