linkbutton oncommand,onClick,网格视图中的RowCommand未触发

时间:2013-06-19 15:59:02

标签: asp.net gridview command

我已经阅读了几篇关于这个问题的帖子并试了一下但没有运气,我不确定我错过了什么是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">
                            <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>

这是我绑定网格视图的地方:

 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();
        }

    }

这是LinkBut​​ton的代码:

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

我删除了所有我想要显示此消息的内容,但没有响应 请帮忙......

3 个答案:

答案 0 :(得分:5)

我发现您错过了gridview中的onrowcommand。试试这个

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">

然后将其添加到您的codeFile

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

答案 1 :(得分:1)

尝试使用GridView RowCommand事件,该事件应在单击链接按钮时触发,命令名称为/ arg。

答案 2 :(得分:0)

删除OnCommand="btAdd_Command",因此LinkButton将是......

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

OnRowCommand="btAdd_Command"内添加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">

现在在Code Behind上,修改Button的定义如下......

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";
    }
}

标记您必须使用GridViewCommandEventArgs,而不是CommandEventArgs