jeudi 26 février 2015

On button click, how can I use some C# in my code behind to automatically check several checkboxes in the front end?

I have some front-end code that generates a list of items after the user searches for items. Example: If the user searches for "fruit" it might return a list of different kinds of fruits. Each of these items has a checkbox by it in case the user wants to delete the item(s) or make modifications. Sometimes, the list of returned items is really long. If the user wants to delete all the items returned, it would take a while to check each box automatically. So I've added a "Select All" button that I want to use in order to check all of the boxes on click.


Here is my code so far. First, this is the checkbox code. It's an asp:CheckBox inside of an asp:TemplateField that is inside of an asp:GridView:



<asp:CheckBox ID="uxSelectDelete" AutoPostBack="true" runat="server" />


Here is my Select All button:



<asp:Button Text="Select All" ID="uxSelectAll" runat="server" OnClick="uxSelectAll_Click" />


Here is my uxSelectAll_Click method in the code behind:



protected void uxSelectAll_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in uxSearchedItems.Rows)
{
CheckBox selected = (CheckBox)row.FindControl("uxSelectDelete");
selected.Checked = true;
}
}


I tried to incorporate selected.CheckedChange but it seems that functionality is for sending an event whenever the user clicks a checkbox. Which isn't what I want - I want the Select All button to check all of the checkboxes in the list for me. I would appreciate any tips in the right direction.





Aucun commentaire:

Enregistrer un commentaire