Geeks With Blogs

@azamsharp
  • azamsharp The WWDC app says that there are new videos but there is nothing under videos!!! about 533 days ago
  • azamsharp Can I post my opinion on iOS 7 Beta or is it under NDA or something? about 533 days ago
  • azamsharp iOS 7 BETA installed successfully! about 533 days ago
  • azamsharp iOS 7 BETA installed! Now restoring! I am on NET10 which uses AT&T network. Hopefully 3G will still work! Lets c.. about 533 days ago
  • azamsharp The missing of button borders in Xcode 5 makes it hard to determine the clickable area. about 533 days ago
  • azamsharp I might wait for iOS to be released before I can put it on my device! Don't want a bricked or unstable device! about 533 days ago
  • azamsharp Android has many features that no one uses, number of versions that no one upgrades to and thousands of apps that no one pays for. about 533 days ago
  • azamsharp Maybe it is just me but the new iOS 7 UI looks like Windows Phone 8 UI! #maybeIamCrazy about 534 days ago
  • azamsharp Anyone using H20 network upgraded to iOS 7 BETA Keep me updated if you face any problems. about 534 days ago
  • azamsharp @merowing_ You already downloaded it! I cannot even load the developer's website! :( about 534 days ago

AzamSharp Some day I will know everything. I hope that day never comes.

You can also sort DataBound columns of the GridView. The one thing that you need to do is to make the header columns as the links and write the client side scripts which will handle the client postbacks. Check out the code below.

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
if (e.Row.RowType == DataControlRowType.Header)
        {
            TableCellCollection cells = e.Row.Cells;

            
foreach (TableCell cell in cells)
            {
                cell.Text = Server.HtmlDecode(GenerateScript
                (cell.Text,GridView1));
            }
        }
    }

 private string GenerateScript(string columnName,GridView gv)
    {
        
string optionalParam = "Sort$" + columnName;

        StringBuilder sb = 
new StringBuilder();
        sb.Append("<a href=\"");
        sb.Append("javascript:");
        sb.Append(ClientScript.GetPostBackEventReference
        (gv, optionalParam,
false));
        sb.Append("\">");
        sb.Append(columnName);
        sb.Append("</a>");

        
return sb.ToString();         

    }
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {

// Handle this event like I did in the previous post.

    }
    
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {

        
if (e.Row.RowType == DataControlRowType.Header)
        {
            TableCellCollection cells = e.Row.Cells;

            
foreach (TableCell cell in cells)
            {
                GenerateScript(cell.Text, GridView1);
            }
        }
       
    }

As, you can see in the above code that I have made use of the ClientScript.GetPostBackEventReference method. You can also manually type the JavaScript __doPostBack method if you want. Something like this:

 private string GenerateScript(string id,string columnName)
    {
        StringBuilder sb = 
new StringBuilder(); 
        sb.Append("<a href=\"");
        sb.Append("javascript:__doPostBack('");
        sb.Append(id);
        sb.Append("','Sort$");
        sb.Append(columnName);
        sb.Append("')\">");
        sb.Append(columnName);
        sb.Append("</a>"); 
        
        
return sb.ToString();         
    }
Hope it helps!

 

powered by IMHO 1.3

Posted on Friday, April 28, 2006 12:25 AM | Back to top


Comments on this post: Manually Sorting DataBound GridView Columns

# re: Manually Sorting DataBound GridView Columns
Requesting Gravatar...
I keep getting the error:

"Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation."
Left by Gijs on Jul 13, 2006 12:09 AM

# re: Manually Sorting DataBound GridView Columns
Requesting Gravatar...
Btw, I like to keep enableEventValidation to True. I couldn't get it to work with ClientScriptManager.RegisterForEventValidation but that's probably because I suck ;)
Left by Gijs on Jul 13, 2006 12:10 AM

# re: Manually Sorting DataBound GridView Columns
Requesting Gravatar...
This is a new issue with .Net 2.0. See http://www.codeproject.com/useritems/DoubleClickGridviewRow.asp for some guidance.
Left by BPM on Nov 30, 2006 1:57 PM

# re: Manually Sorting DataBound GridView Columns
Requesting Gravatar...
I keep getting the error:

"Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation."

pls rply asap
Left by Gunjan on Sep 11, 2007 2:08 AM

# re: Manually Sorting DataBound GridView Columns
Requesting Gravatar...
This code works well....bt nt on columns with datetime datatype


I want to know how we can sort data with datatype column in grid view


Left by stella on Sep 13, 2007 1:54 AM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net | Join free