I have a fairly simple GridView. This is the markup for Columns:
<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>
However, when I click on of the columns for sorting, it first triggers a row command event and then comes to sorting event. Can anybody tell me what is the mistake I am doing? In the RowCommand argument I get the SortExpression. This is really funny for me!
Sort is a row command. Check out this MSDN GridView.RowCommand Event article for more details.
In your row command event you should add an if statement so you can determine when the row command code should execute. Use 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);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With