Hi Xenophon,
Thanks for your followup.
If what you worried about is the DataGrid being disabled for Viewstate,
then that's doesn't matter since we can still retrieve the controls from
DataGrid.Items collection when posting back as long as we bind data with
datagrid everytime in the Page_Load. Anyway, I've made a simple demo page
which have used both the two means :
1. Use serverside datagrid obejct model to query the checkbox controls and
get the checked state.
2. Use clientside script to save all the checkboxes's checked states before
post back to server.
The page's code are pasted in the bottom of the message, you can have a
test on your side to see whether it helps.
Thanks,
Steven Cheng
Microsoft Online Support
Get Secure!
www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
=============== aspx=========== =========
<HTML>
<HEAD>
<title>checkbox grid</title>
<meta name="GENERATOR " Content="Micros oft Visual Studio .NET 7.1">
<meta name="CODE_LANG UAGE" Content="C#">
<meta name="vs_defaul tClientScript" content="JavaSc ript">
<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5">
<script language="jscri pt">
function saveCheckArray( )
{
var i=0;
var array = new Array();
for(i=0;i<docum ent.all.length; i++)
{
if(document.all[i].type == "checkbox" &&
document.all[i].id.indexOf("dg Check")> -1)
{
var chk = document.all[i];
array.push(chk. checked);
}
}
document.getEle mentById("check Array").value = array.join("|") ;
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="dgCheck" runat="server" AutoGenerateCol umns="False"
EnableViewState ="False">
<Columns>
<asp:BoundColum n HeaderText="Nam e" DataField="name "></asp:BoundColumn >
<asp:TemplateCo lumn HeaderText="Sel ected">
<ItemTemplate >
<asp:CheckBox ID="chkSelect" Runat="server" Checked="<%#
((System.Data.D ataRowView)Cont ainer.DataItem)[1] %>">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateCol umn>
</Columns>
</asp:DataGrid>
<input type="hidden" id="checkArray " runat="server">
<P>
<asp:Button id="btnSubmit" runat="server" Text="Submit"></asp:Button>
</P>
</form>
</body>
</HTML>
=========code behind========= ============
public class checkboxgrid : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Butt on btnSubmit;
protected System.Web.UI.H tmlControls.Htm lInputHidden checkArray;
protected System.Web.UI.W ebControls.Data Grid dgCheck;
private void Page_Load(objec t sender, System.EventArg s e)
{
dgCheck.DataSou rce = GetDataSource() ;
dgCheck.DataBin d();
btnSubmit.Attri butes["onclick"] ="saveCheckArra y();";
}
private DataTable GetDataSource()
{
DataTable dt = new DataTable();
dt.Columns.Add( "name",typeof(s tring));
dt.Columns.Add( "selected",type of(bool));
for(int i=0;i<10;i++)
{
DataRow dr = dt.NewRow();
dr[0] = "Name_" + i;
dr[1] = i%3 == 0? true:false;
dt.Rows.Add(dr) ;
}
return dt;
}
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
InitializeCompo nent();
base.OnInit(e);
}
private void InitializeCompo nent()
{
this.btnSubmit. Click += new System.EventHan dler(this.btnSu bmit_Click);
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
private void btnSubmit_Click (object sender, System.EventArg s e)
{
CheckBox chk = null;
int i = 0;
Response.Write( "<br>Values from DataGrid server object:");
for(i=0;i<dgChe ck.Items.Count; i++)
{
chk = dgCheck.Items[i].FindControl("c hkSelect") as CheckBox;
Response.Write( string.Format(" <br/>Item {0} Selected: {1}", i,
chk.Checked));
}
Response.Write( "<br>Values from clientside input hidden field:");
string[] items = checkArray.Valu e.Split("|"[0]);
for(i=0;i<items .Length;i++)
{
Response.Write( string.Format(" <br/>Item {0} Selected: {1}", i,
items[i]));
}
}
}
=============== =======