473,782 Members | 2,542 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 #1
12 2189

Hi Jim,

Thanks for posting in this group.
Based on my understanding, you wanted to dynamic create some datagrids and
add to the placeholder, but when you postback, you find the placeholder's
child control collection is empty.
I did not see you code, but I think when postback you may forget to
recreate the datagrids and rebind to these controls.
If you do like this, it should work. Code snippet listed below:

private void Page_Load(objec t sender, System.EventArg s e)
{
// Put user code to initialize the page here
dg1=new DataGrid();
DataGrid dg2=new DataGrid();
DataGrid dg3=new DataGrid();
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();
dg3.DataSource= ds;
dg3.DataBind();
PlaceHolder1.Co ntrols.Add(dg1) ;
PlaceHolder1.Co ntrols.Add(dg2) ;
PlaceHolder1.Co ntrols.Add(dg3) ;
}

Private void Button1_Click(o bject sender, System.EventArg s e)
{
Response.Write( PlaceHolder1.Co ntrols.Count.To String());
}

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 #2
I understand that, but the problem is I can't get the data from the database
again for 2 reasons. 1 It may have changed, and 2 I'm trying to get the
items the user selected form the datagrids. If I reload them I don't know
which items they selected. Especially if a new record comed back in the
middle of the old ones.

These datagrids have a checkbox column. I need to go through the grids and
find which rows were checked.
Thanks,
jim

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

Hi Jim,

Thanks for posting in this group.
Based on my understanding, you wanted to dynamic create some datagrids and
add to the placeholder, but when you postback, you find the placeholder's
child control collection is empty.
I did not see you code, but I think when postback you may forget to
recreate the datagrids and rebind to these controls.
If you do like this, it should work. Code snippet listed below:

private void Page_Load(objec t sender, System.EventArg s e)
{
// Put user code to initialize the page here
dg1=new DataGrid();
DataGrid dg2=new DataGrid();
DataGrid dg3=new DataGrid();
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();
dg3.DataSource= ds;
dg3.DataBind();
PlaceHolder1.Co ntrols.Add(dg1) ;
PlaceHolder1.Co ntrols.Add(dg2) ;
PlaceHolder1.Co ntrols.Add(dg3) ;
}

Private void Button1_Click(o bject sender, System.EventArg s e)
{
Response.Write( PlaceHolder1.Co ntrols.Count.To String());
}

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 #3

Hi Jim,

Thanks for your feedback.
For your first reason, I think you should store your dataset in session
variable or viewstate, when postback, you can bind your datagrid with the
old dataset.
For your second reason, how do you implement your "row selection" in
webform datagrid? If you implement it through checkbox column(I think this
column is also customized by you), I think you can expose a property in
your customized checkbox column which returns its checkbox's checked state.
The article below customize a checkbox column expose the checkbox checked
state:
http://www.codeproject.com/aspnet/da...heckboxcol.asp

Use these 2 ways, I think you can get what you want, if there is still
anything unclear, please feel free to tell me.

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 #4
Thanks for the response Jeffrey,
We do use a custom check box column that I got form this news group and it
returns an array of indexes of items that were checked.

We can not use a session variable because the users connect to a web server
via a virtual IP. There is some middle where that distributes the requests
between 3 or 4 web servers. We may not end up on the same web server
between connections and postbacks. I believe viewstate is what I want to
use but haven't been able to figure out how to use it. We set ViewState to
true which is supposed to be the default) on each datagrid but we can never
find the datagrids. When a control is dragged onto the form and given an ID
we can just refer to it by name. There must be something else that enables
the control to be able to be referencable on a postback. I just don't know
what.

Any ideas?

Thanks again,
jim

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

Hi Jim,

