Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing Page Validators in JavaScript 1

Status
Not open for further replies.

markros

Programmer
May 21, 2007
3,150
US
Hi everybody,

I'm trying to access page validators in the ASPX using the JavaScript code bellow to initially show the required validation messages:

Code:
function showReqFld(){
    var pBlk=parent.document.getElementById('spnStat').innerHTML;
    if(!pBlk||pBlk.match("Edit")) {
       if (Page_Validators) {
        for (i=0;i<Page_Validators.length;i++) {
            if(Page_Validators[i].innerHTML.match('Required'))Page_Validators[i].style.display="block";
        }   
       } 
    }
    var Loading = parent.document.getElementById('loading');
    if (Loading)
        Loading.style.display="none";
}

I'm facing two problems:

1. If I'm using User Controls, the changing display of the validators doesn't seem to work (the validators' messages are not displayed).

2. In my server-side code I hide all the forms (set MultiView index to 0) once the information is properly saved. Therefore there are no Page Validators in this case. How can I adjust the above function to make sure it doesn't break?

Of course, I can add try/catch block, but I was thinking is there other ways of checking Page_Validators for existence?

Thanks in advance.
 
if we could take a look at the whole html code that is being produced maybe we could help.


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
I was thinking along these lines as well, but unfortunately this page uses IFrames technique, so I can only see main page in the View Source and I don't see the actual form. Do you know of a way to see the whole source for IFrames?

Also I'm going to try one more quick idea I tried to implement last night.
 
in order to see the code in the frame open the page with firefox...right click on the frame and it will give an option This Frame...roll over that and it will give you an option View Frame Source

good luck with your new idea!


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
The idea didn't work :) Ok, I was testing mostly in IE7, I'm switching some other controls to User Controls and then I'll try in FireFox and post the code here.

Thanks a lot again.
 
Do you know if it's possible to detect when the frame is completely loaded?

I have the following code:

Code:
<iframe id="myframe" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" style="text-align: center; overflow: visible; width:710px; display: none;"></iframe>
<div id="loading" class="more">Loading...</div>

    <script type="text/ecmascript">
    var loading = document.getElementById("loading");
    function go()
    {
        var SelInsert = document.getElementById("PersonType");
        if(SelInsert.selectedIndex != 0){
            loading.style.display = 'block'
            var frm=loadintoIframe('myframe', SelInsert.options[SelInsert.selectedIndex].value);
        //setTimeout("loading.style.display = 'none'", 5000);    
        //var x=document.getElementById("myframe").contentDocument;
          // This doesn't work for User Controls          
           showReqFld();  
        }            
    }

How should I adjust it to run showReqFld() only after I loaded the document?

Thanks again.
 
Here is the whole CreatePerson page code:

Code:
<%@ Page Language="C#" MasterPageFile="~/admin.master" AutoEventWireup="true" CodeFile="CreatePerson.aspx.cs"
    Theme="Profesional" Inherits="Coordinator_People_CreatePerson" Title="Create Users" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
  <div id="spnStatFrame" style="display:none;">
<table style="font-weight: bolder; vertical-align: middle; color: #FFFFFF; width: 32%; text-align: center;" cellpadding="0" cellspacing="0">
<tr>
    <td><img alt="" src="../../images/tl-LightBlue.gif" height="10" width="10" /></td>
    <td style="background-color: #5BA2EB;">&nbsp;</td>
    <td><img alt="" src="../../images/tr-LightBlue.gif" height="10" width="10" /></td>
</tr>
<tr style="background-color: #5BA2EB;">
    <td>&nbsp;</td>
    <td><span id="spnStat"></span></td>
    <td>&nbsp;</td>
</tr>
<tr>
    <td><img alt="" src="../../images/bl-LightBlue.gif" height="10" width="10" /></td>
    <td style="background-color: #5BA2EB;">&nbsp;</td>
    <td><img alt="" src="../../images/br-LightBlue.gif" height="10" width="10" /></td>
</tr>
</table>
</div>

<script type="text/javascript">
    var dv = document.getElementById("spnStatFrame");
    var Spn = document.getElementById("spnStat");
    
    function DisplayMessage(Message)
    {            
        dv.style.display = "inline";        
        Spn.innerHTML = Message;
        setTimeout("dv.style.display = 'none'", 300000);
    }
</script>

<div class="p5" style="text-align:right">
    View all People <a href="peopleV.aspx"><img alt="View People" src="../../images/id_card_view.png" title="View all People" class="peopleVNavIcon"/></a>
</div>


<div>
Create a
<select id="PersonType" name="PersonType" onchange="javascript:go();">
                        <option selected="selected" value="">--Choose a Person Type--</option>
                        <option value="InsertPerson.aspx?ptype=B">Special Friend</option>
                        <option value="InsertPerson.aspx?ptype=N">Regular Person</option>
                        <option value="InsertPerson.aspx?ptype=A">Adult Volunteer</option>
                        <option value="InsertPerson.aspx?ptype=M">Teen Volunteer</option>
                        <option value="InsertPerson.aspx?ptype=F">Teen Bnei Mitzvah</option>
                        <%--<option value="InsertBneiMitzvah.aspx">Teen Bnei Mitzvah</option>--%>
                        <option value="InsertPerson.aspx?ptype=O">Volunteer In-Training</option>
</select>
</div>
<iframe id="myframe" scrolling="no" marginwidth="0" marginheight="0" 
frameborder="0" style="text-align: center; overflow: visible; 
    width:710px; display: none;" ></iframe>
<div id="loading" class="more">Loading...</div>

    <script type="text/ecmascript">
    var loading = document.getElementById("loading");
    function go()
    {
        var SelInsert = document.getElementById("PersonType");
        if(SelInsert.selectedIndex != 0){
            loading.style.display = 'block'
            var frm =loadintoIframe('myframe', SelInsert.options[SelInsert.selectedIndex].value);
        //setTimeout("loading.style.display = 'none'", 5000);    
        //var x=document.getElementById("myframe").contentDocument;
          // This doesn't work for User Controls          
          document.frames["myframe"].onload = function() {showReqFld(); };
          
        }            
    }   
       
    </script>

</asp:Content>

And here is the code for the showReqFld()
Code:
function showReqFld(){
    var pBlk=parent.document.getElementById('spnStat').innerHTML;
    if(!pBlk||pBlk.match("Edit")) {
       // Do nothing if client validation is not active
      if (typeof Page_Validators != "undefined")         
      {
        for (i=0;i<Page_Validators.length;i++) {
            if(Page_Validators[i].innerHTML.match('Required'))Page_Validators[i].style.display="block";
        }    
      }
    }
    var Loading = parent.document.getElementById('loading');
    if (Loading)
        Loading.style.display="none";
}

However, the check for Page_Validators now always render false.

Originally showReqFld() was at the bottom of the InsertPerson page. It worked, but not fot the user controls.

The other approach I may try (suggested on another forum) is to call Page.Validate() from the server side. I'm guessing I may need to call it in PreRender method.
 
Wow! All that was missing in my user control is Display="Dynamic" for required validators. Once I added it, this particular user control starts to work properly.

I spent so much time chasing the problem and it was that simple! Unbelivable!
 
no worries...live to learn...is your problem totally solved?


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Yes, this minor particular problem is now solved.

I wish all life problems can be solved this way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top