473,796 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RegisterArrayDe claration 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 RegisterArrayDe claration, 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 2246
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******@onlin e.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 RegisterArrayDe claration, 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.o detocode.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.m icrosoft.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>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]));
}
}
}
=============== =======
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
4503
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 text file in a list box so the user can see the total car fleet, my problem is that I want everything to line up. So far what I'm getting is something like: R296RKV Ford Mondeo Large On Hire TUVWXYZ ...
2
1099
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 javascript function. I,want my grid data to be maintained in cache when i click the html button. before going to that page i want to cache my page data,so that i can
0
1242
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 a manner simmilar to the following: Page.RegisterArrayDeclaration( dataObjectArrayName, string.Format( "{{ spanID:'{0}', calendarID:'{1}' }}", spanID,
3
1009
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
937
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 used a Custom class which is inherited by dataview...and the OnListChanged and IndexListChanged event are overridden...and in those funtions im using begininvoke to make sure that when dataset changes from separate thread the grid does'nt...
0
1426
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 then wanted to start putting some data into it. As you will see, the program is not yet finished but I post what I have so far.
6
4788
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 maxrow = 20, maxcol = 60; // grid dimensions class Life { public: Life (); void initialize(); void print(); void update(); private:
5
2606
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 grid, on any cell, it throws an exception on the Program.cs file static void Main(string Args) { try { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); ...
2
7170
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", myArray.ToString()) When I view my source html I see var myArray = new Array(System.Int32) What am I doing wrong here?
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10467
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10244
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.