?
Solved

C# Datagrid  can't get sort command and item command to both work

Posted on 2005-04-09
2
Medium Priority
?
564 Views
Last Modified: 2011-10-03

I built a datagrid "TireDisplay", and I added this cool two-way sorting function:

void TireDisplayGrid_SortCommand(Object sender, DataGridSortCommandEventArgs e)
{
       string sortDir = "";
       if (Request.QueryString["sortOn"] == e.SortExpression)
             {
           // The sort expression matches what was sorted on last time
                       if (Request.QueryString["sortDir"] == "")
                 {
                 sortDir = "ASC";
                 }
           else if (Request.QueryString["sortDir"] == "ASC")
                 {
                 sortDir = "DESC";
                 }
           else if (Request.QueryString["sortDir"] == "DESC")
                 {
                 sortDir = "ASC";
                 }
             }
       else
       {
      // Have a new sort expression so always start out ASC
      sortDir = "ASC";
       }
     
 // Redirect back to this page with the appropriate sort expression and direction.
 Response.Redirect(Request.ServerVariables["SCRIPT_NAME"] + "?sortOn=" + e.SortExpression + "&sortDir=" + sortDir);
}

Everything works great.  Next, I needed a detail link for items in the grid.  So I added an event handler for a button column:

void GetQuote_ItemCommand(Object sender, DataGridCommandEventArgs e)
{
//get values from row where button is clicked, create a session variable, and go to detail page
string SelectedPrice = e.Item.Cells[5].Text;
Session["QuotePrice"] = SelectedPrice;
Response.Redirect("PriceQuote.aspx");
}

Everthing still works.  Then I added the button column:

<asp:ButtonColumn Text="Get a Quote" ButtonType="LinkButton" Visible="true"/>

Still OK!  But the final piece is the "OnItemCommand = GetQuote_ItemCommand" attribute of the grid.  As soon as I add this, the sort command breaks.  It now tries to activate the GetQuote_ItemCommand event, and the app breaks.

It seems like it can't distinguish between DataGridCommandEventArgs  and DataGridSortCommandEventArgs

Help!!



0
Comment
Question by:patgarvey
2 Comments
 
LVL 18

Accepted Solution

by:
tusharashah earned 1200 total points
ID: 13745244
Add CommandName property in ButtonColumn
<asp:ButtonColumn CommandName="ButtonClick" Text="Get a Quote" ButtonType="LinkButton" Visible="true"/>

Now modify your CS code like following:

void GetQuote_ItemCommand(Object sender, DataGridCommandEventArgs e)
{
 if( e.CommandName == "ButtonClick" )
   {
      //get values from row where button is clicked, create a session variable, and go to detail page
      string SelectedPrice = e.Item.Cells[5].Text;
      Session["QuotePrice"] = SelectedPrice;
      Response.Redirect("PriceQuote.aspx");
    }
}


-tushar
0
 

Author Comment

by:patgarvey
ID: 13745433

That worked!!  Thanks tushar!!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question