RegisterArrayDeclaration Help Needed with Grid

X

xenophon

I have a grid with checkboxes in it. When a checkbox is un/checked, I
want to set a true-false value in an array. Then on PostBack I can
work with that array.

I know I need to use RegisterArrayDeclaration, but I don't see a quick
tutorial how. Can anyone post sample C# code-behind-only code on how
to do this?

Thanks.
 
S

Steven Cheng[MSFT]

Thanks for Scott's informative suggestion.

Hi Xenophon ,

As Scott has mentioned, we can query the select status in each datagrid Row
at serverside when post back rather than use clientsdie script to store
these info. Since the serverside ASP.NET provide a convenient object model
for us to access the datagrid's item template, I think it'll be much
better. What's your oponion on this?

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.)
 
X

xenophon

Thanks, but I won't be able to do that because I am not keeping the
ViewState contents because the grid is complex and large. Pushing a
couple of hundred kbytes back upstream just to know a checkbox state
is too much for this app.
 
X

xenophon

I cannot use this solution. The rendered grid is very large and
complex, so EnableViewState is set to false for the grid. I just need
to know the state of the checkboxes, with just the checkbox state (and
grid row id) going back upstream.

Please show an example of how I can do this, using only C# (my grids
are not created in the .aspx page tempalte at all).

Thanks.
 
S

Steven Cheng[MSFT]

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.type == "checkbox" &&
document.all.id.indexOf("dgCheck")> -1)
{
var chk = document.all;
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.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));
}
}
}
======================
 
S

Steven Cheng[MSFT]

Hi Xenophon,

Any progress on this issue? Does the test page I pasted in the former
message help? If anything else we can help, please feel free to post here.

Thanks & Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top