C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




CheckBoxes in DataGrid


Posted Date: 08 Sep 2006    Resource Type: Articles    Category: Web Applications
Author: sivavenugopalMember Level: Silver    
Rating: Points: 10



This code shows how to use checkbox in datagrid..

function select_deselectAll (chkVal, idVal)
{
var frm = document.forms[0];
// Loop through all elements
for (i=0;i< frm.length; i++)
{
// Look for our Header Template's Checkbox
if (idVal.indexOf ('CheckAll') != -1)
{
// Check if main checkbox is checked, then select or deselect datagrid checkboxes
if(chkVal == true)
{
frm.elements[i].checked = true;
}
else
{
frm.elements[i].checked = false;
}
// Work here with the Item Template's multiple checkboxes
}
else if (idVal.indexOf ('DeleteThis') != -1)
{
// Check if any of the checkboxes are not checked, and then uncheck top select all checkbox
if (frm.elements[i].checked == false)
{
frm.elements[1].checked = false;
//Uncheck main select all checkbox
}
}
}
}
function confirmDelete (frm)
{
// loop through all elements
for (i=0; i<frm.length; i++)
{
// Look for our checkboxes only
if (frm.elements[i].name.indexOf("DeleteThis") !=-1)
{
// If any are checked then confirm alert, otherwise nothing happens
if(frm.elements[i].checked)
{
return confirm ('Are you sure you want to delete your selection(s)?')
}
}
}
}


<ASP:DataGrid id="MyDataGrid" runat="server" Width="700" BackColor="white" BorderColor="black"
CellPadding="3" CellSpacing="0" Font-Size="9pt" AutoGenerateColumns="False" HeaderStyle-BackColor="darkred" HeaderStyle-ForeColor="white">
< Columns>
< asp:TemplateColumn>
< HeaderTemplate>
< asp:CheckBox ID="CheckAll" OnCheckedChanged="javascript: return select_deselectAll (this.checked,this.id);" runat="server" />
< font face="Webdings" color="white" size="4">< /font>
< /HeaderTemplate>
< ItemTemplate>
< asp:CheckBox ID="DeleteThis" OnCheckedChanged="javascript: return select_deselectAll (this.checked, this.id);" runat="server" />
< /ItemTemplate>
< /asp:TemplateColumn>
< asp:TemplateColumn>
< HeaderTemplate>
ID
< /HeaderTemplate>
< ItemTemplate>
< asp:Label ID="StoreID" Text='< %# DataBinder.Eval (Container.DataItem, "ID") %>' runat="server"/>
< /ItemTemplate>
< /asp:TemplateColumn>
< asp:BoundColumn HeaderText="Store" Datafield="Store" runat="server" />
< asp:BoundColumn HeaderText="Address" Datafield="Address" runat="server" />
< asp:BoundColumn HeaderText="City" Datafield="City" runat="server" />
< asp:BoundColumn HeaderText="State" Datafield="State" runat="server" />
< asp:BoundColumn HeaderText="Zip" Datafield="Zip" runat="server" />
< /Columns>
< /ASP:DataGrid>


< asp:Button Text="Delete Items" OnClick="DeleteStore" ID="Confirm" runat="server" />
< span id="OutputMsg" runat="server" />

public void DeleteStore (Object sender, EventArgs e)
{
string dgIDs = "";
bool BxsChkd = false;
foreach (DataGridItem i in MyDataGrid.Items)
{
CheckBox deleteChkBxItem = (CheckBox) i.FindControl ("DeleteThis");
if (deleteChkBxItem.Checked)
{
BxsChkd = true;
// Concatenate DataGrid item with comma for SQL Delete
dgIDs += ((Label) i.FindControl ("StoreID")).Text.ToString() + ",";
}
}
// Set up SQL Delete statement, using LastIndexOf to remove tail comma from string.
string deleteSQL = "DELETE from Stores WHERE stor_id IN (" + dgIDs.Substring (0, dgIDs.LastIndexOf (",")) + ")";
if (BxsChkd == true)
{
// Execute SQL Query only if checkboxes are checked, otherwise error occurs with initial null string
try
{
SqlHelper.ExecuteNonQuery (objConnect, CommandType.Text, deleteSQL);
}
catch (SqlException err)
{
//Exception Catching code as usual displaying message
}
//Refresh data
BindData();
}
}
public void BindData()
{
String sqlQuery = "Select stor_id As Id, stor_name As Store, City, State, Zip from Stores";
MyDataGrid.DataSource = SqlHelper.ExecuteDataset(objConnect, CommandType.Text, sqlQuery);
MyDataGrid.DataBind();
objConnect.Close();
objConnect = null;
}




Responses

Author: Prince    21 Dec 2007Member Level: Bronze   Points : 0
hi you can also delete the records without making using a foreach loop in the code behind page. you can do this in the other way by using a javascript and get the records which are checked and placed that value in the server side hidden field in the page and access that hidden field value in the code so that you will not require to use foreach loop in the code behind page.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Use  .  Datagrid  .  Checkbox  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: A general thought on ASP.NET releases
Previous Resource: ASP.NET Controls
Return to Discussion Resource Index
Post New Resource
Category: Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use