salRad
salRad

Reputation: 320

linkbutton oncommand, onClick, RowCommand in grid view not firing

I've read tens of posts about this issue and tried them all but with no luck, I'm not sure what am I missing this is gridview code:

<asp:GridView ID="recentJobsGridView" runat="server" CellPadding="4" ForeColor="#333333"
                            GridLines="None" Height="151px" Width="541px" Visible="False" AutoGenerateColumns="False"
                            PageSize="5" AllowPaging="True" OnPageIndexChanging="recentJobsGridView_PageIndexChanging"
                            DataKeyNames="orderItemId">
                            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                            <Columns>
                                <asp:TemplateField HeaderText="Order Date" Visible="true">
                                    <ItemTemplate>
                                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("orderItemId") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Order Date">
                                    <ItemTemplate>
                                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("orderDate") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="QTY">
                                    <ItemTemplate>
                                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("QTY") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="ID">
                                    <ItemTemplate>
                                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Length">
                                    <ItemTemplate>
                                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("length") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="wall">
                                    <ItemTemplate>
                                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("wall") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Paper Composition">
                                    <ItemTemplate>
                                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("paperComposition") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:LinkButton ID="btAdd" runat="server" OnCommand="btAdd_Command"  Text="Add" 
                                        CommandArgument='<%# Container.DataItem %>' CommandName="Add"></asp:LinkButton>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                            <EditRowStyle BackColor="#999999" />
                            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                            <SortedAscendingCellStyle BackColor="#E9E7E2" />
                            <SortedAscendingHeaderStyle BackColor="#506C8C" />
                            <SortedDescendingCellStyle BackColor="#FFFDF8" />
                            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                            <SortedAscendingCellStyle BackColor="#E9E7E2" />
                            <SortedAscendingHeaderStyle BackColor="#506C8C" />
                            <SortedDescendingCellStyle BackColor="#FFFDF8" />
                            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                        </asp:GridView>

here is where I bind the grid view:

 protected void custGridView_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (custGridView.SelectedDataKey != null)
        {
            selectCustomer = (int)custGridView.SelectedDataKey.Value;
            recentJobsGridView.Visible = true;
            recentJobsGridView.DataSource = ViewDataSource(selectCustomer);
            recentJobsGridView.DataBind();
        }

    }

and this is the code of the LinkButton:

 protected void btAdd_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "Add")
        {
            StatusLbl.Text = "Hellooooooo";
        }
    }

I removed everything I just want this message to be displayed but NOTHING is responding Help please...

Upvotes: 2

Views: 16225

Answers (3)

Tadit Dash
Tadit Dash

Reputation: 305

Remove OnCommand="btAdd_Command", so LinkButton will be...

<asp:LinkButton ID="btAdd" runat="server" Text="Add" 
                CommandArgument='<%# Container.DataItem %>' 
                CommandName="Add">
</asp:LinkButton>

And add OnRowCommand="btAdd_Command" inside GridView.

<asp:GridView ID="recentJobsGridView" runat="server" CellPadding="4" 
              ForeColor="#333333" GridLines="None" Height="151px" Width="541px" 
              Visible="False" AutoGenerateColumns="False"
              PageSize="5" AllowPaging="True" 
              OnPageIndexChanging="recentJobsGridView_PageIndexChanging"
              DataKeyNames="orderItemId"
              OnRowCommand="btAdd_Command">

Now on Code Behind, modify the Button's definition like below...

protected void btAdd_Command(Object sender, GridViewCommandEventArgs e)
{
    // If multiple buttons are used in a GridView control, use the
    // CommandName property to determine which button was clicked.
    if (e.CommandName == "Add")
    {
        StatusLbl.Text = "Hellooooooo";
    }
}

Mark that you have to use GridViewCommandEventArgs, not CommandEventArgs.

Upvotes: 0

Philyphil54
Philyphil54

Reputation: 66

I see you are missing the onrowcommand in your gridview. Try this

asp:GridView ID="recentJobsGridView" runat="server" CellPadding="4" ForeColor="#333333"
                            GridLines="None" Height="151px" Width="541px" Visible="False" AutoGenerateColumns="False"
                            PageSize="5" AllowPaging="True" OnPageIndexChanging="recentJobsGridView_PageIndexChanging"
                            DataKeyNames="orderItemId"  onrowcommand="recentJobsGridView_RowCommand">

then add this to your codeFile

 protected void recentJobsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Add")) 
            {
              StatusLbl.Text = "Hellooooooo";
            }
        }

Upvotes: 5

Brian Mains
Brian Mains

Reputation: 50728

Try using the GridView's RowCommand event, which should fire on clicking the link button, with the command name/arg.

Upvotes: 1

Related Questions