Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 28th, 2008, 08:36 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: West Palm Beach, Florida, USA.
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default [Resolved] GridView TemplateField - get value

I have a GridView and have converted a BoundField into a TemplateField.

In my GridView I have 2 text fields, one short and a one long. When I load the GridView I show the short text field as a LinkButton and the long text field as an invisible TeplAteField. When I click on the LinkButton I need to retrieve the long text.

This is the markup code being created:

:):)


Code:
<asp:TemplateField HeaderText="Notes1" Visible="False">
                       <EditItemTemplate>
                           <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("item_notes1") %>'></asp:TextBox>
                       </EditItemTemplate>
                       <ItemTemplate>
                           <asp:Label ID="Label1" runat="server" Text='<%# Bind("item_notes1") %>'></asp:Label>
                       </ItemTemplate>
                   </asp:TemplateField>



Here is my code for the click on the linkbutton, need the code to retrieve the TemplateField value. My code does not work


Code:
Protected Sub GridView2_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView2.RowCommand

If e.CommandName = "notes" Then

Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim mySelectedRow As GridViewRow = GridView2.Rows(index)
            Dim mySelectedCell As TableCell

            mySelectedCell = mySelectedRow.Cells(6)
            Dim MyText As String = mySelectedCell.Text


:):):)
Reply With Quote
  #2 (permalink)  
Old July 28th, 2008, 08:42 AM
Friend of Wrox
 
Join Date: Nov 2007
Location: Central Florida, USA.
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
Default

do something like:

Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim mySelectedRow As GridViewRow = GridView2.Rows(index)

mySelectedCell = mySelectedRow.FindControl("Label1")

Dim MyText As String = CType(mySelectedCell, TextBox).Text




Jason Hall
Reply With Quote
  #3 (permalink)  
Old July 28th, 2008, 09:16 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: West Palm Beach, Florida, USA.
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Jason, thank you. Get some errors:

mySelectedCell = mySelectedRow.FindControl("Label1")
RuntimeError:
Unable to cast object of type 'System.Web.UI.WebControls.Label' to type 'System.Web.UI.WebControls.TableCell'.


Syntax error:
mySelectedCell = mySelectedRow.FindControl("Label1")
Value of type "System.Web.UI.Webcontrol.TableCell" cannot be converted to "System.Web.Ui.Webcontrol.Textbox'
Reply With Quote
  #4 (permalink)  
Old July 28th, 2008, 09:19 AM
Friend of Wrox
 
Join Date: Nov 2007
Location: Central Florida, USA.
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
Default

sorry my fault:

Try this

Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim mySelectedRow As GridViewRow = GridView2.Rows(index)
Dim MyText As String = CType(mySelectedRow.FindControl("Label1"), TextBox).Text

Jason Hall
Reply With Quote
  #5 (permalink)  
Old July 28th, 2008, 10:42 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: West Palm Beach, Florida, USA.
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Jason, see below, thank you.


Dim MyText As String = CType(mySelectedRow.FindControl("Label1"), TextBox).Text

Get error:
Unable to cast object of type 'System.Web.UI.WebControls.Label' to type 'System.Web.UI.WebControls.TextBox'.

Reply With Quote
  #6 (permalink)  
Old July 28th, 2008, 10:44 AM
Friend of Wrox
 
Join Date: Nov 2007
Location: Central Florida, USA.
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
Default

I'm having a brain fart kinda day. change where it says textbox to label because the control you
are pulling the text from is a label

Dim MyText As String = CType(mySelectedRow.FindControl("Label1"), Label).Text

Jason Hall
Reply With Quote
  #7 (permalink)  
Old July 28th, 2008, 11:24 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: West Palm Beach, Florida, USA.
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Jason, that worked super, thank you. That saves me an extra trip to a table to get the info. Now, is it possible to show the text in a tooltip when I click on cell 5 (which is a link button) and have text in cell 6 show in the tool tip? Can you share some ideas here please?

Reply With Quote
  #8 (permalink)  
Old July 28th, 2008, 11:30 AM
Friend of Wrox
 
Join Date: Nov 2007
Location: Central Florida, USA.
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
Default

you do it very similarly

CType(mySelectedRow.FindControl("LinkButton1"), LinkButton).ToolTip = MyText

how ever if you want it to always be the hidden text with out even selecting the row. You would set
that in your aspx page

something like

<asp:TemplateField HeaderText="Link">
     <ItemTemplate>
         <asp:Label ID="LinkButton1" runat="server" Text="Click Here" ToolTip='<%# Eval("item_notes1") %>'></asp:Label>
     </ItemTemplate>
</asp:TemplateField>

basically you can use the bound data from your data source in all of your template fields?

I think this is what you mean right.

Jason Hall
Reply With Quote
  #9 (permalink)  
Old July 28th, 2008, 11:32 AM
Friend of Wrox
 
Join Date: Nov 2007
Location: Central Florida, USA.
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
Default

sorry i just copied with out fixing everything:

<asp:TemplateField HeaderText="Link">
     <ItemTemplate>
         <asp:LinkButton ID="LinkButton1" runat="server" Text="Click Here" ToolTip='<%# Eval("item_notes1") %>'></asp:LinkButton>
     </ItemTemplate>
</asp:TemplateField>

Jason Hall
Reply With Quote
  #10 (permalink)  
Old July 28th, 2008, 12:14 PM
Friend of Wrox
 
Join Date: Mar 2007
Location: West Palm Beach, Florida, USA.
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Here is what I have:

GridView with 6 columns:
Column 1 = Date (BoundFiled)
Column 2 = Job Number (Boundfield)
Column 3 = Quantity (BoundField)
Column 4 = Cost Center (Boundfield)
Column 5 = Short Text - 20 chararacters (LinkButton)
Column 6 = Long Text - 200 characters (hidden TemplateField)

The idea was to click on column 5 and then show the lon text from column 6 in a ListBox pop up. This works now fine (with the code you gave me !!). The problem is that ListBox does not wrap.

So what I was thinking was to do "mouse over" or "click" on cell 5 and show the long text in tool tip from cloumn 6. This is the code snippet I am looking for. (Do I need to change column 5 back to a boundfield?) Thank you.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Resolved] GridView to Excel snufse ASP.NET 2.0 Basics 2 October 28th, 2008 02:37 PM
[Resolved] GridView Sorting and Paging snufse ASP.NET 2.0 Basics 1 October 9th, 2008 07:45 AM
Can not Edit gridview having TemplateField coolcoder2007 .NET Framework 2.0 4 March 6th, 2008 06:36 AM
[Resolved] IListSource problem in GridView snufse ASP.NET 2.0 Basics 5 February 12th, 2008 01:42 PM
Enable Editing on Gridview - RESOLVED Andrew.Berry ASP.NET 2.0 Basics 0 October 3rd, 2007 05:29 AM



All times are GMT -4. The time now is 10:25 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.