label in footertemplate of datagrid


R

rn5a

How can I place a Label control in the FooterTemplate of a DataList
control?

I tried this

<script runat="server">
Sub Page_Load(obj As Object, ea As EventArgs)
Dim dblTotal As Double
Dim sqlReader As SqlDataReader

dblTotal = 'calling a function which returns a Double
sqlReader = 'calling another function which returns
SqlDataReader

dlCart.DataSource = sqlReader
dlCart.DataBind
lblTotal.Text = dblTotal
End Sub
</script>

<form runat="server">
<asp:DataList ID="dlCart" runat="server">
<HeaderTemplate>
<table>
<th>ID</th>
<th>Product</th>
<th>Unit Price</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
.........
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
<td colspan=2>TOTAL:</td>
<td><asp:Label ID="lblTotal" runat="server"/></td>
</tr>
</table>
</FooterTemplate>
</asp:DataList>
</form>

But it throws the error

Name 'lblTotal' is not declared.

pointing to

lblTotal.Text = dblTotal

How do I add a Label within the FooterTemplate of a DataList so that
the total can be displayed under the Unit Price column? Or is there any
other way by which I can display the total under the Unit Price column
in the DataList?
 
Ad

Advertisements

R

rn5a

Eliyahu, could you please show me a small example of what you have
suggested? I haven't done anything like that anytime.

Eliyahu said:
Handle ItemDataBound event. Detect the footer item by the ItemType. Use
FindControl ("lblTotal") to locate the label inside the footer item.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


How can I place a Label control in the FooterTemplate of a DataList
control?

I tried this

<script runat="server">
Sub Page_Load(obj As Object, ea As EventArgs)
Dim dblTotal As Double
Dim sqlReader As SqlDataReader

dblTotal = 'calling a function which returns a Double
sqlReader = 'calling another function which returns
SqlDataReader

dlCart.DataSource = sqlReader
dlCart.DataBind
lblTotal.Text = dblTotal
End Sub
</script>

<form runat="server">
<asp:DataList ID="dlCart" runat="server">
<HeaderTemplate>
<table>
<th>ID</th>
<th>Product</th>
<th>Unit Price</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
........
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
<td colspan=2>TOTAL:</td>
<td><asp:Label ID="lblTotal" runat="server"/></td>
</tr>
</table>
</FooterTemplate>
</asp:DataList>
</form>

But it throws the error

Name 'lblTotal' is not declared.

pointing to

lblTotal.Text = dblTotal

How do I add a Label within the FooterTemplate of a DataList so that
the total can be displayed under the Unit Price column? Or is there any
other way by which I can display the total under the Unit Price column
in the DataList?
 
R

rn5a

OK...Eliyahu...I got it...this is it...(for those who might find this
useful)

<script runat="server">
Sub Page_Load(obj As Object, ea As EventArgs)
Dim sqlReader As SqlDataReader

sqlReader = 'calling another function which returns
SqlDataReader

dlCart.DataSource = sqlReader
dlCart.DataBind
End Sub

Sub BindItem(ByVal obj As Object, ByVal ea As
DataListItemEventArgs)
Dim dblTotal As Double

dblTotal = 'calling a function which returns a Double

If (ea.Item.ItemType = ListItemType.Footer) Then
CType(ea.Item.FindControl("lblTotal"), Label).Text =
dblTotal
End If
End Sub
</script>

<form runat="server">
<asp:DataList ID="dlCart" OnItemDataBound="BindItem" runat="server">
<HeaderTemplate>
<table>
<th>ID</th>
<th>Product</th>
<th>Unit Price</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
.........
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
<td colspan=2>TOTAL:</td>
<td><asp:Label ID="lblTotal" runat="server"/></td>
</tr>
</table>
</FooterTemplate>
</asp:DataList>
</form>

Thanks, Eliyahu, for your useful suggestion...............:)

Eliyahu said:
Handle ItemDataBound event. Detect the footer item by the ItemType. Use
FindControl ("lblTotal") to locate the label inside the footer item.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


How can I place a Label control in the FooterTemplate of a DataList
control?

I tried this

<script runat="server">
Sub Page_Load(obj As Object, ea As EventArgs)
Dim dblTotal As Double
Dim sqlReader As SqlDataReader

dblTotal = 'calling a function which returns a Double
sqlReader = 'calling another function which returns
SqlDataReader

dlCart.DataSource = sqlReader
dlCart.DataBind
lblTotal.Text = dblTotal
End Sub
</script>

<form runat="server">
<asp:DataList ID="dlCart" runat="server">
<HeaderTemplate>
<table>
<th>ID</th>
<th>Product</th>
<th>Unit Price</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
........
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
<td colspan=2>TOTAL:</td>
<td><asp:Label ID="lblTotal" runat="server"/></td>
</tr>
</table>
</FooterTemplate>
</asp:DataList>
</form>

