1

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

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

在下面的代码中,我试图获取 aRowIndex并将其传递给CommandArgument并使用 a 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 == "Select") {string ProfitcenterCode = ((GridViewRow)(((LinkBut​​ton)e.CommandSource).NamingContainer)).Cells[2].Text ; } }

4

2 回答 2

3

我尝试使用LinkButton_Click()event 而不是jason建议的RowCommand那样:

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);
            }
于 2013-03-07T14:17:58.890 回答
1

您可以使用 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  %>'>
于 2013-03-06T19:05:39.803 回答