Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
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 July 15th, 2008, 08:31 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default How to get text of gridview buttonfield

I have a gridview and I am able to get cell values for bound fields like this:

Code:
If e.CommandName = "description" Then
            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim mySelectedRow As GridViewRow
            mySelectedRow = GridView1.Rows(index)
            Dim mySelectedCell As TableCell
            mySelectedCell = mySelectedRow.Cells(3)
            Dim jobtext As String
            jobtext = mySelectedCell.Text
End If
Now I need to get the value for a Button Field and not sure how to do this. Here is the code I have:

Code:
Dim index = Convert.ToInt32(e.CommandArgument)
Dim mySelectedRow As GridViewRow = GridView1.Rows(index)
Dim myButtonField As LinkButton = mySelectedRow.FindControl("descrption")
Dim jobtext As String = myButtonField.Text.ToString
Get error (on last line in code): Object reference not set to an instance of an object.

Can somebody help please. Thank you.
 
Old July 20th, 2008, 04:09 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anubhav.kumar
Default

Hi,

Instead of

Dim myButtonField As LinkButton = mySelectedRow.FindControl("descrption")

Please try

Dim myButtonField As LinkButton = mySelectedRow.Cells(indexOfButtonField).FindContro l("descrption")





Anubhav Kumar
http://anksaxena.blogspot.com
 
Old July 21st, 2008, 11:48 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
Default

you need to take it one step farther and change the type of control to a linkbutton

Dim index = Convert.ToInt32(e.CommandArgument)
Dim mySelectedRow As GridViewRow = GridView1.Rows(index)
Dim myButtonField As LinkButton = ctype(mySelectedRow.FindControl("descrption"), LinkButton)
Dim jobtext As String = myButtonField.Text.ToString

and you can do this even easier:

Dim index = Convert.ToInt32(e.CommandArgument)
Dim mySelectedRow As GridViewRow = GridView1.Rows(index)
Dim jobtext As String = ctype(mySelectedRow.FindControl("descrption"), LinkButton).Text.ToString

Jason Hall





Similar Threads
Thread Thread Starter Forum Replies Last Post
Making Text Flash in a gridview Andrew.Berry ASP.NET 2.0 Basics 2 February 14th, 2008 09:57 AM
To get text of ButtonField column in GridView nitinp ASP.NET 2.0 Professional 11 May 26th, 2007 12:55 PM
Chnage text of a buttonfield GS ASP.NET 2.0 Basics 1 March 16th, 2007 09:07 AM
Change name of buttonfield on click GS ASP.NET 2.0 Professional 0 March 15th, 2007 11:34 AM
To get text of Button Field column in GridView nitinp ASP.NET 2.0 Basics 1 November 3rd, 2006 10:57 AM





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