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

GridView Problem calling event handler

Status
Not open for further replies.

ljwilson

Programmer
May 1, 2008
65
US
I am really new to ASP.NET I have a gridview that I am working with and simply want to put a button on it that will fire a stored proc I have written that takes a parameter (id). The id field is bound to the datasource. Here is what I have:

Code:
<body>
    <form id="form1" runat="server">
        <uc1:checkAuthintication ID="checkAuthintication1" runat="server" />
        <table width="100%">
            <tr>
                <td align="center" colspan="3">
                    <asp:Label ID="Label1" runat="server" SkinID="title" Text="Document List"></asp:Label></td>
            </tr>
            <tr>
                <td style="width: 350px">
                    <asp:Label ID="Label3" runat="server" SkinID="Title" Text="Add New Document"></asp:Label></td>
                <td colspan="2">
                </td>
            </tr>
            <tr>
                <td style="width: 350px">
                    <asp:Label ID="Label2" runat="server" Text="File Name: " Width="85px"></asp:Label>
                    <asp:FileUpload ID="uploadFile" runat="server" Width="250px" /></td>
                <td colspan="2">
                    <asp:Label ID="Label4" runat="server" Text="File Description: " Width="140px"></asp:Label><asp:TextBox
                        ID="txtFileDescription" runat="server" MaxLength="250" Width="400px" EnableViewState="False"></asp:TextBox><asp:Button
                            ID="btnAddFile" runat="server" Text="Add File" Width="85px" /></td>
            </tr>
        </table>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="fileNewName"
            DataSourceID="SqlDataSource1"  AllowSorting="True" Width="100%">
            <Columns>
                <asp:CommandField ShowSelectButton="True" />
                <asp:BoundField DataField="fileOldName" HeaderText="File Name" SortExpression="fileOldName" />
                <asp:BoundField DataField="fileDescription" HeaderText="Description" SortExpression="fileDescription" />
                <asp:BoundField DataField="createDate" HeaderText="Upload Date" SortExpression="createDate" />
                <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" SortExpression="id" Visible="False" />
                <asp:CommandField ShowDeleteButton="True" />
            </Columns>            
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ClinicalTrialsConnectionString %>"
            SelectCommand="sp_getDocumentList" SelectCommandType="StoredProcedure" 
            DeleteCommand="sp_deleteDocument" DeleteCommandType="Text">
            <SelectParameters>
               <asp:Parameter Name="awardId_fk" Type="Int32" />
               <asp:Parameter Name="meditechId" Type="String" />
            </SelectParameters>
            <DeleteParameters>
                <asp:Parameter Name="id" Type="Int32" />
            </DeleteParameters>
        </asp:SqlDataSource>
        <asp:Label ID="lblMessage" runat="server" SkinID="error" Text=""></asp:Label><br />
        <br />
        <!--#include file ="footer.inc"-->
    </form>
</body>

That is the form. Here is the vb:
Code:
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

        ' If multiple ButtonField column fields are used, use the
        ' CommandName property to determine which button was clicked.
        If e.CommandName = "Delete" Then

            ' Convert the row index stored in the CommandArgument
            ' property to an Integer.
            Dim idx As Integer = Convert.ToInt32(e.CommandArgument)

            ' Get the last name of the selected author from the appropriate
            ' cell in the GridView control.
            Dim selectedRow As GridViewRow = GridView1.Rows(idx)
            Dim id_cell As TableCell = selectedRow.Cells(4)
            Dim my_id As String = id_cell.Text

            ' Display the selected author.
            lblMessage.Text = e.CommandName
            txtFileDescription.Text = "ID = " & my_id

        End If

    End Sub

Right now I just want it to give me an indication that this vb is getting executed. Currently I see nothing happening. When my stored proc is expecting a variable, I get an error telling me that it expects a parameter that was not supplied.

How can I just put a button on the form (in a column) that will fire off an event when pressed. Currently it seems that it can only be a SELECT, EDIT, DELETE, etc type. Any help would be greatly appreciated.

Thanks,

LJ
 
Ok. I have that working. I was missing the Handles statement:

Code:
 Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles GridView1.RowCommand

Now my only issue is getting the value of a row in the GridView. I know which column I need, I just need to get the value of that cell in the selected row.

I have tried numerous methods and none work. Here is what I have right now:
Code:
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles GridView1.RowCommand

        Dim mytest As Integer
        mytest = e.CommandArgument.ToString

        If e.CommandName = "HideDoc" Then
            
        Dim my_id = GridView1.SelectedRow.Cells(mytest).Text

        lblMessage.Text = "ID = " & my_id 'mytest 'id_cell 'my_id

        End If
    End Sub

I am getting this error message:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Any ideas?
 
The problem is that no row is selected because you are not cause a row to be selected when the button is clicked.

If you have visual studio help installed, navigate to this page:
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref16/html/E_System_Web_UI_WebControls_GridView_RowCommand.htm

They are doing exactly what you want. What they do is use the RowCreatedEvent, which assigns their ID column to the command Argument property. Then, in the RowCommand event, they access that property and it returns the id of the row that the button was clicked in.
 
Well, I am not creating a row. My guess is that the RowCreatedEvent will not fire with what I am doing. I have code to get me the current row index, I just need to get the value of a particular column and the detected row in the grid.
 
Well, I am not creating a row. My guess is that the RowCreatedEvent will not fire with what I am doing.

If you are using the gridview, you should do more reseach into the events available to you and what they actuall do. The RowCreatedEvent as well as the RowDataBound event is fired for each row that is bound to the grid.
 
Well, I will admit that I am new to ASP.NET, but your answer just seemed way to complicated for such a simple matter. I kept playing with it and found out that this works just fine.

Code:
 ' This is the row that fired off the event
 Dim rowNum As Integer = e.CommandArgument.ToString

 ' Get the value of our cell (id is in Column 4)
 Dim my_id = GridView1.Rows(rowNum).Cells(4).Text

All this is processed within the GridView1.RowCommand Event

Thanks,

LJ
 
Glad you got it. I guess I was not understanding exactly what you were trying to do. I didn't realize you already had the ID of the row in the commandargument. Where exactly did you add the value to the commandargument?
 
I didn't explicitly add that to the commandargument. Is that not a default behavior?
 
Ok, I see why I am getting confused now.
Yes, the row INDEX is in the CommandArgument not the row ID. The difference being that the row index is from of the row in the grid. So, if you have 10 rows in your grid, the first row is row 0 (the index is 0 based) and the last row would be 9.

The reason I was confused is that you initially said ID which to me, means an ID from the database, which you would need to add to the command argument.

Again, sorry to confuse you, and glad you got it.
 
Ok. I think the confusion started when I mentioned I needed the ID column's selected row value. I was getting the index of the selected row. ID is a column in my DB and also a column (bound) to the GridView.

Thanks,

LJ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top