2

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

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

4

3 回答 3

5

我看到你onrowcommand在你的 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="recentJobsGridView_RowCommand">

然后将其添加到您的 codeFile

 protected void recentJobsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Add")) 
            {
              StatusLbl.Text = "Hellooooooo";
            }
        }
于 2013-08-12T19:47:30.833 回答
1

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

于 2013-06-19T16:11:12.073 回答
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">

现在在后面的代码上,修改按钮的定义,如下所示......

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

于 2013-10-01T12:37:01.760 回答