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

[Solved] Problem with rebinding Listbox inside TemplateColumn

2 Answers 147 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tobias
Top achievements
Rank 1
Tobias asked on 20 May 2009, 09:47 AM
Hi,

I have a page with a grid on it. The grid is edited by the nice Inline form thats available. I have a couple of columns and a template column with a small table with a listbox and a dropdown and two buttons Add and Remove.

What I want to do is that when the user selects an item in the Combobox and press Add the item should appear in the Listbox. And when I select an item in the listbox and press Remove the Item should of course be removed.

Everything is working great, Ajax and all... But when I press the Add button and my ItemCommand fires I'm not able to find the Listbox again. I can't even find out that the row is editable.

Best Regards
Daniel

2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 20 May 2009, 11:20 AM
Hi Daniel,

You can set the CommandName of the Add button or Remove button and then try out the following code to achieve the scenario.

ASPX:
 
<telerik:GridTemplateColumn UniqueName="TempCol1"
<ItemTemplate> 
    
</ItemTemplate> 
<EditItemTemplate> 
   <table id="Table1"
   <tr> 
       <td> 
           <br /> 
           <telerik:RadComboBox ID="RadComboBox1" runat="server"
               <Items> 
                   <telerik:RadComboBoxItem runat="server" Text="RadComboBoxItem1" Value="RadComboBoxItem1" /> 
                   <telerik:RadComboBoxItem runat="server" Text="RadComboBoxItem2" Value="RadComboBoxItem2" /> 
                   <telerik:RadComboBoxItem runat="server" Text="RadComboBoxItem3" Value="RadComboBoxItem3" /> 
               </Items> 
               <CollapseAnimation Duration="200" Type="OutQuint" /> 
           </telerik:RadComboBox> 
       </td> 
       <td> 
       </td> 
    <td> 
        <asp:ListBox ID="ListBox1" runat="server" Height="56px"></asp:ListBox></td>                        
   </tr> 
   <tr> 
       <td> 
           <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add" CommandName="Add" />&nbsp; 
           <asp:Button ID="Button2" runat="server" Text="Remove" CommandName="Remove" /></td
       <td> 
       </td> 
   <td> 
   </td> 
   </tr>                        
   </table>                
</EditItemTemplate>                     
</telerik:GridTemplateColumn> 

CS:
 
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
{         
    if (e.CommandName == "Add"
    { 
        Button btn = (Button)e.CommandSource; 
        GridEditFormItem editItem = (GridEditFormItem)btn.NamingContainer; // 
        string str = (editItem.FindControl("RadComboBox1"as RadComboBox).SelectedItem.Text; 
        (editItem.FindControl("ListBox1"as ListBox).Items.Add(str); 
    } 
    else if (e.CommandName == "Remove"
    { 
        Button btn = (Button)e.CommandSource; 
        GridEditFormItem editItem = (GridEditFormItem)btn.NamingContainer; // 
        ListBox listbox1 = (ListBox) (editItem.FindControl("ListBox1")); 
        if(listbox1.SelectedItem != null
        { 
            string str =  listbox1.SelectedItem.Text; 
            listbox1.Items.Remove(str); 
        } 
    } 

Thanks,
Shinu.
0
Tobias
Top achievements
Rank 1
answered on 20 May 2009, 11:51 AM
Thanks,

It worked perfectly.

//Daniel
Tags
Grid
Asked by
Tobias
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Tobias
Top achievements
Rank 1
Share this question
or