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

[Solved] checkboxlist in command template problem

2 Answers 107 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Lee Malo
Top achievements
Rank 1
Lee Malo asked on 27 Jan 2010, 04:08 AM
I currently have the following code working

 

<CommandItemTemplate>

 

 

<div id="list_countries" onmouseover="mouseover()" onmouseout="mouseout()" >

 

 

<asp:CheckBoxList ID="CheckBoxList1" runat="server" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">

 

 

</asp:CheckBoxList>

 

 

</div>
<CommandItemTemplate>


which runs this

 

protected

 

void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)

 

{

 

 

CheckBoxList cbList = sender as CheckBoxList;

 

 

 

foreach

 

(ListItem listItem in cbList.Items)

 

RadGrid1.MasterTableView.Columns[cbList.Items.IndexOf(listItem)].Display = listItem.Selected;

}



This works great but I need to change it so instead of OnSelectedIndexChanged it runs off of a button like so
<CommandItemTemplate>

<

 

div id="list_countries" onmouseover="mouseover()" onmouseout="mouseout()" >

 

 

<asp:CheckBoxList ID="CheckBoxList1" runat="server" >

 

 

</asp:CheckBoxList>

 

 

</div>

 

 

<div class="commandmiddle">

 

 

<asp:Button ID="Button5" runat="server" Text="Show/Hide Columns" OnClick="CheckBoxList1_SelectedIndexChanged"> />

 

 

</div>
</CommandItemTemplate>

but I cant get the checkboxlist control in the CheckBoxList1_SelectedIndexChanged function if its from the button

what should I do?

 

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 27 Jan 2010, 05:32 AM
Hello,

You can access the CheckBoxList placed in CommandItemTemplate using the following code.

CS:
 
    protected void Button5_Click(object sender, EventArgs e) 
    { 
        GridCommandItem commandItem = (GridCommandItem)RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0]; 
        CheckBoxList cbList = (CheckBoxList)commandItem.FindControl("CheckBoxList1"); 
 
        foreach (ListItem listItem in cbList.Items) 
        { 
            RadGrid1.MasterTableView.Columns[cbList.Items.IndexOf(listItem)].Display = listItem.Selected; 
        } 
        RadGrid1.Rebind(); 
    } 

-Shinu.
0
Lee Malo
Top achievements
Rank 1
answered on 27 Jan 2010, 02:29 PM
perfect, exactly what I needed thank you
Tags
Grid
Asked by
Lee Malo
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Lee Malo
Top achievements
Rank 1
Share this question
or