473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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
Expand|Select|Wrap|Line Numbers
  1. <script type = "text/javascript">
  2.  
  3.  
  4.    function selectRow(index)
  5.    {
  6.      //Retrieve a reference to the table that represents the GridView
  7.      var gridView = document.getElementById('<%=gvSideList.ClientID%>');
  8.  
  9.      if(gridView)
  10.      {
  11.        //If able to get a reference to the table representing the GridView...      
  12.        var doPostbackArgument='Select$'+index;
  13.              __doPostBack('<%=gvSideList.ClientID%>',doPostbackArgument);
  14.      }
  15.  
  16.     }
  17.  
This is in the child window

Expand|Select|Wrap|Line Numbers
  1. <script type = "text/javascript" >
  2.         function selecttherow(index)
  3.         {
  4.         window.opener.selectRow(index);
  5.         }
  6.  
  7.  
  8.  
  9.     </script>
  10.  

Expand|Select|Wrap|Line Numbers
  1. protected void gvresults_SelectedIndexChanged(object sender, EventArgs e)
  2.     {
  3.  
  4.  
  5.         //declare variables
  6.         GridViewRow row;
  7.         int index;
  8.         int id;
  9.  
  10.         //retrieve index, row, and id from gvresults
  11.         index = gvresults.SelectedIndex;
  12.         row = gvresults.Rows[index];
  13.         id = Convert.ToInt32(row.Cells[5].Text);
  14.         //fill a datatable from general with id's for selection
  15.         SqlCommand cmd = new SqlCommand("SELECT ID FROM General", csbg);
  16.         cmd.Parameters.AddWithValue("@id", id);
  17.         SqlDataAdapter da = new SqlDataAdapter(cmd);
  18.         DataTable dt = new DataTable();
  19.  
  20.         da.Fill(dt);
  21.         //loop through the dt and find the index of the matching id from the search window
  22.         for (int i = 0; i < dt.Rows.Count; i++)
  23.         {
  24.             if (dt.Rows[i]["ID"].Equals(id))
  25.             {
  26.                 //execute script to select the matching row in the dataentry page
  27.                 string script = "";
  28.                 script += "selecttherow(";
  29.                 script += i;
  30.                 script += ");";
  31.                 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "selecting", script, true);
  32.                 break;
  33.             }
  34.         }
  35.  
  36.     }
  37.  
Let me know if you need me to further clarify anything.
Thanks ahead of time.
Aug 16 '10 #1

✓ answered by Frinavale

Remember this method???
Expand|Select|Wrap|Line Numbers
  1. function selectRow(id){
  2.   //Retrieve a reference to the table that represents the GridView
  3.   var gridView = document.getElementById('<%=MyGridView.ClientID%>');
  4.  
  5.   if(gridView){
  6.     //If able to get a reference to the table representing the GridView...
  7.     //Looping through the rows in the GridView
  8.     //looking for one that matches the ID passed in:
  9.     var gridViewRows = gridView.getElementsByTagName("tr");
  10.     for(rowIndex = 0; rowIndex < gridViewRows.length; rowIndex++){
  11.       var row = gridViewRows[rowIndex];
  12.       var cells = row.getElementsByTagName("td");
  13.       var theIndexOfTheCellWhereTheIdIsLocated = 4;
  14.       if(cells.length>theIndexOfTheCellWhereTheIdIsLocated){
  15.         if(cells[theIndexOfTheCellWhereTheIdIsLocated]==id){
  16.           var doPostbackArgument='Select$'+rowIndex;
  17.           __doPostback('<%=MyGridView.ClientID%>',doPostbackArgument);
  18.         }
  19.       }
  20.     }
  21.  
  22.   }
  23. }
Move this method to you parent page...
In the child page, just pass the "ID" to the parent-page-method.

-Frinny

7 3633
Frinavale
9,735 Expert Mod 8TB
Instead of passing the Index, consider passing the ID of the element that was selected.

