473,395 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

RegisterArrayDeclaration Help Needed with Grid


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.
Nov 19 '05 #1
6 2218
Just as an alternate solution: you could loop through the grid on
postback and pick out which rows have the checkbox checked.

there is some sample code in this article:
http://odetocode.com/Articles/372.aspx

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 17 May 2005 21:44:25 -0400, xenophon <xe******@online.nospam>
wrote:

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.


Nov 19 '05 #2
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.)

Nov 19 '05 #3
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.

On Tue, 17 May 2005 22:47:59 -0400, Scott Allen
<sc***@nospam.odetocode.com> wrote:
Just as an alternate solution: you could loop through the grid on
postback and pick out which rows have the checkbox checked.

there is some sample code in this article:
http://odetocode.com/Articles/372.aspx


Nov 19 '05 #4

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.


On Wed, 18 May 2005 08:20:54 GMT, v-******@online.microsoft.com
(Steven Cheng[MSFT]) wrote:
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.)


Nov 19 '05 #5
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]));
}
}
}
======================
Nov 19 '05 #6
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.)

Nov 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Roy Riddex | last post by:
I have a text file which holds data for 30 cars in the following way: CarRegistration CarType CarClass Available I'm trying to display the full contents of this...
2
by: Raghu Raman | last post by:
Hi, In my asp .net page am using many controls and a grid.i populate many html controls innside the grid dynamically .if i click the grid ,it wll go to other page using onclick client side...
0
by: Ken Baltrinic | last post by:
I am authoring a composite server control (inheriting from System.Web.UI.Control) that uses a good chunk of client side javascript. To make this work I need to call RegisterArrayDeclaration and in...
3
by: Alan | last post by:
I will need to load data from a datatable to a datagrid several times. How do I clear the contents of the datagrid before loading the contents into the datagrid ?
0
by: batista | last post by:
Hello all, Let me first explain my windows application.... I have a grid which is bound to a dataview of a dataset... Now this dataset is updated from a separate thread and therefore I've...
0
by: Svenn Bjerkem | last post by:
Hi, Armed with Programming Python 3rd Edition and Learning Python 2nd edition I try to write an application which I at first thought was simple, at least until I was finished with the GUI and...
6
by: CapMaster | last post by:
I'm having some trouble programming the game of life. In the version my teacher gave us, it involves a class with a private grid variable for the game. Here's the class she gave us: .. const int...
5
by: misra | last post by:
Hi, My form has a grid and the datasource the grid is an array. There is an event which adds records to the array and assigns the array as the datasource to the grid. When I just click on the...
2
by: =?Utf-8?B?Qw==?= | last post by:
Hi, I have a 3 dimenionsal array which I populate in my C# code. I then want to make this available to my Javascript so I do below. Page.ClientScript.RegisterArrayDeclaration("myArray",...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.