473,322 Members | 1,846 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,322 software developers and data experts.

Databinding by ClientID

Hi,
I'm trying to learn ASP.NET by doing a pilot project:

I have a DataGrid which contains a nested DataList. I want to iterate
through the SQLDataReader for the DataGrid and populate each DataList
by binding to a fresh SQLDataReader. At the moment I can only bind to
the first instance of the DataList. If I could bind the data by the
ClientID (or UniqueID), I'm sure it would work. Is there a way of
doing this? Or maybe there is a better approach to getting nested data
from separate SQLDataReaders.

Here's the relevant part of the code with extraneous bits removed (I
hope the line breaks don't make it unreadable!):

// ######### ASPX ###########

<asp:DataGrid Border="1" Runat="server" Id="dgApplicants">
<Columns>
<asp:BoundColumn DataField="ApplicantRef" Visible="False" />

<%-- Some more BoundColumns and TemplateColumns --%>

<asp:TemplateColumn>
<ItemTemplate>
<tr>
<td width="30px">&nbsp;</td>
<td colspan="8">
<asp:DataList Border="0" Runat="server" Id="dgApplications"
AutoGenerateColumns="False" Height="0px" Width="100%"
AllowSorting="False">
<ItemTemplate>
<b>App&nbsp;<%# DataBinder.Eval(Container.DataItem,
"VendorNo")%>&nbsp;<%# DataBinder.Eval(Container.DataItem,
"ApplicationRef")%>&nbsp;v<%# DataBinder.Eval(Container.DataItem,
"Version")%>&nbsp;<%# DataBinder.Eval(Container.DataItem,
"Status")%></b>
</ItemTemplate>
</asp:DataList>
</td>
</tr>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

// ######### ASPX ENDS ###########

// ######### CODE-BEHIND ###########
//adoApplicantReader is the outer loop
//adoApplicationReader is the inner loop

