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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get ID from selected row in gridview using image button

Status
Not open for further replies.

primagic

IS-IT--Management
Jul 24, 2008
476
0
0
GB
I have a gridview with a template field column which has an image button using 'select' command.

Code:
<asp:TemplateField ShowHeader="False" ItemStyle-HorizontalAlign="Center">
                                <ItemTemplate>
                                    <asp:ImageButton ID="btnSelect" runat="server" CausesValidation="False" ImageUrl="images/select_icon.png"
                                        CommandName="Select" Text="Select" OnClick="LinkButton1_Click"   
                                    />
                                </ItemTemplate>
                            </asp:TemplateField>

I am trying to get the value of a hidden column from the selected row and parse it as a parameter to add it to a database.

In my code behind I have for getting value:

Code:
Protected Sub LinkButton1_Click(sender As Object, e As EventArgs)
        Dim clickedRow As GridViewRow = TryCast(DirectCast(sender, ImageButton).NamingContainer, GridViewRow)
        Dim lblID As Label = DirectCast(clickedRow.FindControl("CertificateID"), Label)
        lblSelectedRecord.text = lblID.Text
    End Sub

Using as a parameter in my stored procedure:
Code:
cmd.Parameters.Add("@CertificateID", SqlDbType.Int).Value = lblSelectedRecord.Text.Trim

I am getting the error 'Object reference not set to an instance of an object.' on the line
Code:
blSelectedRecord.text = lblID.Text

Any ideas
 
First you should be using the SelectdIndexChanging event of the gridview. There is also a SelectedIndexChanged, but I think you want the former.
Second, are you trying to get the ID of the row to do a Select or Update from the database?
 
This example is for a linkbutton
There is another parameter called commandArgument. In this case I am sending the value of two fieldsin the gridviewrow.
CommandArgument='<%# Container.DataItem("service_request_id") & "%" & Container.DataItem("Client_Id") %>'

You can parse the value you set this parameter to in the gridview_RowCommand event
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand

Dim wc As WebControl = CType(e.CommandSource, WebControl)

If TypeOf wc Is LinkButton Then
Dim gvrSelected As GridViewRow
Dim lnkEdit As LinkButton = DirectCast(e.CommandSource, LinkButton)
gvrSelected = DirectCast(lnkEdit.Parent.Parent, GridViewRow)

Select Case e.CommandName
Case "Select"
Dim identifierArray() As String
identifierArray = Split(Convert.ToString(e.CommandArgument), "%")

etc........


What gets us into trouble is not what we don’t know, its what we know for sure that just ain’t so.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top