But it throws the error

Name 'lblTotal' is not declared.

pointing to

lblTotal.Text = dblTotal

How do I add a Label within the FooterTemplate of a DataList so that
the total can be displayed under the Unit Price column? Or is there any
other way by which I can display the total under the Unit Price column
in the DataList?
 
E

Eliyahu Goldin

Handle ItemDataBound event. Detect the footer item by the ItemType. Use
FindControl ("lblTotal") to locate the label inside the footer item.
 
E

Eliyahu Goldin

<asp:DataList ID="dlCart" runat="server"
OnItemDataBound="dlCart_ItemDataBound">

In the script:

Sub dlCart_ItemDataBound(sender As Object, e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Footer
Dim dblTotal As Double
dblTotal = 'calling a function which returns a Double
Dim TotalLabel As Label = CType(e.Item.FindControl("lblTotal"),
Label)
TotalLabel .Text = dblTotal
End If
End Sub


--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


Eliyahu, could you please show me a small example of what you have
suggested? I haven't done anything like that anytime.

Eliyahu said:
Handle ItemDataBound event. Detect the footer item by the ItemType. Use
FindControl ("lblTotal") to locate the label inside the footer item.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


How can I place a Label control in the FooterTemplate of a DataList
control?

I tried this

<script runat="server">
Sub Page_Load(obj As Object, ea As EventArgs)
Dim dblTotal As Double
Dim sqlReader As SqlDataReader

dblTotal = 'calling a function which returns a Double
sqlReader = 'calling another function which returns
SqlDataReader

dlCart.DataSource = sqlReader
dlCart.DataBind
lblTotal.Text = dblTotal
End Sub
</script>

<form runat="server">
<asp:DataList ID="dlCart" runat="server">
<HeaderTemplate>
<table>
<th>ID</th>
<th>Product</th>
<th>Unit Price</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
........
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
<td colspan=2>TOTAL:</td>
<td><asp:Label ID="lblTotal" runat="server"/></td>
</tr>
</table>
</FooterTemplate>
</asp:DataList>
</form>

But it throws the error

Name 'lblTotal' is not declared.

pointing to

lblTotal.Text = dblTotal

How do I add a Label within the FooterTemplate of a DataList so that
the total can be displayed under the Unit Price column? Or is there any
other way by which I can display the total under the Unit Price column
in the DataList?
 
R

rn5a

Eliyahu, can you please tell me why does ASP.NET throw the error

Name 'lblTotal' is not declared.

pointing to the

lblTotal.Text = dblTotal

line in the Page_Load sub when lblTotal is used within the
FooterTemplate of the DataList control?

Thanks


Eliyahu said:
<asp:DataList ID="dlCart" runat="server"
OnItemDataBound="dlCart_ItemDataBound">

In the script:

Sub dlCart_ItemDataBound(sender As Object, e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Footer
Dim dblTotal As Double
dblTotal = 'calling a function which returns a Double
Dim TotalLabel As Label = CType(e.Item.FindControl("lblTotal"),
Label)
TotalLabel .Text = dblTotal
End If
End Sub


--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


Eliyahu, could you please show me a small example of what you have
suggested? I haven't done anything like that anytime.

Eliyahu said:
Handle ItemDataBound event. Detect the footer item by the ItemType. Use
FindControl ("lblTotal") to locate the label inside the footer item.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


How can I place a Label control in the FooterTemplate of a DataList
control?

I tried this

<script runat="server">
Sub Page_Load(obj As Object, ea As EventArgs)
Dim dblTotal As Double
Dim sqlReader As SqlDataReader

dblTotal = 'calling a function which returns a Double
sqlReader = 'calling another function which returns
SqlDataReader

dlCart.DataSource = sqlReader
dlCart.DataBind
lblTotal.Text = dblTotal
End Sub
</script>

<form runat="server">
<asp:DataList ID="dlCart" runat="server">
<HeaderTemplate>
<table>
<th>ID</th>
<th>Product</th>
<th>Unit Price</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
........
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
<td colspan=2>TOTAL:</td>
<td><asp:Label ID="lblTotal" runat="server"/></td>
</tr>
</table>
</FooterTemplate>
</asp:DataList>
</form>

But it throws the error

Name 'lblTotal' is not declared.

pointing to

lblTotal.Text = dblTotal

How do I add a Label within the FooterTemplate of a DataList so that
the total can be displayed under the Unit Price column? Or is there any
other way by which I can display the total under the Unit Price column
in the DataList?
 
Ad

Advertisements

E

Eliyahu Goldin

You can't refer to controls in templates directly by their id.Only
FindControl in the item events will do.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


Eliyahu, can you please tell me why does ASP.NET throw the error

Name 'lblTotal' is not declared.

pointing to the

lblTotal.Text = dblTotal

line in the Page_Load sub when lblTotal is used within the
FooterTemplate of the DataList control?

Thanks


Eliyahu said:
<asp:DataList ID="dlCart" runat="server"
OnItemDataBound="dlCart_ItemDataBound">

In the script:

Sub dlCart_ItemDataBound(sender As Object, e As
DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Footer
Dim dblTotal As Double
dblTotal = 'calling a function which returns a Double
Dim TotalLabel As Label =
CType(e.Item.FindControl("lblTotal"),
Label)
TotalLabel .Text = dblTotal
End If
End Sub


--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


Eliyahu, could you please show me a small example of what you have
suggested? I haven't done anything like that anytime.

Eliyahu Goldin wrote:
Handle ItemDataBound event. Detect the footer item by the ItemType.
Use
FindControl ("lblTotal") to locate the label inside the footer item.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


How can I place a Label control in the FooterTemplate of a DataList
control?

I tried this

<script runat="server">
Sub Page_Load(obj As Object, ea As EventArgs)
Dim dblTotal As Double
Dim sqlReader As SqlDataReader

dblTotal = 'calling a function which returns a Double
sqlReader = 'calling another function which returns
SqlDataReader

dlCart.DataSource = sqlReader
dlCart.DataBind
lblTotal.Text = dblTotal
End Sub
</script>

<form runat="server">
<asp:DataList ID="dlCart" runat="server">
<HeaderTemplate>
<table>
<th>ID</th>
<th>Product</th>
<th>Unit Price</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
........
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
<td colspan=2>TOTAL:</td>
<td><asp:Label ID="lblTotal" runat="server"/></td>
</tr>
</table>
</FooterTemplate>
</asp:DataList>
</form>

But it throws the error

Name 'lblTotal' is not declared.

pointing to

lblTotal.Text = dblTotal

How do I add a Label within the FooterTemplate of a DataList so that
the total can be displayed under the Unit Price column? Or is there
any
other way by which I can display the total under the Unit Price
column
in the DataList?
 
Ad

Advertisements

R

rn5a

But why can't controls be referred to by their IDs in templates?

Microsoft shouldn't have done this. It has made life all the more
tougher for ASP.NET developers.....


Eliyahu said:
You can't refer to controls in templates directly by their id.Only
FindControl in the item events will do.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


Eliyahu, can you please tell me why does ASP.NET throw the error

Name 'lblTotal' is not declared.

pointing to the

lblTotal.Text = dblTotal

line in the Page_Load sub when lblTotal is used within the
FooterTemplate of the DataList control?

Thanks


Eliyahu said:
<asp:DataList ID="dlCart" runat="server"
OnItemDataBound="dlCart_ItemDataBound">

In the script:

Sub dlCart_ItemDataBound(sender As Object, e As
DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Footer
Dim dblTotal As Double
dblTotal = 'calling a function which returns a Double
Dim TotalLabel As Label =
CType(e.Item.FindControl("lblTotal"),
Label)
TotalLabel .Text = dblTotal
End If
End Sub


--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


Eliyahu, could you please show me a small example of what you have
suggested? I haven't done anything like that anytime.

Eliyahu Goldin wrote:
Handle ItemDataBound event. Detect the footer item by the ItemType.
Use
FindControl ("lblTotal") to locate the label inside the footer item.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


How can I place a Label control in the FooterTemplate of a DataList
control?

I tried this

<script runat="server">
Sub Page_Load(obj As Object, ea As EventArgs)
Dim dblTotal As Double
Dim sqlReader As SqlDataReader

dblTotal = 'calling a function which returns a Double
sqlReader = 'calling another function which returns
SqlDataReader

dlCart.DataSource = sqlReader
dlCart.DataBind
lblTotal.Text = dblTotal
End Sub
</script>

<form runat="server">
<asp:DataList ID="dlCart" runat="server">
<HeaderTemplate>
<table>
<th>ID</th>
<th>Product</th>
<th>Unit Price</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
........
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
<td colspan=2>TOTAL:</td>
<td><asp:Label ID="lblTotal" runat="server"/></td>
</tr>
</table>
</FooterTemplate>
</asp:DataList>
</form>

But it throws the error

Name 'lblTotal' is not declared.

pointing to

lblTotal.Text = dblTotal

How do I add a Label within the FooterTemplate of a DataList so that
the total can be displayed under the Unit Price column? Or is there
any
other way by which I can display the total under the Unit Price
column
in the DataList?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top