Link to home
Start Free TrialLog in
Avatar of blitzzy
blitzzy

asked on

Getting value from selected ButtonField in GridView

Hi Experts,

I have a GridView that is populated with results from a stored procedure. The first column in the grid is a ButtonField containing the ID (integer) pulled from the stored procedure. I have set this ButtonField to act as a link that will either open a new page for the file or redirect to a iframe.

My problem is, I cannot get the ID from the ButtonField. I have tried numerous ways but keep receiving a null string value.

I'm new to ASP.NET, so please help and provide me with some explanations if you can. Thanks!
' CODE ATTEMPT # 1
Protected Sub GridView1_RowCommand(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        TextBox1.Text = GridView1.Rows(e.CommandArgument).Cells(0).Text
 
' CODE ATTEMPT # 2
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
        Dim SelectedRowIndex As Integer
 
        SelectedRowIndex = GridView1.SelectedIndex
        TextBox1.Text = GridView1.Rows(SelectedRowIndex).Cells(0).Text

Open in new window

Avatar of Velio
Velio
Flag of South Africa image

the button is a control, therefore Cells(0).Text will return ""
but if you use GridView.Rows(SelectedRowIndex).FindControl("yourButtonsID").Text, you should be able to get the text.
Avatar of blitzzy
blitzzy

ASKER

Velio:
How do I set the ID for the button? Which property do I use in the source code?
ASKER CERTIFIED SOLUTION
Avatar of Velio
Velio
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of blitzzy

ASKER

Your suggestion worked like a charm. I did think about using CType, but I never would have thought to cast it as a LinkButton. Thank you!

Now, if only I can work out my other error connecting to my web service. I think I will have to create another question for that.