473,791 Members | 3,193 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I find a grid control ona web page in a post back?

We ahve a .NET web page that we dynamically create a bunch of DataGrids and
add them to a place holder. When the user hits submit we need to go through
all of the DataGrids to get some data. We can't seem to find the grids on
the post back, the PlaceHolder.Con trols.Count is 0. How do we get that data
back.

The DataGrids have check boxes that the user may have checked. Those are
the rows we want to get so we can't just requery and rebind.

Any ideas?

Thanks,
Jim
Nov 15 '05
12 2190

Hi Jim,

Thanks for your feedback.
Oh, I have found out the trick.
Actually, the web server control will automatically persists its state in
viewstate, but the dynamic added controls(or columns) are not automatically
added to the page's view state, so you are obliged to add logic to the page
to make sure the persistant with each round trip.
An good way to do is to override page's LoadViewState method:

using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Data.Sql Client;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace checkboxvalueda tagrid
{
public class WebForm1 : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Butt on Button1;
private DataGrid dg1=null ,dg2=null;
protected System.Web.UI.H tmlControls.Htm lForm Form1;

private void Page_Load(objec t sender, System.EventArg s e)
{
if(!IsPostBack)
{
addcolumn();

SqlDataAdapter adapter=new SqlDataAdapter( "select * from
jobs","server=l ocalhost;databa se=pubs;uid=sa; pwd=");
DataSet ds=new DataSet();
adapter.Fill(ds );

dg1.DataSource= ds;
dg1.DataBind();
dg2.DataSource= ds;
dg2.DataBind();
}
}

protected override void LoadViewState(o bject savedState)
{
base.LoadViewSt ate (savedState);
addcolumn();
}

public void addcolumn()
{
dg1=new DataGrid();
dg2=new DataGrid();

TemplateColumn tc=new TemplateColumn( );
tc.ItemTemplate =new checkboxtemplat e();

dg1.Columns.Add (tc);
dg2.Columns.Add (tc);

Form1.Controls. Add(dg1);
Form1.Controls. Add(dg2);
}

public class checkboxtemplat e: ITemplate
{
public void InstantiateIn(C ontrol container)
{
CheckBox cb=new CheckBox();
cb.ID="checkbox ";
container.Contr ols.Add(cb);
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
InitializeCompo nent();
base.OnInit(e);
}

private void InitializeCompo nent()
{
this.Button1.Cl ick += new System.EventHan dler(this.Butto n1_Click);
this.Load += new System.EventHan dler(this.Page_ Load);

}
#endregion

private void Button1_Click(o bject sender, System.EventArg s e)
{
foreach(DataGri dItem item in dg1.Items)
{
CheckBox myCheckbox = (CheckBox)item. Cells[0].Controls[0];
if(myCheckbox.C hecked == true)
{
Response.Write( item.ItemIndex. ToString());
}
}
}
}
}

Note: I dynamic add 2 datagrid into the "FORM" control collection(Can not
be the page's control collection, because the checkbox column will interact
with server side)
In Button1_Click, I only retrieve the first datagrid's checkbox column
state.
It works well on my machine, if you have anything unclear, please feel free
to tell me.

Merry Christmas!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #11

Hi Jim,

Did my reply resolve your problem?
If you still have any concern, please feel free to tell me. I am glad to
work with you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #12
Thanks Jeffrey. Sorry I didn't say thanks earlier. We haven't tried it
yet. We had to change the design and work around the issue because of a
time constraint. We may put the multiple datagrid version code back in at a
later time. The code you sent me looks like it will do the trick and it
gave us some insight into how things work.

Thanks again for all your help!

Jim

""Jeffrey Tan[MSFT]"" <v-*****@online.mi crosoft.com> wrote in message
news:92******** ********@cpmsft ngxa07.phx.gbl. ..

Hi Jim,

Did my reply resolve your problem?
If you still have any concern, please feel free to tell me. I am glad to
work with you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #13

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

Similar topics

5
4809
by: Dan | last post by:
Hi, I'd like to find out the control that caused a postback to be raised. Obviously this could simply done in a control event handler. I am not going to do this method and would like no references as to how to do it this way as I already know how. I know this may be getting into advanced or unheard of territory. I have some custom web user controls that are dynamically created at run time along with other controls. I can still add an...
1
2079
by: Aleksandar Andjelkovic | last post by:
Greetings, Is there a way in ASP.NET to refresh data grid control without (post back) reload of the hole page Thank you, Aleksandar
10
5326
by: Sacha Korell | last post by:
I'm trying to load a drop-down list with all DropDownList control names from another page. How would I be able to find those DropDownList controls? The FindControl method will only find a certain control by id, but I want to find all controls of a certain type (DropDownList in this case). Is there an easier way than to get a control count of the page, loop through all controls on that page, examine their type and, if they're a...
2
2805
by: Smokey Grindle | last post by:
I have the following on my page... a link button that will send the command name "use" to the command handler and a grid view with the following columns 0 - Template column - just a checkbox (asp.net control) 1 - Bound column - DisplayName from data table the checkbox is just a template i made by placeing an asp.net control into the item template when I check items on the grid then click the link and execute the following
0
9669
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
9517
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
10428
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
10207
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...
1
10156
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9030
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
5435
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2916
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.