1

UI機能GridViewいくつかの列があります。最も重要な列はPcCodestringで、各行の値を示します。

予想:そのPcCode列の行からセルの1つをクリックすると、別のセルGridViewが表示されます。ただし、asp:LinkButtonセルにaを使用しようとすると、うまくいきません。セル内のRowCommandaをクリックしてもトリガーされません。私はどこで間違ったことをしているのですか?期待される機能をどのように実現できますか?初心者を助けてくれてありがとう。 asp:LinkButtonGridView

次のコードでは、を取得し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();
        }
    }

行が選択されたら、選択した行の値を文字列として取得し、listitemと比較して、新しいGridViewを表示する必要があります。

試してみました

  • Link_Button_Click(オブジェクト送信者、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()に、代わりにイベントを使用しようとしました。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

送信者オブジェクト(イベントを呼び出すリンクボタン)を使用して、グリッドビューから親行を取得できます。例:

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 に答える