473,387 Members | 1,495 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,387 software developers and data experts.

Accessing Gridview In Another Form

I have a button that opens a window like such using a button click and a javascript function
Expand|Select|Wrap|Line Numbers
  1. function opensearchwindow()
  2.          {
  3.          window.open('Search.aspx', 'Pop_Up', 'width=600, height=500, menubar=no, toolbar=no, resizeable=no');
  4.          }
  5.  
Expand|Select|Wrap|Line Numbers
  1. protected void btnSearch_Click(object sender, EventArgs e)
  2.     {
  3.  
  4.         string openscript = "";
  5.         openscript += "opensearchwindow();";
  6.         ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "openscript", openscript, true);
  7.  
  8.  
  9.     }
  10.  
This window basically allows you to search through a database for matching records and then posts the results in a gridview. I was wondering if there was a way so that if one selects a gridviewrow in the popup window, that it will change the selected row in the parent page. Ive looked all over the net and have found no clear answers on how to do this.

Ive already got some code setup to find the correct row on the parent page but dont know how to change the selected index or get a reference to it so I can.

This is my small routine in the search popup window that doesnt work because I cant access the gridview on the parent page.
Expand|Select|Wrap|Line Numbers
  1. protected void gvresults_SelectedIndexChanged(object sender, EventArgs e)
  2.     {
  3.  
  4.        /*
  5.         GridViewRow row;
  6.         int index;
  7.         string id;
  8.  
  9.         index = gvresults.SelectedIndex;
  10.         row = gvresults.Rows[index];
  11.         id = row.Cells[5].Text;
  12.         SqlCommand cmd = new SqlCommand("SELECT ID FROM General", csbg);
  13.         cmd.Parameters.AddWithValue("@id", id);
  14.         SqlDataAdapter da = new SqlDataAdapter(cmd);
  15.         DataTable dt = new DataTable();
  16.  
  17.         GridView gv = (GridView)Page.PreviousPage.FindControl("gvSideList");
  18.  
  19.         cmd.Connection.Open();
  20.         da.Fill(dt);
  21.         cmd.Connection.Close();
  22.         for (int i = 0; i < dt.Rows.Count; i++ )
  23.         {
  24.             if (dt.Rows[i].Equals(id))
  25.             {  
  26.                 RaisePostBackEvent(gv, "Select$" + i);
  27.                 break;
  28.             }
  29.         }
  30.  
  31.         */
  32.  
I wanna select the parent gridviews row either on the close button of the popup or when a row is selected in the popup window...either or doesnt matter to me.

Thank you ahead of time.
Jun 2 '10 #1

✓ answered by Frinavale

You can specifically call the __doPostback() method :)
Just make sure to pass it the correct parameters.
Or you could use the "obsolete" ClientScriptManager.GetPostBackClientHyperlink method to have ASP.NET generate the __doPostback method for you.

Not too long ago I helped someone that wanted to select a row in a GridView when the user clicked it. To do this, in the RowDataBound event we added an "onclick" attribute for the row being bound.

This is the code used for that:
Expand|Select|Wrap|Line Numbers
  1.  protected void TableGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  2. {
  3.  
  4.  
  5.             if (e.Row.RowType == DataControlRowType.DataRow && ContainingPanel.Enabled)
  6.             {
  7.                 e.Row.Attributes.Add("onmouseover", "this.style.cursor='hand'; this.style.backgroundColor='lightblue';");
  8.                 e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
  9.                 //e.Row.Attributes["onclick"] = "__doPostBack('TableGridView','Select$" + e.Row.RowIndex.ToString()+"');";
  10.                 e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.TableGridView, "Select$" + e.Row.RowIndex);
  11.             }
  12. }
Both methods worked (the commented out __doPostback() method and the ClientScirpt method).

What you need to do is take this and put it into a JavaScript method that will be called from the child window.

This JavaScript method would look something like:
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.  
  24. }
Please be aware that I did not test the above code...it's meant as a guidelines for you to use. You don't necessarily have to pass it an "id" per-say...you could just pass it the index which would save you looking for the row that contains that id....

