I need to call a javascript function from C# code. I am getting object expected error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vamshi2804
    New Member
    • May 2010
    • 5

    #1

    I need to call a javascript function from C# code. I am getting object expected error

    Code:
    //javascript function
    <script language="javascript" type="text/javascript">  
           function popModal() {   
               $find('ModalPopupExtender').show();            
           }    
      
    </script>  
    
    //C# code
    
    protected void Page_Load(object sender, EventArgs e)
    {
       if (!Page.IsPostBack)
       {
          string script = "<SCRIPT LANGUAGE='JavaScript'> ";
          script += "popModal();";
          script += "</SCRIPT>";
      this.RegisterClientScriptBlock("ClientScript", script);
         }
     }
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Well you're getting that message because JavaScript couldn't not find an element with the ID "ModalPopupExte nder". You need to provide the ID of the the ModalPopupExten der to the method. You can do this using the ClientID of the ModalPopupExten der.

    Like this:
    Code:
    protected void Page_Load(object sender, EventArgs e)
    {
       if (!Page.IsPostBack)
       {
          string script = "<SCRIPT LANGUAGE='JavaScript'> ";
          script += "popModal(" + ModalPopupExtender1.ClientID + ");";
          script += "</SCRIPT>";
      this.RegisterClientScriptBlock("ClientScript", script);
         }
     }
    You'll have to change your JavaScript to accept the ID of the ModalPopupExten der as a parameter...and use that with the $find method:
    Code:
    //javascript function
    <script language="javascript" type="text/javascript">  
           function popModal(ModalPopupExtenderClientID) {   
               $find(ModalPopupExtenderClientID).show();            
           }    
     
    </script>

    Comment

    • vamshi2804
      New Member
      • May 2010
      • 5

      #3
      Originally posted by Frinavale
      Well you're getting that message because JavaScript couldn't not find an element with the ID "ModalPopupExte nder". You need to provide the ID of the the ModalPopupExten der to the method. You can do this using the ClientID of the ModalPopupExten der.

      Like this:
      Code:
      protected void Page_Load(object sender, EventArgs e)
      {
         if (!Page.IsPostBack)
         {
            string script = "<SCRIPT LANGUAGE='JavaScript'> ";
            script += "popModal(" + ModalPopupExtender1.ClientID + ");";
            script += "</SCRIPT>";
        this.RegisterClientScriptBlock("ClientScript", script);
           }
       }
      You'll have to change your JavaScript to accept the ID of the ModalPopupExten der as a parameter...and use that with the $find method:
      Code:
      //javascript function
      <script language="javascript" type="text/javascript">  
             function popModal(ModalPopupExtenderClientID) {   
                 $find(ModalPopupExtenderClientID).show();            
             }    
       
      </script>
      Thank you for your quick response.

      But I still get the same error.

      I tried like below also

      script += "popModal(' " + ModalPopupExten der1.ClientID + "');";

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        What exactly are you trying to do?
        The ModalPopupExten der has it's own JavaScript aspect that it uses to display itself. You shouldn't be trying to find it on the page...you should be calling it's methods to display it.

        -Frinny

        Comment

        Working...