if(objDuplicatePartiesApplicant.ListParties(inputS urname, inputTown,
inputMainCPH, inputBusinessName, inputPostcode, inputOrderBy, ref
adoApplicantReader) == MyAssembly.AESIS.ReturnCodes.Success)
{
// OK, the SQL data layer object returned "success", but did we get
some results?
if(adoApplicantReader.HasRows)
{
// successful search: hide prompt and display Update button
lblPrompt.Visible = false;
btnUpdate.Visible = true;

// databinding method
this.dgApplicants.DataSource = adoApplicantReader;
this.dgApplicants.DataBind();
adoApplicantReader.Close();

// with the firehose cursor (SQLDataReader), we have to do the query
again after it is bound to pass it's members...
if(objDuplicatePartiesApplicant.ListParties(inputS urname, inputTown,
inputMainCPH, inputBusinessName, inputPostcode, inputOrderBy, ref
adoApplicantReader) == MyAssembly.AESIS.ReturnCodes.Success)
{
// SQL data layer object returned "success"
if(adoApplicantReader.HasRows)
{
while (adoApplicantReader.Read())
{
int intOrd = adoApplicantReader.GetOrdinal("ApplicantRef");
int intApplicantRef = (int)adoApplicantReader.GetValue(intOrd);

SqlDataReader adoApplicationReader = null;

if(objDuplicatePartiesApplication.ListPartyApplica tions(intApplicantRef,
ref adoApplicationReader) == MyAssembly.AESIS.ReturnCodes.Success)
{
// did we get some results?
if(adoApplicationReader.HasRows)
{
// find a reference to the nested DataList
foreach(DataGridItem dgItem in dgApplicants.Items)
{

DataList dgSelected =
(DataList)dgItem.FindControl("dgApplications");

// here I need to find the reference name of each nested
datagrid
// and assign data to that...
Response.Write("<tr><td><font color=#cc0000>ClientID:
"+dgSelected.ClientID+"</font></td></tr>"); // debug
dgSelected.DataSource = adoApplicationReader;
dgSelected.DataBind();

}
}
adoApplicationReader.Close();
}
}
}
}
else
{
lblPrompt.Text = "No records returned for this search. Please try
again.";
}
// tidy up before the garbage collector gets a chance to do it...
adoApplicantReader.Close();
}
else
{
lblPrompt.Text = "DATABASE ERROR:<br/><font color=#ff0000>\"" +
objDuplicatePartiesApplicant.NativeError + "\"</font><br/>Please
report this error to AESIS support.";
}
}
// ######### CODE-BEHIND ENDS ###########

The whole thing looks ugly to me because I have to keep on getting
fresh data, is there a more flexible cursor that I can use?

My next step is to investigate using DataSets, combining the XML DOMs
and doing all the manipulation of the presentation layer via
client-side JavaScript. Is this the recommended method?

Thanks,
oafyuf
Nov 17 '05 #1
1 2209
Hi Oafyuf,

You need to use the datagrid's ItemDataBound event to populate the inner
datalist. Here is a sample based on the Pubs sample database.

I placed a datagrid on a form and added a template column (the rest are
autogenerate). I added a datalist to the template column. Then I added the
following code.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Bind()
End If
End Sub

Private Sub Bind()
Dim Qry1 As System.Data.SqlClient.SqlDataReader
Dim connectionString As String = "server='localhost';
trusted_connection=true; Database='pubs'"
Dim sqlConnection As System.Data.SqlClient.SqlConnection = New
System.Data.SqlClient.SqlConnection(connectionStri ng)
Dim queryString As String = "SELECT au_id, au_lname, au_fname FROM
authors"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New
System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
sqlConnection.Open()
Qry1 =
sqlCommand.ExecuteReader(System.Data.CommandBehavi or.CloseConnection)
DataGrid1.DataSource = Qry1
DataGrid1.DataBind()
Qry1.Close()
sqlCommand.Dispose()
sqlConnection.Close()
sqlConnection.Dispose()
End Sub

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
Dim list As ListBox
list = e.Item.Cells(0).Controls(1)
Dim Qry1 As System.Data.SqlClient.SqlDataReader
Dim connectionString As String = "server='localhost';
trusted_connection=true; Database='pubs'"
Dim sqlConnection As System.Data.SqlClient.SqlConnection = New
System.Data.SqlClient.SqlConnection(connectionStri ng)
Dim queryString As String = "SELECT title FROM titles t inner
join titleauthor ta on t.title_id = ta.title_id inner join authors a on
ta.au_id = a.au_id where a.au_id = '" & e.Item.Cells(1).Text & "'"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New
System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
sqlConnection.Open()
Qry1 =
sqlCommand.ExecuteReader(System.Data.CommandBehavi or.CloseConnection)
list.DataSource = Qry1
list.DataTextField = "title"
list.DataBind()
If list.Items.Count = 0 Then list.Visible = False
list.Height = list.Height.Point(30 * list.Items.Count)
Qry1.Close()
sqlCommand.Dispose()
sqlConnection.Close()
sqlConnection.Dispose()
End If
End Sub

---
I hope this helps.

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer’s security.

This posting is provided "AS IS", with no warranties, and confers no rights.
--------------------
From: oa****@hotmail.com (oafyuf)
Newsgroups: microsoft.public.dotnet.framework.aspnet
Subject: Databinding by ClientID
Date: 24 Oct 2003 04:24:41 -0700
Organization: http://groups.google.com
Lines: 138
Message-ID: <ea**************************@posting.google.com >
NNTP-Posting-Host: 212.137.57.41
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google.com 1066994681 30561 127.0.0.1 (24 Oct 2003 11:24:41 GMT) X-Complaints-To: gr**********@google.com
NNTP-Posting-Date: Fri, 24 Oct 2003 11:24:41 +0000 (UTC)
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed 00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!postnew s1.google.com!no
t-for-mail Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:186290
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

Hi,
I'm trying to learn ASP.NET by doing a pilot project:

I have a DataGrid which contains a nested DataList. I want to iterate
through the SQLDataReader for the DataGrid and populate each DataList
by binding to a fresh SQLDataReader. At the moment I can only bind to
the first instance of the DataList. If I could bind the data by the
ClientID (or UniqueID), I'm sure it would work. Is there a way of
doing this? Or maybe there is a better approach to getting nested data
from separate SQLDataReaders.

Here's the relevant part of the code with extraneous bits removed (I
hope the line breaks don't make it unreadable!):

// ######### ASPX ###########

<asp:DataGrid Border="1" Runat="server" Id="dgApplicants">
<Columns>
<asp:BoundColumn DataField="ApplicantRef" Visible="False" />

<%-- Some more BoundColumns and TemplateColumns --%>

<asp:TemplateColumn>
<ItemTemplate>
<tr>
<td width="30px">&nbsp;</td>
<td colspan="8">
<asp:DataList Border="0" Runat="server" Id="dgApplications"
AutoGenerateColumns="False" Height="0px" Width="100%"
AllowSorting="False">
<ItemTemplate>
<b>App&nbsp;<%# DataBinder.Eval(Container.DataItem,
"VendorNo")%>&nbsp;<%# DataBinder.Eval(Container.DataItem,
"ApplicationRef")%>&nbsp;v<%# DataBinder.Eval(Container.DataItem,
"Version")%>&nbsp;<%# DataBinder.Eval(Container.DataItem,
"Status")%></b>
</ItemTemplate>
</asp:DataList>
</td>
</tr>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

// ######### ASPX ENDS ###########

