Accessing Gridview In Another Form

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

    Accessing Gridview In Another Form

    I have a button that opens a window like such using a button click and a javascript function
    Code:
    function opensearchwindow()
             {
             window.open('Search.aspx', 'Pop_Up', 'width=600, height=500, menubar=no, toolbar=no, resizeable=no');
             }
    Code:
    protected void btnSearch_Click(object sender, EventArgs e)
        {
            
            string openscript = "";
            openscript += "opensearchwindow();";
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "openscript", openscript, true);
    
            
        }
    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.
    Code:
    protected void gvresults_SelectedIndexChanged(object sender, EventArgs e)
        {
           
           /*
            GridViewRow row;
            int index;
            string id;
    
            index = gvresults.SelectedIndex;
            row = gvresults.Rows[index];
            id = row.Cells[5].Text;
            SqlCommand cmd = new SqlCommand("SELECT ID FROM General", csbg);
            cmd.Parameters.AddWithValue("@id", id);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
    
            GridView gv = (GridView)Page.PreviousPage.FindControl("gvSideList");
    
            cmd.Connection.Open();
            da.Fill(dt);
            cmd.Connection.Close();
            for (int i = 0; i < dt.Rows.Count; i++ )
            {
                if (dt.Rows[i].Equals(id))
                {  
                    RaisePostBackEvent(gv, "Select$" + i);
                    break;
                }
            }
           
            */
    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.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    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

    Comment

    • benwizzle
      New Member
      • May 2010
      • 72

      #3
      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?

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        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

        Comment

        • benwizzle
          New Member
          • May 2010
          • 72

          #5
          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.

          Comment

          • benwizzle
            New Member
            • May 2010
            • 72

            #6
            Could you say access the element and then possibly add a javascript:__do PostBack with the element name and row i want to select?

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              You can specifically call the __doPostback() method :)
              Just make sure to pass it the correct parameters.
              Or you could use the "obsolete" ClientScriptMan ager.GetPostBac kClientHyperlin k 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:
              Code:
               protected void TableGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
              {
              
              
                          if (e.Row.RowType == DataControlRowType.DataRow && ContainingPanel.Enabled)
                          {
                              e.Row.Attributes.Add("onmouseover", "this.style.cursor='hand'; this.style.backgroundColor='lightblue';");
                              e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
                              //e.Row.Attributes["onclick"] = "__doPostBack('TableGridView','Select$" + e.Row.RowIndex.ToString()+"');";
                              e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.TableGridView, "Select$" + e.Row.RowIndex);
                          }
              }
              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:
              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);
                      }
                    }
                  }
              
                }
              
              }
              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

              Comment

              • benwizzle
                New Member
                • May 2010
                • 72

                #8
                I was the one you helped with the row selection :P I will test this out.

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  Originally posted by benwizzle
                  I was the one you helped with the row selection :P I will test this out.
                  :) hehe :)

                  Comment

                  • benwizzle
                    New Member
                    • May 2010
                    • 72

                    #10
                    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.

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #11
                      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)

                      Comment

                      • Frinavale
                        Recognized Expert Expert
                        • Oct 2006
                        • 9749

                        #12
                        Originally posted by benwizzle
                        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.
                        Oh yeah, by the way, you should check out that link I provided to the ClientScriptMan ager.GetPostBac kClientHyperlin k method... that method is the successor to the now obsolete ClientScript.Ge tPostBackClient Hyperlink method.

                        -Frinny

                        Comment

                        • benwizzle
                          New Member
                          • May 2010
                          • 72

                          #13
                          ive been using ScriptManager and ClientScriptMan ager anyways because of all the ajax update panels in my app.
                          I use this for all my javascript registers
                          Code:
                          ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "selecting", script, true);
                          Otherwise none of them will work because of all the ajax crap i got going on.

                          Comment

                          • Frinavale
                            Recognized Expert Expert
                            • Oct 2006
                            • 9749

                            #14
                            Ahh good point :)

                            Comment

                            • benwizzle
                              New Member
                              • May 2010
                              • 72

                              #15
                              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:
                              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);
                                   }
                                  
                                  }
                                  
                                 </script>

                              Is in the <head> tag of my child page.aspx:
                              Code:
                              <script type = "text/javascript" >
                                      function selecttherow(index)
                                      {
                                      window.opener.selectRow(index);
                                      }
                                  </script>

                              And this is in my child page.aspx.cs:
                              Code:
                              protected void gvresults_SelectedIndexChanged(object sender, EventArgs e)
                                  {
                                      GridViewRow row;
                                      int index;
                                      int id;
                              
                                      index = gvresults.SelectedIndex;
                                      row = gvresults.Rows[index];
                                      id = Convert.ToInt32(row.Cells[5].Text);
                                      SqlCommand cmd = new SqlCommand("SELECT ID FROM General", csbg);
                                      cmd.Parameters.AddWithValue("@id", id);
                                      SqlDataAdapter da = new SqlDataAdapter(cmd);
                                      DataTable dt = new DataTable();
                              
                                      
                              
                                      cmd.Connection.Open();
                                      da.Fill(dt);
                                      cmd.Connection.Close();
                                      for (int i = 0; i < dt.Rows.Count; i++)
                                      {
                                          if (dt.Rows[i]["ID"].Equals(id))
                                          {
                                              string script = "";
                                              script += "selecttherow(";
                                              script += i;
                                              script += ");";
                                              ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "selecting", script, true);
                                              break;
                                          }
                                      }
                              Last edited by Frinavale; Jun 2 '10, 08:52 PM. Reason: Re-arranged descriptions of code so that they come before the code rather than after. The code makes more sense this way.

                              Comment

                              Working...