Solved

Disabling Validators for a page postback

Posted on 2003-11-12
9
548 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now