// ######### CODE-BEHIND ###########
//adoApplicantReader is the outer loop
//adoApplicationReader is the inner loop

if(objDuplicatePartiesApplicant.ListParties(inputS urname, inputTown,
inputMainCPH, inputBusinessName, inputPostcode, inputOrderBy, ref
adoApplicantReader) == MyAssembly.AESIS.ReturnCodes.Success)
{
// OK, the SQL data layer object returned "success", but did we get
some results?
if(adoApplicantReader.HasRows)
{
// successful search: hide prompt and display Update button
lblPrompt.Visible = false;
btnUpdate.Visible = true;

// databinding method
this.dgApplicants.DataSource = adoApplicantReader;
this.dgApplicants.DataBind();
adoApplicantReader.Close();

// with the firehose cursor (SQLDataReader), we have to do the query
again after it is bound to pass it's members...
if(objDuplicatePartiesApplicant.ListParties(inputS urname, inputTown,
inputMainCPH, inputBusinessName, inputPostcode, inputOrderBy, ref
adoApplicantReader) == MyAssembly.AESIS.ReturnCodes.Success)
{
// SQL data layer object returned "success"
if(adoApplicantReader.HasRows)
{
while (adoApplicantReader.Read())
{
int intOrd = adoApplicantReader.GetOrdinal("ApplicantRef");
int intApplicantRef = (int)adoApplicantReader.GetValue(intOrd);

SqlDataReader adoApplicationReader = null;

if(objDuplicatePartiesApplication.ListPartyApplica tions(intApplicantRef, ref adoApplicationReader) == MyAssembly.AESIS.ReturnCodes.Success)
{
// did we get some results?
if(adoApplicationReader.HasRows)
{
// find a reference to the nested DataList
foreach(DataGridItem dgItem in dgApplicants.Items)
{

DataList dgSelected =
(DataList)dgItem.FindControl("dgApplications");

// here I need to find the reference name of each nested
datagrid
// and assign data to that...
Response.Write("<tr><td><font color=#cc0000>ClientID:
"+dgSelected.ClientID+"</font></td></tr>"); // debug
dgSelected.DataSource = adoApplicationReader;
dgSelected.DataBind();

}
}
adoApplicationReader.Close();
}
}
}
}
else
{
lblPrompt.Text = "No records returned for this search. Please try
again.";
}
// tidy up before the garbage collector gets a chance to do it...
adoApplicantReader.Close();
}
else
{
lblPrompt.Text = "DATABASE ERROR:<br/><font color=#ff0000>\"" +
objDuplicatePartiesApplicant.NativeError + "\"</font><br/>Please
report this error to AESIS support.";
}
}
// ######### CODE-BEHIND ENDS ###########

The whole thing looks ugly to me because I have to keep on getting
fresh data, is there a more flexible cursor that I can use?

My next step is to investigate using DataSets, combining the XML DOMs
and doing all the manipulation of the presentation layer via
client-side JavaScript. Is this the recommended method?

Thanks,
oafyuf


Nov 17 '05 #2

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

Similar topics

4
by: John | last post by:
Hello all, I'm trying to display a dataset into a datagrid and format the datagrid columns but I'm having a hard time with it. I've tried the MSDN examples but they don't seem to work. ...
1
by: Maras | last post by:
Hello, as we know VB is not case sensivity. I have a following problem, I have to send to another server few parametrs by post or get method, one of them MUST be a "clientID", it's preety...
2
by: kw | last post by:
TextBox t=new TextBox(); Controls.Add(t); t.ID=t.ClientID; //reads: "_MyControl1__ctl16" And even in OnPreRender, I examine the Controls collection and verify that the ID of the TextBox is...
25
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
2
by: Neo Geshel | last post by:
After pouring over about a dozen sites that clearly dealt with ClientID all by itself, I came to the realization that about 2/3+ of them were doing it wrong. It is indeed impossible to grab the...
2
by: JJ | last post by:
Hi, Can anyone advise me how is the ClientID issued? I had a suite Controls which consist of a context menu (i.e. a group of tables, rows and cells situated in a class library) and a set of...
0
by: Swetha | last post by:
Hello I have a FormView with InsertItem, EditItem and ItemTemplates. Depending on from where the page is accessed, the page opens up in either Insert, Edit or ReadOnly mode. The FormView is...
9
by: J055 | last post by:
Hi I have a very simple configuration of the GridView with paging and sorting. When I do a postback the DataBinding event fires twice - in both the ProcessPostData and PreRender stages of the...
2
by: DC | last post by:
Hi, I am doing something like this in the ItemCreated event (ASP.Net 1.1): DataGridItem pagerRow = e.Item; TableCell pagerCell = pagerRow.Cells; Control addedControl = new Control();...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.