 |
| ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

December 20th, 2003, 06:22 PM
|
|
Authorized User
|
|
Join Date: Nov 2003
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
DataList, Grid & Templates
Hi,
I want to create a page that displays general product item information followed by a DataGrid that contains the detail information.
I've implemented a DataList with an Item template for the general product item information. Now I'm trying to add the DataGrid and becoming confused.
Can you put a DataGrid into a DataList, or into it's Item Template? I think I'm doing this wrong.
How can it be done? Please help!
Sandra MacGregor
__________________
Sandy
|
|

December 21st, 2003, 06:21 PM
|
|
Authorized User
|
|
Join Date: Nov 2003
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Follow up - Stillworking on this,
My aspx file contains:
<form id="DL" runat="server">
<asp:datalist id="ProdList" runat="server">
<ItemTemplate>
<asp:Button id=ButtonDetailsLeft runat="server" OnCommand="Command_Button_Click" Text="Details" ToolTip="Click to View Details" CommandName="ShowGrid" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "partTypeID") %>'>
</asp:Button>
<asp:DataGrid id="DataGridLeft" runat="server"></asp:DataGrid>
</ItemTemplate>
</asp:datalist>
</form>
and the code-behind file contains:
protected void Command_Button_Click(Object sender, CommandEventArgs e) {
if(e.CommandName=="ShowGrid" ) {
// Obtain categoryId from QueryString
int partTypeID = Convert.ToInt32(e.CommandArgument);
// Obtain products and databind to an asp:datalist control
NoLimitEngin.ProductsDB productTypes = new NoLimitEngin.ProductsDB();
// Create Instance of Connection Object
SqlConnection sqlConnectionType = new System.Data.SqlClient.SqlConnection();
// Define the connection string
sqlConnectionType.ConnectionString = "data source=ANASAZI;initial catalog=nle;integrated security=SSPI;persist security info=False;workstation id=ANASAZI;packet size=4096";
// fill the datagrid
DataGridLeft = (DataGrid)ProdList.FindControl("DataGridLeft");
DataGridLeft.DataSource = productTypes.GetProductDetails(partTypeID, sqlConnectionType);
DataGridLeft.DataBind();
sqlConnectionType.Close();
}
}
where the GetProductDetails opens the connection and executes the SQL SP command. The DataGrid DataGridLeft
is declared in the class, above this function.
My problem is in the
DataGridLeft = (DataGrid) ProdList.FindControl ("DataGridLeft");
statement. It always returns null. I can't seem to be able to get hold of the DataGrid.
Please tell me how to do this.
Sandra MacGregor
|
|

December 21st, 2003, 11:35 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
You can put a datagrid (or any other list type control) into a datalist item template. You just have to know the right data relationships to make them work together. Take a look at this thread. Find my third post. There's something there that might be of use. It is a small example of doing multi-level databinding. It describes how, with the right dataset constructions, you can bind a bindable control within another bindable control using the data's relationships.
Regarding your question in your followup post...
The problem is that you are trying to find one "DataGridLeft" control in the datalist, when there are multiple instances of that datagrid. What you need to do is look for that control in the current control's container. What I mean is that from within the handler for the button click, you can look at that button's container which is the datalist ListItem. From there you should be able to find the datagrid in that item. Try something like this:
DataGridLeft = (DataGrid) sender.Container.FindControl("DataGridLeft");
Depending on the desired behavior, you'll have to decide which solution to use.
Peter
------------------------------------------------------
Work smarter, not harder.
|
|

December 22nd, 2003, 08:47 PM
|
|
Authorized User
|
|
Join Date: Nov 2003
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Peter,
Thanks very much for responding.
I said "The 'DataGrid DataGridLeft;'
is declared in the class, above this function.", but I found out that when I do that, the datagrid inserted in the template designer from the toolbox developes an error. When I remove that line 'DataGrid DataGridLeft;' the aspx.cs page won't complile because it says "The name 'DataGridLeft' does not exist in the class or namespace 'NoLimitEngin.ProductsList'.
In the case of the button, the object itself is never referred to, only the event handler named in the button declaration.
Also, when I insert the line 'DataGrid DataGridLeft;' just to get it to compile, with
DataGridLeft = (DataGrid) sender.Container.FindControl("DataGridLeft");
instead of
DataGridLeft = (DataGrid)ProdList.FindControl("DataGridLeft");
the compiler says "'object' does not contain a definition for 'Container' although the page top has
using System.ComponentModel;
I read the thread you pointed out. Right now I'm not using a dataset, only a 2 data readers. I wanted not to spend the time of accessing the detail table information if the user doesn't ask for it.
I'm really stuck here.
Sandra MacGregor
|
|

