When that happens, if you don't already know how to make that happen, you will search the internet, and will find the solution:
- Click on the smart tag for the GridView
- Choose Edit Columns...
- Add a new TemplateColumn to your GridView
- Click OK
- Click on the smart tag for the GridView again
- Choose Edit Templates...
- Click on the smart tag for the GridView yet again
- Select Display: Item Template
- Drag a CheckBox from the Toolbox panel to the item template
- ...do whatever else you need to do with the CheckBox
This solution puts a CheckBox in the header row of the GridView, and when the user selects or deselects that CheckBox, all the CheckBoxes in all the rows are selected or deselected. Begin by adding the CheckBox to your ASPX page:
- Click on the smart tag for the GridView
- If you aren't still editing templates, choose Edit Templates, then click the smart tag again
- Select Display: Header Template
- Drag a CheckBox from the Toolbox panel to the header template
- Right-click the CheckBox and choose Properties (the Properties panel will appear)
- Set the AutoPostBack property to true
- Choose the Events button of the Properties panel
- Add a method for the CheckedChanged event
That's all it takes for the UI: you have added a CheckBox to the header row, set it to post back events to the server, and set an event handler for CheckedChanged events. The last, simple thing to do is to write the code to select or deselect all the object-row CheckBoxes. Here is an example; the name of the method and the names of the CheckBoxes will be different in your application.
protected void GridViewHeaderCheckBox_CheckedChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in SourceEventGridView.Rows)
{
CheckBox rowCheckBox = row.Cells[0].FindControl("GridViewRowCheckBox") as CheckBox;
rowCheckBox.Checked = (sender as CheckBox).Checked;
}
}
Other solutions may be more appropriate in other situations. This problem could certainly be solved with Select All and Select None buttons; or with JavaScript; or surely in many other ways. Whichever way you make it happen, your boss should be satisfied.
No comments:
Post a Comment