Trouble getting a reference to a ModalPopupExtender using javascript

Last post 07-09-2009 10:12 AM by anthony.tristan. 3 replies.

Sort Posts:

  • Trouble getting a reference to a ModalPopupExtender using javascript

    06-15-2009, 1:12 PM
    • Member
      89 point Member
    • swolff1978
    • Member since 04-15-2009, 2:53 PM
    • Posts 77

    I am trying to use the following code to get a reference to a modalpopupextender, but when I check the value of the javascript variable 'modal' it is always null. What is the proper way to get a reference to the AJAX control using javascript?

    .vb

            Dim myStringBuilder As New StringBuilder(246)
    myStringBuilder.AppendFormat(" <script type=""text/javascript"">{0}", Environment.NewLine)
    myStringBuilder.AppendFormat(" var modal = $find('<%=modal1.ClientID%>');{0}", Environment.NewLine)
    myStringBuilder.AppendFormat("
    debugger;{0}", Environment.NewLine)
    myStringBuilder.AppendFormat("
    $find('<%=modal1.ClientID%>').add_showing({0}", Environment.NewLine)
    myStringBuilder.AppendFormat(" function(){0}", Environment.NewLine)
    myStringBuilder.AppendFormat(" {{{0}", Environment.NewLine)
    myStringBuilder.AppendFormat(" alert(""Modal popup will be showing"");{0}", Environment.NewLine)
    myStringBuilder.AppendFormat(" }}{0}", Environment.NewLine)
    myStringBuilder.AppendFormat(" );{0}", Environment.NewLine)
    myStringBuilder.AppendFormat(" </script>{0}", Environment.NewLine)
    ClientScript.RegisterStartupScript(Me.GetType(), "myJSalert", myStringBuilder.ToString())

    .aspx

            <ajaxToolkit:ModalPopupExtender ID="mpeResetConfirm" runat="server"
    TargetControlID="btnReset" PopupControlID="pnlConfirmation" BehaviorID="modal1"/>
     
  • Re: Trouble getting a reference to a ModalPopupExtender using javascript

    06-15-2009, 1:30 PM
    Answer
    • Member
      89 point Member
    • swolff1978
    • Member since 04-15-2009, 2:53 PM
    • Posts 77

    Problem solved here

    I removed the code behind and added this to the .aspx page

     

        <script type="text/javascript" language="javascript">
            function pageLoad() 
            {
                var modal = $find('modal1');
                debugger;
                $find('modal1').add_showing(
                    function()
                    {
                        alert("Modal popup will be showing");
                    }
               );  
            }
        </script>
      
  • Re: Trouble getting a reference to a ModalPopupExtender using javascript

    07-08-2009, 10:26 AM

    swolff1978, thanks for posting this information.  I had been having a hard time referencing a modalpopupextender from my javascript code (and may possibly still be).  Currently, I'm having an issue with referencing the show() method of my modalpopupextender.  Here is a quick rundown:

    Overview:  I have a custom control that contains a wizard control.  On that wizard control I am attempting to transfer the validationsummary information into a modalpopupextender rather than displaying a validationsummary on the page itself.  I have a modalpopupextender sitting inside my wizard control which I am referencing from javascript on the aspx.  I have added some attributes to three controls that pass the control into my javascript function.  Details follow...

    Error:  Object doens't support this property or method.

    Codebehind (this codes sits in the Page_PreRender method):

    ((Button)this.FindControl("wizDCS$StepNavigationTemplateContainerID$StepNextButton")).Attributes.Add("onclick", "mVsInit('ValSumDiv', " + wizDCS.WizardSteps[6].FindControl("mpeValSum_Info") + ", 'no');");
    ((Button)this.FindControl("wizDCS$StepNavigationTemplateContainerID$StepNextButton")).Attributes.Add("onmouseup", "mVsInit('ValSumDiv', '" + wizDCS.WizardSteps[6].FindControl("mpeValSum_Info") + "', 'no');");
    this.Attributes.Add("onload", "mVsInit('ValSumDiv', '" + wizDCS.WizardSteps[6].FindControl("mpeValSum_Info") + "', 'no');");

    Javascript:

        var objVSMH = new oVsModalHandler();
        
        ...
        
        function mVsInit(val4, val5, val6)
        {
            objVSMH.setInternalDiv(val4);
            objVSMH.setVsCtrl(val5);
            objVSMH.setInit(val6);
            objVSMH.GetStarted();
        }
        
        function oVsModalHandler()
        {
            var internalDiv = "";
            var internalVsCtrl = null;
            var internalInit = "";
            
            function mValSumModalDialog()
            {
                var mpe = internalVsCtrl;
                Page_ClientValidate();
                if (!Page_IsValid)
                    mpe.show();
            }
            
            this.setInternalDiv = function(val4)
            {
                internalDiv = val4;
            }
            
            this.setVsCtrl = function(val5)
            {
                internalVsCtrl = val5;
            }
            
            this.setInit = function(val6)
            {
                internalInit = val6;
            }
            
            this.GetStarted = function()
            {
                mValSumModalDialog();
            }
        }
    For the life of me, I can't figure out why I can't use the show method for this object (internalVsCtrl/mpe).  Any suggestions or ideas you might have would be greatly appreciated.  Thanks and take care.

  • Re: Trouble getting a reference to a ModalPopupExtender using javascript

    07-09-2009, 10:12 AM

    Ok, I figured it out.  Perhaps it isn't the best way to handle it, but it is working just fine so I thought I'd post it here.  Initially, I had the modalpopupextender and associated controls stored within one of the steps on the wizard.  Here are the steps I took:

    1. Since I was having a hard time referencing the control from javascript because it was embedded in the wizard step, I opted to move that stuff out of the steptemplate and after the wizard control.
    2. I got rid of all my previous validationsummary handlers and created a single validationsummary control to handle all the summary listings
    3. I added the following javascript functions:
      function ShowModalDialog(mpeId)
      {
          Page_ClientValidate();
          if (!Page_IsValid)
              $find('mpeBVS0').show();
       }
      
       function init()
       {
          $get('VSDiv0').style.display = 'none';
       }

    4. Then I added the handlers on the "next" button in the wizard to check for errors before going on (all in the Page_PreRender:
      ((Button)this.FindControl("wizDCS$StepNavigationTemplateContainerID$StepNextButton")).Attributes.Add("onclick", "ShowModalDialog('" + mpeVS0.ClientID + "');");        ((Button)this.FindControl("wizDCS$StepNavigationTemplateContainerID$StepNextButton")).Attributes.Add("onmouseup", "ShowModalDialog('" + mpeVS0.ClientID + "');");
      this.Attributes.Add("onload", "init();");

    Now, instead of having multiple validationsummary controls, I have one control handling all my errors while still having the modalpopup behavior.

Page 1 of 1 (4 items)