?
Solved

Disabling Validators for a page postback

Posted on 2003-11-12
9
Medium Priority
?
567 Views
Last Modified: 2012-05-04
I have an asp.net page (using C#) named title.aspx with a dropdownlist, validator, and linkbutton:

<script language="javascript">
// Creates a new pop-up window for users to enter new dropdownlist items in
function NewWindow(url)
{
var sHTML = "<html><frameset frameborder='no' rows='20%,80%'>" +
"<frame src='common/windowcloser.htm' name='frame1' scrolling='no' noresize>" +
"<frame src='" + url + "' name='frame2' scrolling='yes' noresize>" +
"</frameset></html>"
      
// Create a window object and write the html to it
var win = window.open("", "win", "width=700,height=400");
                        
win.document.open("text/html", "replace");
win.document.write(sHTML);
win.document.close();
}
</script>
<table>
<tr>
<td><asp:textbox id="txtDesc" runat="server" /></td>
</tr>
<tr>
<td style="WIDTH: 229px" align="right"><b>Title:</b></td>
<td align="left">
<asp:dropdownlist id="ddlTitle" runat="server" DataValueField="Title_Lit" DataTextField="Title_Lit" onclick="NewWindow('applicanttitlelist.aspx')"></asp:dropdownlist>
<asp:requiredfieldvalidator id="rfvTitle" ControlToValidate="ddlNewTitle" Display="Static"  Runat="server">*</asp:requiredfieldvalidator>
<br>
<asp:LinkButton ID="lnkAdd" Runat="server" CausesValidation="False" >Add</asp:LinkButton>
</td>
</tr>
<tr>
<td>
<asp:button id="btnSave" tabIndex="20" runat="server" Text="Save" CssClass="buttons" />
</td>
</tr>
</table>

lnkAdd opens a new window where a user can add a new record to the table that populates the ddlTitle dropdownlist.  There is a button in the new window that performs the following javascript when clicked:

top.opener.document.applicantadd.submit();
top.window.close();

I need the new window to refresh the ddlTitle dropdownlist when the new window is closed (which it does already with the submit() javascript function).  However, the problem is that when the submit() function is performed, the validators on the title.aspx page are fired.

Is there any way to cause these validators not to fire when the new window is closed?

FYI - These validators need to be working so they can be utilized by the btnSave button on the title.aspx page.
0
Comment
Question by:cwhite85
  • 5
  • 4
9 Comments
 

Author Comment

by:cwhite85
ID: 9734059
Also, I use submit because I do not want any text that has been entered into the txtDesc textbox to be erased on the postback.
0
 
LVL 1

Expert Comment

by:LosBear
ID: 9734599
how about permanently stopping them just for that page?

include this in your <% @page   header

validateRequest="false"

you might be able to call this in the ispostback function in your page_load routine, but I haven't tried it before.

good luck
bear
0
 

Author Comment

by:cwhite85
ID: 9734626
I cant permanently stop the validators from running.  I need them to check for correct values in my controls when the btnSave is clicked.

Is there any other way?
0
[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

 
LVL 1

Accepted Solution

by:
LosBear earned 100 total points
ID: 9734707
tricky,
you are probably using javascript to refresh the page when the other window closes?

(this is going to be messy)
-create a hidden field
-when the window closes, pass a value to the parent window
-have the form auto-resubmit at this point
-when it resumbits, if the hidden field contains data, then

If hdnField.Value = "something" Then
     validateRequest = "false"
End If

if this is what you want to happen, then I can post code if you want
0
 

Author Comment

by:cwhite85
ID: 9734998
Yes that will work - can you please post some example code for it?
0
 
LVL 1

Expert Comment

by:LosBear
ID: 9735481
Ok,

Add a hidden field on the parent form:


      <form name="frmMain" id="frmMain" runat="server">
            <input type="hidden" name="hdnField" id="hdnField" runat="server" />
      </form>


You can change the name of the form, but make sure you change it in the javascript below:
Next, paste this in the child window place this into the head


      <script language="javascript">
      <!--
            function CopyEmail(ThisValue) {
                  top.opener.window.frmMain.hdnField.value = 'DONOTVALIDATE';
                  self.close();
            }
      //-->
      </script>

This will fill the hidden field with the value "DONOTVALIDATE"
Next you want the parent to form to auto submit, so paste this into the body tag of the CHILD

      <body onload="parent.opener.location.reload();">

If you want to close the window AND resubmit the page, use this:

      <body onload="self.close();parent.opener.location.reload();">
      
Finally, in the parent's Page_Load routine, place this:

Sub Page_Load
      If IsPostBack Then
            If hdnField.Value = "DONOTVALIDATE" Then
                  validateRequest = "false"
            End If
      End If
End Sub
      
Ok, I think this is it :)
0
 

Author Comment

by:cwhite85
ID: 9735605
What object is validateRequest a method or property of?
0
 
LVL 1

Expert Comment

by:LosBear
ID: 9739507
I researched it, and you can only use the validateRequest in either the web.config file or as part of the @Page property
When u said that the validator would fire, are you talking about the <asp:RequiredFieldValidator id="rfvTitle"......
control?  If you are just replace validateRequest="False" to    

rfvTitle.Enabled = False

0
 

Author Comment

by:cwhite85
ID: 9749753
What I ended up doing is using the javascript:

top.opener.document.forms[0].submit();
top.window.close();

when the 'Close Window' button is pressed in the new window.

This way the opener of the new window is postbacked and the new window is closed.  

When the new window is originally opened, the original page has an onlclick event that is called when the link that opens the new window is clicked.  In that onclick event, I set  a ViewState variable (ViewState["IsEditMode"]) equal to a false value.  Then on the postback when the new window is closed, there is an if statement on the load event that checks the ViewState variable.  If the variable is false, all the validators are disabled on the page, if it is true then the validators are all set to true.  This way the validators are only set to false when the new window is opened and true when the new window is not opened.

Thanks for the effort.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question