【问题标题】:Linkbutton click in a asp:GridView cell does not trigger OnRowCommand event在 asp 中单击链接按钮:GridView 单元格不会触发 OnRowCommand 事件
【发布时间】:2013-02-21 17:11:23
【问题描述】:

UI 功能:我有一个 GridView,但列很少。最重要的列是 PcCode,它显示每一行的 string 值。

预期:当我单击该 PcCode 列的一行中的一个单元格时,应显示另一个 GridView。但是,当我尝试将asp:LinkButton 用于单元格时,事情就不起作用了。当我单击GridView 单元格中的asp:LinkButton 时,RowCommand 不会被触发。我在哪里做错了?哪种方式可以实现预期的功能?在此先感谢您帮助新手。

在下面的代码中,我试图获取RowIndex 并将其传递给CommandArgument 并使用CommandName

.aspx 代码

<asp:GridView ID="_UIProfitcenterTotalGridView" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True" PageSize="22" ShowFooter="True" AutoGenerateColumns="False"
    DataKeyNames="ProfitcenterCode" EnableViewState="False" 
    OnRowDataBound="_UIProfitcenterTotalGridView_RowDataBound"
    OnPageIndexChanging="_UIProfitcenterTotalGridView_PageIndexChanging" 
    OnRowCommand="_UIProfitcenterTotalGridView_OnRowCommand">
    <Columns>

       <asp:TemplateField HeaderText="PcCode" InsertVisible="False" 
            ShowHeader="False" SortExpression="ProfitcenterCode" FooterText="Total" FooterStyle-HorizontalAlign="Left">
            <ItemTemplate>
               <asp:LinkButton ID="_UIPCCodeLinkButton" runat="server" Text='<%# Eval("ProfitcenterCode") %>'  
                CommandName="Select" 
                CommandArgument='<%# ((GridViewRow) Container).RowIndex  %>'>
               </asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField> 
...

aspx.cs 的代码

 protected void _UIProfitcenterTotalGridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = _UIProfitcenterTotalGridView.Rows[index];
            string ProfitcenterCode = _UIProfitcenterTotalGridView.DataKeys[_UIProfitcenterTotalGridView.SelectedIndex].Values["ProfitcenterCode"].ToString();
        }
    }

选择行后,我需要将所选行的值作为字符串并与列表项进行比较以显示新的 GridView。

试过

  • 使用 Link_Button_Click(Object sender, EventArgs e) 和以下但失败。

    protected void _UIProfitcenterTotalGridView_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "选择") {string ProfitcenterCode = ((GridViewRow)(((LinkBut​​ton)e.CommandSource).NamingContainer)).Cells[2].Text; } }

【问题讨论】:

    标签: asp.net gridview asplinkbutton rowcommand


    【解决方案1】:

    我尝试使用LinkButton_Click() 事件而不是RowCommand 作为jason 建议:

    protected void LinkButton_Click(Object sender, EventArgs e)
        {
            LinkButton button = (LinkButton)sender;
            GridViewRow row = (GridViewRow)button.NamingContainer;
            if (row != null)
            {
              string theValue = ((LinkButton)sender).CommandArgument.ToString();
              ...
              ...
              //code for the extra thing I needed to do after selecting a cell value.
            }
         }
    

    但是我仍然遇到了问题,我想通了。问题是 LinkButton 没有绑定到行,它不能在选择时传递任何值。缺少的是以下代码:

     if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    LinkButton linkButton = new LinkButton();
                    linkButton.Text = e.Row.Cells[0].Text; //value of the first column from the grid 
                    linkButton.Enabled = true;
                    linkButton.Click += new EventHandler(LinkButton_Click); //triggering the LinkButton event here. 
                    e.Row.Cells[0].Controls.Add(linkButton);
                }
    

    【讨论】:

      【解决方案2】:

      您可以使用 sender 对象(调用事件的链接按钮)并从 gridview 获取父行。示例:

      Protected Sub linkButton_click(ByVal sender As Object, ByVal e As EventArgs)
         'First cast the sender to a link button
         Dim btn As LinkButton = CType(sender, LinkButton)
         'now, if there is a command arguement, you can get that like this
         Dim id As String = btn.CommandArgument
         'to get the gridviewrow that the button is on:
          Dim row As GridViewRow = CType(btn.NamingContainer, GridViewRow)   
      
      End Sub
      

      很难准确地找到你要找的东西,所以如果我错过了什么,请告诉我,我会添加它。

      protected void linkButton_click(object sender, EventArgs e)
      {
          //First cast the sender to a link button
          LinkButton btn = (LinkButton)sender;
          //now, if there is a command arguement, you can get that like this
          string id = btn.CommandArgument;
          //to get the gridviewrow that the button is on:
          GridViewRow row = (GridViewRow)btn.NamingContainer;
      
      }
      

      并将您的链接按钮更改为:

       <asp:LinkButton OnClick="linkButton_click" ID="_UIPCCodeLinkButton"
       runat="server" Text='<%# Eval("ProfitcenterCode") %>'
      
                  CommandName="Select" 
                  CommandArgument='<%# ((GridViewRow) Container).RowIndex  %>'>
      

      【讨论】:

      • 我正在寻找如何在 LinkBut​​ton 单击时从 GridView 中选择行值。我尝试了 linkBut​​ton_Click 事件,但它无法识别,因为它只是客户端检查。我需要回发到服务器,对不起,我对VB一无所知。我只需要知道我这样做的方式是否正确。如果不是,那么哪种方式是正确的? LinkBut​​ton_Click 事件还是 OnRowCommand 事件?
      • 您可以轻松地将 c# 转换为 vb.net(或 vb.net 到 c#)。 converter.telerik.com。它看起来像你设置它的方式,你需要使用点击事件。您需要将事件处理程序添加到链接按钮。我会在 c# 中发布一个版本
      • 这是否意味着我需要同时使用 linkBut​​ton_Click() 和 OnRowCommand()?但是,使用 _Click 事件会给出编译器错误消息:CS1061:“ASP.displaycountries_aspx”不包含“_UIPCCodeLinkBut​​ton_Click”的定义,并且无法找到接受“ASP.displaycountries_aspx”类型的第一个参数的扩展方法“_UIPCCodeLinkBut​​ton_Click”(你是缺少 using 指令或程序集引用?)
      • 我的示例使用了 linkBut​​ton_Click()。您也可以选择使用它。看看我给你的代码。链接按钮有一个调用我发布的方法的 ONClick 事件。尝试使用它
      • 我试过你的 sn-p 但不幸的是 linkBut​​ton_Click() 根本没有被触发。
      猜你喜欢
      • 2012-09-12
      • 2012-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-20
      • 2016-04-25
      • 2019-06-22
      • 2018-03-15
      相关资源
      最近更新 更多