There's more than one way to do everything :)

-Frinny

14 3787
Frinavale
9,735 Expert Mod 8TB
Yup there's a way but it's not pretty.
You need to use the JavaScript window.opener to access the parent window (Please note that for some reason you have to give the child window a name in order to use this). If the window.opener is not null, then you can use it in the child window to call a JavaScript function in the parent window that does the row selection.

:)

-Frinny
Jun 2 '10 #2
so that would mean i would open the popup window with window.opener so I can return values to the parents? Also I was reading that because gridviews are changed into tables...its really hard to change the selected index via JS. Is this true?
Jun 2 '10 #3
Frinavale
9,735 Expert Mod 8TB
Not quite.
You would open the child window just like you are.
In the child window you would have a JavaScript method that uses the window.opener to access the parent window. You can call JavaScript methods in the parent window from the child window using the window.opener property.

So this means that you need to add a JavaScript method in the parent window that accepts the values that are required for selecting the row.

When the child window's selected row changes, you call the parent windows JavaScript method (using the window.opener)...passing it the values.

It's not really that difficult to change the selected index via JavaScript...it takes some thinking, don't get me wrong...I've done GridView row manipulation with JavaScript in the past. I guess it did take me a while to figure out but that is what being a developer is all about :)

-Frinny
Jun 2 '10 #4
well im able to access the parent window and such but still havent found anything on google that is understandable to me about changing the selected index.
Jun 2 '10 #5
Could you say access the element and then possibly add a javascript:__doPostBack with the element name and row i want to select?
Jun 2 '10 #6
Frinavale
9,735 Expert Mod 8TB
You can specifically call the __doPostback() method :)
Just make sure to pass it the correct parameters.
Or you could use the "obsolete" ClientScriptManager.GetPostBackClientHyperlink method to have ASP.NET generate the __doPostback method for you.

Not too long ago I helped someone that wanted to select a row in a GridView when the user clicked it. To do this, in the RowDataBound event we added an "onclick" attribute for the row being bound.

This is the code used for that:
Expand|Select|Wrap|Line Numbers
  1.  protected void TableGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  2. {
  3.  
  4.  
  5.             if (e.Row.RowType == DataControlRowType.DataRow && ContainingPanel.Enabled)
  6.             {
  7.                 e.Row.Attributes.Add("onmouseover", "this.style.cursor='hand'; this.style.backgroundColor='lightblue';");
  8.                 e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
  9.                 //e.Row.Attributes["onclick"] = "__doPostBack('TableGridView','Select$" + e.Row.RowIndex.ToString()+"');";
  10.                 e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.TableGridView, "Select$" + e.Row.RowIndex);
  11.             }
  12. }
Both methods worked (the commented out __doPostback() method and the ClientScirpt method).

What you need to do is take this and put it into a JavaScript method that will be called from the child window.

This JavaScript method would look something like:
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.  
  24. }
Please be aware that I did not test the above code...it's meant as a guidelines for you to use. You don't necessarily have to pass it an "id" per-say...you could just pass it the index which would save you looking for the row that contains that id....

There's more than one way to do everything :)

-Frinny
Jun 2 '10 #7
I was the one you helped with the row selection :P I will test this out.
Jun 2 '10 #8
Frinavale
9,735 Expert Mod 8TB
@benwizzle
:) hehe :)
Jun 2 '10 #9
You are a champ frinny....I just am passing an index instead of searching for a row since i have code already that finds the index of the matching row on the parent page and it works!!! Ive been working on this for days trying to figure out how to do this. Javascript isnt my cup of tea. Thanx again for the help. You just got rid of my headache.
Jun 2 '10 #10
Frinavale
9,735 Expert Mod 8TB
I'm glad I could help :)

JavaScript isn't exactly my forte either but the more I do web development the more I have to use it...so I'm getting better with it. I'm finally at a stage where I'm comfortable working with this strange language (where objects are methods and methods are objects)
Jun 2 '10 #11
Frinavale
9,735 Expert Mod 8TB
@benwizzle
Oh yeah, by the way, you should check out that link I provided to the ClientScriptManager.GetPostBackClientHyperlink method... that method is the successor to the now obsolete ClientScript.GetPostBackClientHyperlink method.

