Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning name of button in a datagrid

Status
Not open for further replies.

jshurst

Programmer
Oct 27, 2004
1,158
US
I have a button in a datagrid. I have the button displaying a value from a database. I would like to be able to extract this value when I click the button.

(I'm attempting to open a file saved on the web servers hard drive, or at least prompt the user to save it). Can someone tell me how to get the text value from the button?

Thanks.
 
Have a look into the DataGrid's ItemCommand method. Depending on what you are doing in your DataGrid, and how you've set it up, this event should fire when the Button is clicked.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
right, this button does fire, however, I need to reference the text of the button. Kind of like when you have a hyperlink column and when you click on it it passeses the parameter. This parameter is based on the row that I click.

I want to do the same thing, except I would like to open a file saved on the web server. I might can do this with a template column, I just thought it would be easier with a button.

My question is, how do I get the value (or text) of the button?
 
As ca8msm says.. use the ItemDataBound event.

You can check using:
e.CommandName
 
Something like:
Code:
private void YourDataGrid_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
	Button b = (Button)source;
	string myText = b.Text.ToString();
}


Vince
 
Thanks for the help, but I can't quite find what I'm looking for. I have some code in the event, but what I'm looking for is something like this...

Code:
Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommand
        If e.CommandName = "DocName" Then
            'need to open the file that this is pointed to.
            'I can run another sql command to reference the datakey, however what I need ("filename.doc" - or whatever it may be for the certain row) is already there (in the columns collection, text field - not the command name), I just don't know how to get it, or if I can get it this way.
        End If
End Sub

It's going to be something like... <a href="javascript:__doPostBack('DataGrid1$_ctl5$_ctl2','')">filename.doc</a>

I think I may go about this another way.
 
Ok. I changed my datakey field to the DocName field (which is also unique). For future reference, is the datakey field the only field that you can get the value of?

Code:
DataGrid1.DataKeys(e.Item.ItemIndex)
 
I don't really understand. Your original post said:
I have a button in a datagrid. I have the button displaying a value from a database. I would like to be able to extract this value when I click the button.
I pointed out the ItemCreated event and Veep's example does exactly what you asked for. Why can't you use that method?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Sorry, I must not have refreshed the page before I posted. On Veep's suggestion, what is the "source"?
 
It's just the sender object


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top