unable to get selecteddatakey in rowCommand

Last post 07-06-2009 4:26 PM by superguppie. 1 replies.

Sort Posts:

  • unable to get selecteddatakey in rowCommand

    07-06-2009, 5:06 AM
    • Member
      4 point Member
    • irtikaz
    • Member since 07-06-2009, 8:34 AM
    • Posts 3

    hi all

    I am using asp.net 2.0 with C#. I have a link button in a grid viewlike this

     

     

     

     

    <asp:TemplateField HeaderText="Reject">

     

    <ItemTemplate>

     

    <asp:LinkButton ID="lnbSelect" runat="server" CausesValidation="False" CommandName="Select"

    EnableTheming="false" OnClientClick="confirmReject" OnClick="lnbSelect_Click" Text="Select"></asp:LinkButton>  </ItemTemplate>

    </asp:TemplateField>

     

     

     

    but i am not able to get its selecteddatakey in rowCommand event handler

     

    protected void gvRequests_RowCommand(object sender, GridViewCommandEventArgs e)

    {

    int ReqNo;

     

    GridViewRow

    selectedRow = (GridViewRow)((LinkButton

    )e.CommandSource).NamingContainer;

    int

    index = selectedRow.RowIndex;

    ReqNo =

    Convert.ToInt32(gvRequests.SelectedDataKey["ReqNo"

    ].ToString());

     

    }

     

    im getting error "object reference not set to an instance of object" 

     

     

     

  • Re: unable to get selecteddatakey in rowCommand

    07-06-2009, 4:26 PM
    Answer
    • Contributor
      7,023 point Contributor
    • superguppie
    • Member since 05-19-2009, 7:42 AM
    • Posts 1,240
    The Button_Click doesn't select the row. (Or at least, it doesn't before the RowCommand event fires.) So the GridView will not have a SelectedDataKey.
    However, chances are that e.CommandArgument is the RowIndex of the Row the Button is in.
    If the Select works, then you could handle the SelectedIndexChanged, that fires after the Row is selected.
    Superguppie.

    Please remember to click “Mark as Answer” on the post that helps you.
    This can be beneficial to other community members reading the thread.

    When all you've got is a Hammer,
    Every Problem looks like a Nail.
    Michael Swain.
Page 1 of 1 (2 items)