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>checkboxgrid</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script language="jscript">
function saveCheckArray()
{
var i=0;
var array = new Array();
for(i=0;i<document.all.length;i++)
{
if(document.all[i].type == "checkbox" &&
document.all[i].id.indexOf("dgCheck")> -1)
{
var chk = document.all[i];
array.push(chk.checked);
}
}
document.getElementById("checkArray").value = array.join("|");
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="dgCheck" runat="server" AutoGenerateColumns="False"
EnableViewState="False">
<Columns>
<asp:BoundColumn HeaderText="Name" DataField="name"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Selected">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" Runat="server" Checked="<%#
((System.Data.DataRowView)Container.DataItem)[1] %>">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</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.Page
{
protected System.Web.UI.WebControls.Button btnSubmit;
protected System.Web.UI.HtmlControls.HtmlInputHidden checkArray;
protected System.Web.UI.WebControls.DataGrid dgCheck;
private void Page_Load(object sender, System.EventArgs e)
{
dgCheck.DataSource = GetDataSource();
dgCheck.DataBind();
btnSubmit.Attributes["onclick"] ="saveCheckArray();";
}
private DataTable GetDataSource()
{
DataTable dt = new DataTable();
dt.Columns.Add("name",typeof(string));
dt.Columns.Add("selected",typeof(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(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnSubmit_Click(object sender, System.EventArgs e)
{
CheckBox chk = null;
int i = 0;
Response.Write("<br>Values from DataGrid server object:");
for(i=0;i<dgCheck.Items.Count;i++)
{
chk = dgCheck.Items[i].FindControl("chkSelect") 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.Value.Split("|"[0]);
for(i=0;i<items.Length;i++)
{
Response.Write(string.Format("<br/>Item {0} Selected: {1}", i,
items[i]));
}
}
}
======================