Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics 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
 
Old March 4th, 2006, 10:42 PM
Authorized User
 
Join Date: Jan 2006
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
Default Functions in Item Templates

I'm working on a feature using a gridview that has this requirement:

> determine an item template label value
> by performing a function
> that takes as input
> two other item template label values.

I'm thinking the way to make this work is based on the type of construct shown below (The function would be different in the actual feature). This test case works with labels 1,2, and 4. However, I don't know the right syntax for Label 5, where I attempt to use two of the other label values as input to the function.

Any help with how to solve this problem would be appreciated.

Thanks!

=================
Public Function TestFunction(ByVal varA, ByVal varB)
       Return varA & varB
End Function
=================

<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text= '<%# DataBinder.Eval(Container.DataItem, "Field1") %>'></asp:Label>
 <asp:Label ID="Label2" runat="server" Text= '<%# DataBinder.Eval(Container.DataItem, "Field2") %>'>
</asp:Label>
<asp:Label ID="Label4" runat="server" Text= '<%# TestFunction("Hi ", "There") %>'></asp:Label>

<asp:Label ID="Label5" runat="server" Text= '<%# TestFunction((Container.DataItem, "Field1"),(Container.DataItem, "Field2")) %>'></asp:Label>

</ItemTemplate>
=================
 
Old March 4th, 2006, 11:45 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

I would use the ItemDataBound event of the gridview.

Get a refence to each label. Then call the function with the text of the labels(params) and set that result to the text of the result label.

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim lbl1, lbl2, lbl4, lbl5 As New Label
            lbl1 = e.Row.FindControl("Label1")
            lbl2 = e.Row.FindControl("Label2")
            lbl4 = e.Row.FindControl("Label4")
            lbl4.Text = TestFunction("Hi", "There")
            lbl5 = e.Row.FindControl("Label5")
            lbl5.Text = TestFunction(lbl1.Text, lbl2.Text)
        End If
    End Sub

Jim

 
Old March 6th, 2006, 12:29 AM
Authorized User
 
Join Date: Jan 2006
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the response. That looks just right. 'Preciate it!
 
Old March 6th, 2006, 12:36 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

No problem.. hope it works for you

 
Old March 7th, 2006, 10:16 PM
Authorized User
 
Join Date: Jan 2006
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think it was the right direction, but I couldn't get the find controls to come up with anything, through several variations.

This code in Gridview_RowDataBound:
-----------
If e.Row.RowType = DataControlRowType.DataRow Then
Debug.WriteLine(e.Row.Cells.Count & " " & e.Row.Cells(0).Controls.Count) in Gridview_RowDataBound
-----------
says 1 cell and 5 controls. The one cell is correct,
but I've got 3 html image buttons, 4 visible asp labels, and 3 hidden fields in the itemtemplate, and they show up in the DHMTL when the page is rendered, as being within the cell.

Another way might be to go through e.Row.DataItem, but I don't know the syntax there, either.

Any other thoughts?

Thanks!
 
Old March 8th, 2006, 12:53 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

   If you have the label names as you described in your original post, and they are in a template column, my code will work. As for the HTML controls, you will not be able to access them that way, unless you convert them to server controls using runat="server" in the HTML.

 
Old March 8th, 2006, 11:36 AM
Authorized User
 
Join Date: Jan 2006
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the message. I'll play with the findcontrols some more.
 
Old March 8th, 2006, 05:07 PM
Authorized User
 
Join Date: Jan 2006
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
Default

FYI, I've found an alternate solution to the particular need I had, of the form

<asp:Label ID="Lvl" runat="server" cssClass="indent01"
Width='<%# (GetIndentWidth(Container.DataItem("Lvl"))) %>' Text= ' '>

So, given my overly driven development cycle, I may not be testing your particular approach anytime soon. However, I appreciate the suggestion; prompts are helpful and this one led me to learn some things about _RowDataBound. I expect I'll be finding other needs in this area later in the dev cycle, so I look forward to getting .FindControl to work then. Cheers!
 
Old March 9th, 2006, 12:09 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Glad to help.. The code is useful..I use it often for datagrids and other controls of that type..

Good luck...

Jim






Similar Threads
Thread Thread Starter Forum Replies Last Post
Aligning Header & Item DataList Templates rsearing ASP.NET 2.0 Basics 2 October 5th, 2007 06:09 AM
Item Templates hbansal ASP.NET 1.0 and 1.1 Professional 6 August 2nd, 2007 06:24 AM
"templates" Somesh ASP.NET 2.0 Professional 8 March 27th, 2007 08:53 AM
Want Combo Box first item if only 1 item markw707 Access 3 June 9th, 2004 04:03 PM
Find Out First Item of each item Group Jane SQL Language 1 November 22nd, 2003 12:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.