-Frinny
Jun 2 '10 #12
ive been using ScriptManager and ClientScriptManager anyways because of all the ajax update panels in my app.
I use this for all my javascript registers
Expand|Select|Wrap|Line Numbers
  1. ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "selecting", script, true);
  2.  
Otherwise none of them will work because of all the ajax crap i got going on.
Jun 2 '10 #13
Frinavale
9,735 Expert Mod 8TB
Ahh good point :)
Jun 2 '10 #14
Just in case anyone was wondering or is trying to do something similar to what I just did here is the final relevant coding.

That is right after the <body> tag of my parent page.aspx:
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.  
  18.    </script>
  19.  

Is in the <head> tag of my child page.aspx:
Expand|Select|Wrap|Line Numbers
  1. <script type = "text/javascript" >
  2.         function selecttherow(index)
  3.         {
  4.         window.opener.selectRow(index);
  5.         }
  6.     </script>
  7.  

And this is in my child page.aspx.cs:
Expand|Select|Wrap|Line Numbers
  1. protected void gvresults_SelectedIndexChanged(object sender, EventArgs e)
  2.     {
  3.         GridViewRow row;
  4.         int index;
  5.         int id;
  6.  
  7.         index = gvresults.SelectedIndex;
  8.         row = gvresults.Rows[index];
  9.         id = Convert.ToInt32(row.Cells[5].Text);
  10.         SqlCommand cmd = new SqlCommand("SELECT ID FROM General", csbg);
  11.         cmd.Parameters.AddWithValue("@id", id);
  12.         SqlDataAdapter da = new SqlDataAdapter(cmd);
  13.         DataTable dt = new DataTable();
  14.  
  15.  
  16.  
  17.         cmd.Connection.Open();
  18.         da.Fill(dt);
  19.         cmd.Connection.Close();
  20.         for (int i = 0; i < dt.Rows.Count; i++)
  21.         {
  22.             if (dt.Rows[i]["ID"].Equals(id))
  23.             {
  24.                 string script = "";
  25.                 script += "selecttherow(";
  26.                 script += i;
  27.                 script += ");";
  28.                 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "selecting", script, true);
  29.                 break;
  30.             }
  31.         }
  32.  
Jun 2 '10 #15

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

Similar topics

3
by: TonyM | last post by:
Hi all, I have an application with a few different Windows forms. I am trying to update a statusbar panel's text that is in the main form, from another form. When I set the statusbar and the...
2
by: authorking | last post by:
How to access a control in another form and how to make a form become a MDI container.
4
by: Andrew Diabo | last post by:
I have 2 forms (Form1 and Form2) in my C# project. I created the second form from the main form like so: Fom2 aForm = new Form2(); aForm.ShowDialog(); How do I access the properties of a...
1
by: tmaster | last post by:
Within a class, can I create a property that is a listview? Here's what I tried, but it doesn't seem to work: '------------ create property to give the second form access to the first form's...
0
by: Geraldine Hobley | last post by:
Hello I have a problem whereby I have a treeview control on one form called projecttree and I wish to clea the nodes from this control in another form The form that contains the treeview is...
7
by: David | last post by:
Hi, I am having trouble with a form that I have loaded trying to access a procedure on the main form. The trouble seems to be that a Global Array that is declare in a Module is producing a...
6
by: surfrat_ | last post by:
Hi, I have a project that loads a splash screen while initialization takes place. On the splash form is a label that I want to update with messages on what is happening with the initialization....
3
by: =?Utf-8?B?dmJ0cnlpbmc=?= | last post by:
Background: I have a windows form called 'frmMain'. On this form, I have a LABEL control called 'lblWorkStatus', and it's property is set to PUBLIC. On another form, I have a process that checks...
7
by: rb0135 | last post by:
I have one form that initializes a class, called Players. But, I need to access these initialized values in another form. Obiously, if I do Players player = new Players(); it creates a new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.