CodeEngine
CodeEngine

Reputation: 296

Gridview button onclick not firing from a Microsoft Developer Network Example

I created a simple Grid view with Multiple columns and add a button so when clicked pass the values of the row to a different webform but button on click not firing. I use the example code from the link : http://msdn.microsoft.com/en-us/library/bb907626(v=vs.100).aspx

And this is my code:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AllowSorting="True" DataSourceID="SqlDataSource1" 
    PageSize="100" AutoGenerateColumns="False">
    <Columns>
        <%--<asp:HyperLinkField HeaderText="Edit" NavigateUrl="FormReport2.aspx" 
            Text="Edit" />--%>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="AddButton" runat="server" 
                CommandName="AddToCart" 
                CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
                Text="Add to Cart" />
            </ItemTemplate> 
        </asp:TemplateField>

        <asp:BoundField DataField="fldEmployeeID" HeaderText="EmployeeID" 
            SortExpression="fldEmployeeID" />
        <asp:BoundField DataField="fldAbsentDate" HeaderText="AbsentDate" 
            SortExpression="fldAbsentDate" />
        <asp:BoundField DataField="fldAbsentCode" HeaderText="AbsentCode" 
            SortExpression="fldAbsentCode" />
        <asp:BoundField DataField="fldRuleViolationWarningType" 
            HeaderText="Rule Violation Warning Type" 
            SortExpression="fldRuleViolationWarningType" />
        <asp:BoundField DataField="fldRuleViolationIssueDate" 
            HeaderText="Rule Violation Issue Date" 
            SortExpression="fldRuleViolationIssueDate" />
        <asp:BoundField DataField="fldLOAEndDate" HeaderText="LOA End Date" 
            SortExpression="fldLOAEndDate" />                                     

    </Columns>
</asp:GridView>

code behind

 Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As  System.Web.UI.WebControls.GridViewCommandEventArgs)
    If (e.CommandName = "AddToCart") Then
        ' Retrieve the row index stored in the CommandArgument property.
        Dim index As Integer = Convert.ToInt32(e.CommandArgument)

        ' Retrieve the row that contains the button 
        ' from the Rows collection.
        Dim row As GridViewRow = GridView1.Rows(index)

        ' Add code here to add the item to the shopping cart.

    End If
End Sub

Any help would be very appreciated. And also what would be the best way to pass the values from the row to a different web form from the code behind?

Upvotes: 0

Views: 198

Answers (1)

Andrei
Andrei

Reputation: 56688

You forgot to sing up for the event. See the last line here:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AllowSorting="True" DataSourceID="SqlDataSource1" 
    PageSize="100" AutoGenerateColumns="False"
    OnRowCommand="GridView1_RowCommand">

Upvotes: 1

Related Questions