-Frinny
Aug 16 '10 #2
Gridview SelectedDataKey and SelectedValue are read only. Ive tried to use them to set the selected row but was not successful.
Aug 16 '10 #3
still trying to tackle this problem. Havent found an easy way to do this yet.
Aug 17 '10 #4
Frinavale
9,735 Expert Mod 8TB
Remember this method???
Expand|Select|Wrap|Line Numbers
  1. function selectRow(id){
  2.   //Retrieve a reference to the table that represents the GridView
  3.   var gridView = document.getElementById('<%=MyGridView.ClientID%>');
  4.  
  5.   if(gridView){
  6.     //If able to get a reference to the table representing the GridView...
  7.     //Looping through the rows in the GridView
  8.     //looking for one that matches the ID passed in:
  9.     var gridViewRows = gridView.getElementsByTagName("tr");
  10.     for(rowIndex = 0; rowIndex < gridViewRows.length; rowIndex++){
  11.       var row = gridViewRows[rowIndex];
  12.       var cells = row.getElementsByTagName("td");
  13.       var theIndexOfTheCellWhereTheIdIsLocated = 4;
  14.       if(cells.length>theIndexOfTheCellWhereTheIdIsLocated){
  15.         if(cells[theIndexOfTheCellWhereTheIdIsLocated]==id){
  16.           var doPostbackArgument='Select$'+rowIndex;
  17.           __doPostback('<%=MyGridView.ClientID%>',doPostbackArgument);
  18.         }
  19.       }
  20.     }
  21.  
  22.   }
  23. }
Move this method to you parent page...
In the child page, just pass the "ID" to the parent-page-method.

-Frinny
Aug 17 '10 #5
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.
Aug 17 '10 #6
Frinavale
9,735 Expert Mod 8TB
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.
Aug 17 '10 #7
The final function i came up with that works is
Expand|Select|Wrap|Line Numbers
  1. function selectRowCustom(id)
  2.    {
  3.        var gridview = document.getElementById('<%=gvSideList.ClientID%>');
  4.        if(gridview)
  5.        {
  6.             for(var count=0; count<gridview.rows.length; count++)
  7.             {
  8.                 var cell = gridview.rows[count].cells;
  9.                 if (cell[1].innerHTML == id)
  10.                 {
  11.                     var selected = count - 1;
  12.                     var doPostbackArgument='Select$'+selected;
  13.                     __doPostBack('<%=gvSideList.ClientID%>', doPostbackArgument);
  14.                 }
  15.             }
  16.        }
  17.    }
  18.  
ty for the help yet again frinny
Aug 19 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Vikram Bhatia | last post by:
In my web application we are able to store large data in the browser cookie keeping in mind the limit of 300 cookies per cookie file, 20 keys per cookie per domain and 4KB max size of each cookie....
1
by: Edwin Knoppert | last post by:
I post this again, no answer before. During the beta version it worked, the same code does no longer work in the new release. I have a simple gridview with ButtonField, the button deletes a...
1
by: codefragment | last post by:
Hi Theres a SortParameterName property that can be set to pass a sort expression, what about a sort direction? I need this info to do my select. I tried hacking this by wiring into...
1
by: Eric | last post by:
I have a GridView control that I want to sort on multiple columns when I click a particular column. For example, I display Last name + ", " + First name in the first column and display id in the...
0
by: Smokey Grindle | last post by:
I'm trying desperatly to add a sort order arrow to my grid view and have this code below ofr it... but when the code runs the girdview sortexpression is always = "" never the sort name... why?! i...
3
by: Navodit | last post by:
Hi I have a problem in which I am not sure if Javascript can or should be used. If anyone can throw light on this it wd be great : Basically I am working on the development of a database where...
1
by: l3d007 | last post by:
I used this example straight from msdn in a gridview that already sorts based on each column. For some reason I cannot get it to sort based on two sorting expressions.Is there some property of the...
2
by: aftabShaikh | last post by:
i have a button and a gridview when i click a button one row in added in a gridview row contains textboxes. on first textbox there is button besides when i click tht button popwindow is opened now...
1
by: r786amesh | last post by:
Hello Everybody, My question is I want to display Nested gridview footer total by using javascript. its very urgent pls help me... thnx ramesh
7
by: Vladzeem | last post by:
Hi there, I have a problem to access a checkbox inside GridView using row index using javascript. Here is the code: var c; var cellValue; var rCount = 0; var cellControl; gridViewCtl =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.