Archive for Ottobre 10th, 2007

Selecting ALL CheckBoxes in The GridView

Ottobre 10th, 2007

Description: You can make a checkbox to select or de-select all the checkboxes in the gridview saving yourself from clicking each one by one

Link: http://www.codekeep.net/snippets/fdf48b18-2a3f-4315-b09b-47ff14a35a78.aspx

Introduction: 

GridView is a new data bound control introduced by Microsoft in Visual Studio.NET 2005. Most of the operations like sorting, paging and selecting item from the GridView is already built in and you can use it through the design view. In this article I will explain that how you can select single as well as all the checkboxes which are inside the GridView control. 

Selecting Checkboxes inside the GridView Control:

GridView has CheckboxField column which maps the checkbox to a field in the database. In this article we won't be using that, we will make a checkbox in a template column. Simply add a asp:checkbox control in the item template of the GridView control. If you are working with Datagrid control and want the same functionality than please check out my article Selecting Checkboxes inside the Datagrid control. 

The html code looks something like this: 

 <asp:GridView />
</asp:GridView> 

Now in the button click event write this code: 

// StringBuilder object
StringBuilder str = new StringBuilder(); 

// Select the checkboxes from the GridView control 

for (int i = 0; i < GridView1.Rows.Count; i++)

{

GridViewRow row = GridView1.Rows[i];

bool isChecked = ((CheckBox) row.FindControl("chkSelect")).Checked;

if (isChecked)

{

// Column 2 is the name column 

str.Append(GridView1.Rows[i].Cells[2].Text); 

} 

}

// prints out the result 

Response.Write(str.ToString()); 

The code above just iterates through the GridView and selects the checked checkboxes. Later it appends the selected value to a StringBuilder object. In order to use StringBuilder you will need to add the System.Text namespace.    

Making a CheckAll functionality:

To add a check all functionality in the GridView simply add a html checkbox to the header template of the checkbox column. 

 <HeaderTemplate>
<input)?spanChk:spanChk.children.item[0];

xState=theBox.checked;

elm=theBox.form.elements;

for(i=0;i<elm.length;i++)

if(elm[i].type=="checkbox" && elm[i].id!=theBox.id)

{

//elm[i].click();

if(elm[i].checked!=xState)

elm[i].click();

//elm[i].checked=xState;

}

}

</script>

This is it. I hope you like the article, happy coding! 

Blogging: The Free Internet Marketing Method

Ottobre 10th, 2007

Blogging is one of the easiest ways to get your message noticed on the Web. While blogging has now become a hot method for teens to broadcast their thoughts, it’s also a great Internet marketing tool. By Rodney T. 1010