Retrieving a gridview's sort expression and direction via Javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • benwizzle
    New Member
    • May 2010
    • 72

    Retrieving a gridview's sort expression and direction via Javascript

    I was given help in a previous topic located here: http://bytes.com/topic/asp-net/answe...w-another-form

    In this topic I had gridview rows that are selectable and a search window that returned search results. We found a way for me to select a row in the parent window of the search one. My issue now is that when the parent windows gridview is sorted, the function no longer selects the correct row. I was wondering if there was any way to access the gridviews sort expression and direction, or some other way I can do this so it accounts for sorting. Relavent code is as follow:

    This is in my parent window
    Code:
    <script type = "text/javascript">
        
    
       function selectRow(index)
       {
         //Retrieve a reference to the table that represents the GridView
         var gridView = document.getElementById('<%=gvSideList.ClientID%>');
        
         if(gridView)
         {
           //If able to get a reference to the table representing the GridView...      
           var doPostbackArgument='Select$'+index;
                 __doPostBack('<%=gvSideList.ClientID%>',doPostbackArgument);
         }
        
        }
    This is in the child window

    Code:
    <script type = "text/javascript" >
            function selecttherow(index)
            {
            window.opener.selectRow(index);
            }
                  
            
           
        </script>

    Code:
    protected void gvresults_SelectedIndexChanged(object sender, EventArgs e)
        {
            
    
            //declare variables
            GridViewRow row;
            int index;
            int id;
    
            //retrieve index, row, and id from gvresults
            index = gvresults.SelectedIndex;
            row = gvresults.Rows[index];
            id = Convert.ToInt32(row.Cells[5].Text);
            //fill a datatable from general with id's for selection
            SqlCommand cmd = new SqlCommand("SELECT ID FROM General", csbg);
            cmd.Parameters.AddWithValue("@id", id);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
    
            da.Fill(dt);
            //loop through the dt and find the index of the matching id from the search window
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["ID"].Equals(id))
                {
                    //execute script to select the matching row in the dataentry page
                    string script = "";
                    script += "selecttherow(";
                    script += i;
                    script += ");";
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "selecting", script, true);
                    break;
                }
            }
           
        }
    Let me know if you need me to further clarify anything.
    Thanks ahead of time.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Instead of passing the Index, consider passing the ID of the element that was selected.

    -Frinny

    Comment

    • benwizzle
      New Member
      • May 2010
      • 72

      #3
      Gridview SelectedDataKey and SelectedValue are read only. Ive tried to use them to set the selected row but was not successful.

      Comment

      • benwizzle
        New Member
        • May 2010
        • 72

        #4
        still trying to tackle this problem. Havent found an easy way to do this yet.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Remember this method???
          Code:
          function selectRow(id){
            //Retrieve a reference to the table that represents the GridView
            var gridView = document.getElementById('<%=MyGridView.ClientID%>');
           
            if(gridView){
              //If able to get a reference to the table representing the GridView...
              //Looping through the rows in the GridView
              //looking for one that matches the ID passed in:
              var gridViewRows = gridView.getElementsByTagName("tr");
              for(rowIndex = 0; rowIndex < gridViewRows.length; rowIndex++){
                var row = gridViewRows[rowIndex];
                var cells = row.getElementsByTagName("td");
                var theIndexOfTheCellWhereTheIdIsLocated = 4;
                if(cells.length>theIndexOfTheCellWhereTheIdIsLocated){
                  if(cells[theIndexOfTheCellWhereTheIdIsLocated]==id){
                    var doPostbackArgument='Select$'+rowIndex;
                    __doPostback('<%=MyGridView.ClientID%>',doPostbackArgument);
                  }
                }
              }
           
            }
          }
          Move this method to you parent page...
          In the child page, just pass the "ID" to the parent-page-method.

          -Frinny

          Comment

          • benwizzle
            New Member
            • May 2010
            • 72

            #6
            Does this method account for the gridview being sorted? As far as i knew the HTML of a sorted gridview does not change from its original non-sorted. Its all done on the client side. I actually just broke down and used a session variable to save the sort expression and direction of the gridview but havent got around to trying your method out. You have never steered me wrong before so I declared it the best answer.

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              When you sort the data in the GridView the HTML is changed so that the table rows (<tr>s) are in the order that you sorted. The DataSource on the server that you have bound the grid to does not change....

              The JavaScript looks through the <tr>s and finds the one that has the matching "ID" in cell X...so it will select the correct row regardless of where it is in the table.

              Comment

              • benwizzle
                New Member
                • May 2010
                • 72

                #8
                The final function i came up with that works is
                Code:
                function selectRowCustom(id)
                   {
                       var gridview = document.getElementById('<%=gvSideList.ClientID%>');
                       if(gridview)
                       {
                            for(var count=0; count<gridview.rows.length; count++)
                            {
                                var cell = gridview.rows[count].cells;
                                if (cell[1].innerHTML == id)
                                {
                                    var selected = count - 1;
                                    var doPostbackArgument='Select$'+selected;
                                    __doPostBack('<%=gvSideList.ClientID%>', doPostbackArgument);
                                }
                            }
                       }
                   }
                ty for the help yet again frinny

                Comment

                Working...