title: Calling C# Method on ModalPopup display
body: <p>I've started quite recently developing asp.net web forms and I have this situation that I just can't figure it out on my own. </p>
  
  <p>I have a linkbutton for which, when clicked, I would like to open a modal popup window. The modal popup contains a panel with a gridview in it, that I would like to bind (fill) before the modal popup is shown based on C# method.</p>
  
  <p>Basically, what I need to do is, when the user clicks the button, </p>
  
  <p>(1) the gridview inside the modal popup gets populated </p>
  
  <p>(2) the modal popup will be displayed showing the user a fully populated gridview.</p>
  
  <p>Here are code snippets:</p>
  
  <pre><code>&lt;%-- Relationship Type--%&gt;
      &lt;tr style="background-color:#eee;"&gt;      
          &lt;td class="borderTd"&gt; 
            &lt;asp:LinkButton ID="lnkRelTypes" runat="server" Text="Relationship Type:" OnClick="btPopulateRelTypes_Click" &gt; &lt;/asp:LinkButton&gt;
         &lt;/td&gt;  
      &lt;td style=" border:0;"&gt;
              &lt;asp:DropDownList ID="ddlRelationshipType" runat="server" TabIndex="3" CssClass="field"&gt; &lt;/asp:DropDownList&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;ajaxToolkit:ModalPopupExtender ID="modalppSearchRelTypes" runat="server" TargetControlID="lnkRelTypes" 
                                          PopupControlID="panelMaintainRelTypes" BackgroundCssClass="modalBackground" OkControlID="OkButton"
                                          DropShadow="true" /&gt;
  </code></pre>
  
  <p>The problem is that when I debug it, when I press the linkbutton, the event never gets fired and the modal popup it's displayed, of course, with nothing in it because the gridview was never populated.</p>
  
  <p>I have tried creating a javascript function that would call the method (as a webmethod) assigning it OnClientClick event of the button, but by doing that, inside my static web method I can not use any of the page controls, such as the gridview that I am trying to populate.</p>
  
  <p><strong>LATER EDIT providing the JS used to call a WebMethod that would fill the gridview</strong></p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
       function PopulateRelTypes() 
       {
           PageMethods.GetResult(OnSucess, OnError);
  
           function OnSucess(result)
           { }
  
           function OnError(result) {
               alert(result);
           }
       }
  </code></pre>
  
  <p>C# code:</p>
  
  <pre><code>[WebMethod]
  public static string GetResult()
  {
      string result = GetRelTypes();
      return result;
  
  }
  </code></pre>
  
  <p>Error message: An object reference is required for the non-static field, method, or property 'GetRelTypes()'    </p>
  
  <p>This was the JS function that was assigned on the OnClientClick event of the link button, but it didn't allow me to use any of the page controls (eg: gridview, labels, etc)</p>
  
  <hr>
  
  <p><strong>LATER later EDIT :)</strong></p>
  
  <p>I think I forgot to mention something important, which is that both the linkbutton and the gridview from the modalpopup are both inside UpdatePanels. Therefore the postback of the page never gets fired when clicking the linkbutton.</p>
  
  <hr>
  
  <p>The only case where I made this to work was to create a second button, inside the modal popup, that would fill my gridview by calling the GetRelTypes() method (which was changed to a void not a string), but that's not how I want it to be...</p>
  
  <p>Any suggestions, please?</p>
  
  <hr>
  
  <p><strong>My Solution:</strong></p>
  
  <p>For the link button from the main form - I've added it an OnClientClick event to call a JS function as follows:</p>
  
  <p>LinkButton:</p>
  
  <pre><code>&lt;asp:LinkButton ID="lnkRelTypes" runat="server" Text="Relationship Type:" OnClientClick="PopulateRelTypes();" &gt; &lt;/asp:LinkButton&gt;
  </code></pre>
  
  <p>JS function:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
       function PopulateRelTypes() 
       {
           document.getElementById('&lt;%= btPopupLoad.ClientID %&gt;').click();        
       }
  &lt;/script&gt;
  </code></pre>
  
  <p>The javascript function calls a hidden button that I've placed inside the modal popup. That hidden button has a server side clickevent that populates the gridview as I need to:</p>
  
  <p>Modalpopup hidden button:</p>
  
  <pre><code>&lt;asp:Button ID="btPopupLoad" runat="server" Text="Load" 
                  CssClass="button" onclick="btPopupLoad_Click" style="display:none;"/&gt; 
  </code></pre>
  
  <p>C# method to fetch the data:</p>
  
  <pre><code>protected void btPopupLoad_Click(object sender, EventArgs e)
  {
      GetRelTypes();
  }
  </code></pre>
  
  <p>Thank god I finally found a solution to this, it was driving me crazy!!! :) I just hope this solution will help others (as me) someday! </p>