Thanks for your feedback.
For your first reason, I think you should store your dataset in session
variable or viewstate, when postback, you can bind your datagrid with the
old dataset.
For your second reason, how do you implement your "row selection" in
webform datagrid? If you implement it through checkbox column(I think this
column is also customized by you), I think you can expose a property in
your customized checkbox column which returns its checkbox's checked state. The article below customize a checkbox column expose the checkbox checked
state:
http://www.codeproject.com/aspnet/da...heckboxcol.asp

Use these 2 ways, I think you can get what you want, if there is still
anything unclear, please feel free to tell me.

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 #5

Hi Jim,

Thanks for your feedback.
To store dataset in ViewState, you should first convert the dataset into
string. To fulfill this, you can use stringreader and stringwriter.

Sample code snippet like this:

protected DataSet ds;
private void Page_Load(objec t sender, System.EventArg s e)
{
// Put user code to initialize the page here
DataGrid dg1=new DataGrid();
DataGrid dg2=new DataGrid();
DataGrid dg3=new DataGrid();

ds=new DataSet();
if(!IsPostBack)
{
SqlDataAdapter adapter=new SqlDataAdapter( "select * from
jobs","server=l ocalhost;databa se=pubs;uid=sa; pwd=");
adapter.Fill(ds );
StringWriter sw=new StringWriter();
ds.WriteXml(sw) ;
ViewState["ds"]=sw.ToString();
}
else
{
StringReader sr=new StringReader((s tring)(ViewStat e["ds"]));
ds.ReadXml(sr);
}
dg1.DataSource= ds;
dg1.DataBind();
dg1.EnableViewS tate=true;
dg2.DataSource= ds;
dg2.DataBind();
dg3.DataSource= ds;
dg3.DataBind();

PlaceHolder1.Co ntrols.Add(dg1) ;
PlaceHolder1.Co ntrols.Add(dg2) ;
PlaceHolder1.Co ntrols.Add(dg3) ;
DataGrid1.DataS ource=ds;
DataGrid1.DataB ind();
}

It works well on my machine, if you still have anything unclear, please
feel free to tell me.

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 #6
Thanks, I think I see how that solves my problem. Will the datagrids that
get rebound still have the same rows checked in the check box columns? To
put it even simpler, if the user clicked on a row in the grid, would the
SelectedItem property still be set to the correct row?

Just out of curiosity, is there a way to gain access to the grid control
objects that I added dynamically like I would a grid control that I dropped
on the form from the toolbox?

Thanks again,
Jim

""Jeffrey Tan[MSFT]"" <v-*****@online.mi crosoft.com> wrote in message
news:6C******** ******@cpmsftng xa07.phx.gbl...

Hi Jim,

Thanks for your feedback.
To store dataset in ViewState, you should first convert the dataset into
string. To fulfill this, you can use stringreader and stringwriter.

Sample code snippet like this:

protected DataSet ds;
private void Page_Load(objec t sender, System.EventArg s e)
{
// Put user code to initialize the page here
DataGrid dg1=new DataGrid();
DataGrid dg2=new DataGrid();
DataGrid dg3=new DataGrid();

ds=new DataSet();
if(!IsPostBack)
{
SqlDataAdapter adapter=new SqlDataAdapter( "select * from
jobs","server=l ocalhost;databa se=pubs;uid=sa; pwd=");
adapter.Fill(ds );
StringWriter sw=new StringWriter();
ds.WriteXml(sw) ;
ViewState["ds"]=sw.ToString();
}
else
{
StringReader sr=new StringReader((s tring)(ViewStat e["ds"]));
ds.ReadXml(sr);
}
dg1.DataSource= ds;
dg1.DataBind();
dg1.EnableViewS tate=true;
dg2.DataSource= ds;
dg2.DataBind();
dg3.DataSource= ds;
dg3.DataBind();

PlaceHolder1.Co ntrols.Add(dg1) ;
PlaceHolder1.Co ntrols.Add(dg2) ;
PlaceHolder1.Co ntrols.Add(dg3) ;
DataGrid1.DataS ource=ds;
DataGrid1.DataB ind();
}

It works well on my machine, if you still have anything unclear, please
feel free to tell me.

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 #7

Hi Jim,

Thanks for your feedback.
For your further question, I will do some research, I will reply to you
ASAP.
Thanks for your understanding.

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 #8

Hi Jim,

After my research, I find that the datagrid control will do the viewstate
management for its content.
So you even need not save the dataset to viewstate yourself.
The code below proved this:
private void Page_Load(objec t sender, System.EventArg s e)
{

DataGrid dg1=new DataGrid();
dg1.EnableViewS tate = false;
DataGrid dg2=new DataGrid();
DataGrid dg3=new DataGrid();

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

this.Controls.A dd(dg1);
this.Controls.A dd(dg2);
this.Controls.A dd(dg3);

if(!IsPostBack)
{
dg1.DataSource= ds;
dg1.DataBind();
dg1.EnableViewS tate=true;
dg2.DataSource= ds;
dg2.DataBind();
dg3.DataSource= ds;
dg3.DataBind();

DataGrid1.DataS ource=ds;
DataGrid1.DataB ind();
}
}

You should place a button on the web form, when click, the form will post
back to server side, because dg1.EnableViewS tate = false, the datagrid1
will disappear.
While the other datagrids will remain with the origianl data, althrough
they did not rebind to database.

For your checkbox row selection problem, I think I meet some problem. When
I dynamicly add the TemplateColumn to the datagrid, if my datagrid did not
rebind to datasource, this column will disappear, code like this:

private void Page_Load(objec t sender, System.EventArg s e)
{
// Put user code to initialize the page here
TemplateColumn tc=new TemplateColumn( );
tc.ItemTemplate =new checkboxtemplat e();
DataGrid1.Colum ns.Add(tc);

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

DataGrid1.DataS ource=ds;
DataGrid1.DataB ind();
}
}

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

When click a button to postback, my TemplateColumn disappeared.

I will spend more time to figure it out. Thanks for your understanding.

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 #9
Thanks for all your work, I look forward to reading what you find.

jim

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

Hi Jim,

After my research, I find that the datagrid control will do the viewstate
management for its content.
So you even need not save the dataset to viewstate yourself.
The code below proved this:
private void Page_Load(objec t sender, System.EventArg s e)
{

DataGrid dg1=new DataGrid();
dg1.EnableViewS tate = false;
DataGrid dg2=new DataGrid();
DataGrid dg3=new DataGrid();

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

this.Controls.A dd(dg1);
this.Controls.A dd(dg2);
this.Controls.A dd(dg3);

if(!IsPostBack)
{
dg1.DataSource= ds;
dg1.DataBind();
dg1.EnableViewS tate=true;
dg2.DataSource= ds;
dg2.DataBind();
dg3.DataSource= ds;
dg3.DataBind();

DataGrid1.DataS ource=ds;
DataGrid1.DataB ind();
}
}

You should place a button on the web form, when click, the form will post
back to server side, because dg1.EnableViewS tate = false, the datagrid1
will disappear.
While the other datagrids will remain with the origianl data, althrough
they did not rebind to database.

For your checkbox row selection problem, I think I meet some problem. When
I dynamicly add the TemplateColumn to the datagrid, if my datagrid did not
rebind to datasource, this column will disappear, code like this:

private void Page_Load(objec t sender, System.EventArg s e)
{
// Put user code to initialize the page here
TemplateColumn tc=new TemplateColumn( );
tc.ItemTemplate =new checkboxtemplat e();
DataGrid1.Colum ns.Add(tc);

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

DataGrid1.DataS ource=ds;
DataGrid1.DataB ind();
}
}

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

When click a button to postback, my TemplateColumn disappeared.

I will spend more time to figure it out. Thanks for your understanding.

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 #10

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
2078
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
5325
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
2804
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
9639
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
9479
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
10311
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
10146
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
10080
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
9942
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6733
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
5378
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...
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.