Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 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 Professional 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 November 4th, 2006, 12:39 AM
Authorized User
 
Join Date: Aug 2006
Posts: 39
Thanks: 1
Thanked 0 Times in 0 Posts
Default To get text of ButtonField column in GridView

:)Hello,

I have designed a GridView with two columns. One is ButtonField type which is bound with "Customer_Name" field and second BoundColumn type with "Company" field.

I want to store text of cell of ButtonField column of any row. My code is as below.

private void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
  if (e.CommandName == "Select")
  {
    int index = Convert.ToInt32(e.CommandArgument);
    GridViewRow selectedRow = CustomersGridView.Rows[index];
    string strValue = selectedRow.Cells[1].Text;

    Response.Write(strValue);
    //As above line returns the text of column 2. Here I want to return text of column 1. How?????
   }
}

Please give me the solution. Thanks.:)


 
Old November 4th, 2006, 05:40 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Maybe I am missing something, but shouldn't you just change this:

 string strValue = selectedRow.Cells[1].Text;


to this:

 string strValue = selectedRow.Cells[0].Text;

This gets the text from the first cell.

If that doesn't help, can you elaborate a bit about what you're trying to accomplish?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old November 4th, 2006, 05:51 AM
Authorized User
 
Join Date: Aug 2006
Posts: 39
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I already have tried following

string strValue = selectedRow.Cells[0].Text; but it did not work.

I just want to store text of cell of "Customer_Name" column in a variable, when I click on any cell from the ButtonField column. I don't want to use hidden column for that.

 
Old November 4th, 2006, 05:53 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Can you define "did not work"? What do you get?

What exactly are you trying to do? Do you want to get the text on the button?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old November 6th, 2006, 01:15 AM
Authorized User
 
Join Date: Aug 2006
Posts: 39
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I have a GridView like following. Column 1 is ButtonField, 2 is BoundField.

Customer Company
----------- ------------
Mike Company1
Tom Company2
Jack Company3
... ....

Since "Customer" column is obtained by binding data to a ButtonField type column. As I click on "Tom", I want to store the text "Tom" in a variable.

If I write: string strValue = selectedRow.Cells[0].Text;
It returns blank string as strValue ="". Which I don't want.
I want to get some how,

strValue="Tom"

If I take a hidden TemplateField type column and write
strValue=((Label)selectedRow.Cells[2].Controls[1]).Text;
It gives me what I want, but in a GridView with 20 columns I don't want to do like this.



 
Old November 6th, 2006, 03:00 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Right, I see.

If it's a button (a LinkButton or a normal Button), you should be able to use FIndControl to find the button and then get its text, like this (untested)

LinkButton myButton = selectedRow.FindControl("NameOfButton") as LinkButton;

if (myButton != null)
{
  custName = myButton.Text;
}

Instead of using FindControl,

  selectedRow.Cells[0].Controls[0]

might work as well.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old November 6th, 2006, 04:32 AM
Authorized User
 
Join Date: Aug 2006
Posts: 39
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Wow!!! thanks. It is working fine.
Thanks again.

 
Old November 6th, 2006, 04:40 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

For the benefit of the archive of this forum, would you mind posting your final solution??

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old November 8th, 2006, 05:42 AM
Authorized User
 
Join Date: Aug 2006
Posts: 39
Thanks: 1
Thanked 0 Times in 0 Posts
Default

My final working code is as below:

private void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Select")
    {
       string strValue="";
       int index = Convert.ToInt32(e.CommandArgument);
       GridViewRow selectedRow = CustomersGridView.Rows[index];
       strValue = ((LinkButton)(selectedRow.Cells[0].Controls[0])).Text;

       Response.Write(strValue);
    }
}

All thanks to Imar.

 
Old May 26th, 2007, 12:04 PM
Registered User
 
Join Date: May 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi nitinp: What I need is slightly different with yours, I need to get the bound field's data to a string, when user click on the Buttonfield. You said that you could get it by doing: string strValue = selectedRow.Cells[1].Text;

But I got empty string returned. Would you please let me know how you get it?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Get GridView Cell Value Based on GridView Column stublair C# 2008 aka C# 3.0 0 September 4th, 2008 08:30 AM
How to get text of gridview buttonfield snufse ASP.NET 2.0 Basics 2 July 21st, 2008 11:48 AM
Chnage text of a buttonfield GS ASP.NET 2.0 Basics 1 March 16th, 2007 09:07 AM
To get text of Button Field column in GridView nitinp ASP.NET 2.0 Basics 1 November 3rd, 2006 10:57 AM
How to grab a column value from a GridView convergent7 ASP.NET 2.0 Basics 0 July 14th, 2006 12:45 AM





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