|
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
|
|
|
November 4th, 2006, 12:39 AM
|
Authorized User
|
|
Join Date: Aug 2006
Posts: 39
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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.:)
|
November 4th, 2006, 05:40 AM
|
|
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
November 4th, 2006, 05:51 AM
|
Authorized User
|
|
Join Date: Aug 2006
Posts: 39
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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.
|
November 4th, 2006, 05:53 AM
|
|
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
November 6th, 2006, 01:15 AM
|
Authorized User
|
|
Join Date: Aug 2006
Posts: 39
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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.
|
November 6th, 2006, 03:00 AM
|
|
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
November 6th, 2006, 04:32 AM
|
Authorized User
|
|
Join Date: Aug 2006
Posts: 39
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Wow!!! thanks. It is working fine.
Thanks again.
|
November 6th, 2006, 04:40 AM
|
|
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
November 8th, 2006, 05:42 AM
|
Authorized User
|
|
Join Date: Aug 2006
Posts: 39
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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.
|
May 26th, 2007, 12:04 PM
|
Registered User
|
|
Join Date: May 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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?
|
|
|