3

私はかなり単純な GridView を持っています。これは、列のマークアップです。

 <Columns>
                <asp:TemplateField HeaderText="JD Name" SortExpression="FullName"
                    HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="180px" >
                    <ItemTemplate>
                        <asp:LinkButton CommandName="edt" CommandArgument='<%#Eval("JurisdictionID") %>' runat="server" Text='<%#Eval("FullName") %>' />
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:BoundField HeaderText="JD Abbreviation" ItemStyle-Width="200px"  DataField="JDAbbreviation" SortExpression="JDAbbreviation"
                    HeaderStyle-HorizontalAlign="Center" />

                 <asp:TemplateField 
                    HeaderStyle-HorizontalAlign="Center" >
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkStat" CommandName="inac" CommandArgument='<%#Eval("JurisdictionID") %>' 
                        runat="server" Text='<%#Utils.GetStatusString((bool) Eval("IsActive")) %>' />
                    </ItemTemplate>
                </asp:TemplateField>

            </Columns>

ただし、並べ替えのために列をクリックすると、最初に行コマンドイベントがトリガーされ、次に並べ替えイベントになります。私がしている間違いは誰に教えてもらえますか?RowCommand 引数で、SortExpression を取得します。これは私にとって本当に面白いです!

4

1 に答える 1

4

Sortですrow command。詳細については、この MSDN GridView.RowCommand イベントの記事を参照してください。

if行コマンド イベントでは、行コマンド コードをいつ実行するかを決定できるように、ステートメントを追加する必要があります。e.CommandName を使用します。

void ContactsGridView_RowCommand(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")
  {
    // Convert the row index stored in the CommandArgument
    // property to an Integer.
    int index = Convert.ToInt32(e.CommandArgument);

    // Retrieve the row that contains the button clicked 
    // by the user from the Rows collection.
    GridViewRow row = ContactsGridView.Rows[index];

    // Create a new ListItem object for the contact in the row.     
    ListItem item = new ListItem();
    item.Text = Server.HtmlDecode(row.Cells[2].Text) + " " +
    Server.HtmlDecode(row.Cells[3].Text);

    // If the contact is not already in the ListBox, add the ListItem 
    // object to the Items collection of the ListBox control. 
    if (!ContactsListBox.Items.Contains(item))
    {
      ContactsListBox.Items.Add(item);
    }
  }
}  
于 2012-05-28T11:28:49.823 に答える