Solved

Disabling Validators for a page postback

Posted on 2003-11-12
9
556 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 1

Accepted Solution

by:
LosBear earned 50 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

710 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