December 22nd, 2003, 08:57 PM
|
|
Authorized User
|
|
Join Date: Nov 2003
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
More Info, Peter,
The data binding for the data list is being done in the Page_Load function:
private void Page_Load(object sender, System.EventArgs e) {
// Obtain categoryId from QueryString
int categoryID = Int32.Parse(Request.Params["categoryID"]);
// Obtain products and databind to an asp:datalist control
NoLimitEngin.ProductsDB productCatalogue = new NoLimitEngin.ProductsDB();
ProdList.DataSource = productCatalogue.GetProducts(categoryID);
ProdList.DataBind();
}
Thanks for all your help!
Sandra MacGregor
|
|

December 23rd, 2003, 02:23 PM
|
|
Authorized User
|
|
Join Date: Nov 2003
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Continuing,
So I'm declaring DataGrid dgl; in the class and trying:
ControlCollection mycontrols = ProdList.Controls;
//bool doit = mycontrols.Contains("DataGridLeft");
ERROR no such overloaded method
//int doit = mycontrols.IndexOf("DataGridLeft")
ERROR no such overloaded method
string name = mycontrols.ToString();
name = "System.Web.UI.ControlCollection"
mycontrols.Contains("DataGridLeft")
ERROR no such method
There must be some way to do this in C#
Sandra MacGregor
|
|

December 29th, 2003, 03:42 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
I'm a little hazy on this topic: been a while since I've thought about it and in the blur of holidays this and some other threads have probably blended together.
I think I'm getting the general sense for what's wrong here, perhaps this explanation will help. I'll use datagrids in examples because that's what I'm most familiar with.
When you want to do multi-level databinding, you put some bindable control into the item template for the "outer" bindable control. The outer control occurs a single time and can live as a control in the markup (<asp:datagrid ... />) as well as a declared variable in the code-behind (Protected WithEvents grdMyDataGrid As DataGrid). Any INNER bindable controls can only live as controls in the markup. This is because they will occur multiple times (once for each repeated item in the control one level up).
Typically you bind the outer control in the page load handler. Binding the inner control(s) is trickier. You either need to have a dataset with relations such that you can select a set of rows from another datatable in the dataset based on a row relationship, or you could handle the control's databinding events and manually bind the inner control's data based on whatever you choose. I won't get into the details about where/how to GET the data, only how to BIND the data to the inner control.
Seeing as this thread seems to be focused on the latter idea of inner control binding, let's look at that.
When a control is data bound, there is usually an event that fires for each item as it's bound. For a datagrid this event is ItemDataBound. This event delegate's second argument (e) is a DataGridItemEventArgs object. On that object is Item which is the current datagrid item being bound. Once you can see that, you can access any of the columns of the grid (the Cells collection) or search for a control (e.Item.FindControl("..")).
Once you can see and access the controls that make up the outer bindable control's item template, you can do whatever you need with/to them. In this case we would find the inner bindable control, set its datasource and bind it. This would look something like this:
Dim objInnerGrid As DataGrid
objInnerGrid = CType(e.Item.FindControl("grdInnerGrid"), DataGrid)
objInnerGrid.DataSource = <another datasource>
objInnerGrid.DataBind
The inner control's data source may need to use data from the current item (e) in order to select applicable (related) data. If so, you can access the other parts of the outer control to grab bits of data to create your filter criteria.
The important things to remember regarding multi-level databinding are:
- "Inner" controls can and will not correspond to a class member (i.e. Protected WithEvents <control name>). They are instantiated with each item of the list you are binding to.
- When you deal with the item handler(s) of a list type control, you are only dealing with one item at a time during the scope of that handler: e.Item is the current list item be it an object in a collection, a row in a table, etc.
Hopefully this will help to clarify the concept a bit.
Peter
------------------------------------------------------
Work smarter, not harder.
|
|
 |