?
Solved

Different Required Field validation for two buttons on the same page

Posted on 2004-11-18
10
Medium Priority
?
1,427 Views
Last Modified: 2008-01-09
I am making changes to an aspx page which contains a form with a number of text boxes. The text boxes are split, so on one side there is three text boxes with a "Search" button below them. On the other side there is 10 boxes with an "Add" button. The buttons above the Search button are only required when the user does a search, and the same for the add buttons. I would like to avoid writing messy server side custom validation code. I had an idea to use two forms in the aspx page, but that didn't work. Any ideas?

Any help would be much appreciated!
0
Comment
Question by:RonanL
  • 6
  • 4
10 Comments
 
LVL 9

Accepted Solution

by:
Rodney Helsens earned 375 total points
ID: 12615645
I do not have a specific example to give, but this MSDN article is, IMO, an excellent resource, I'm sure you can find the answer by reading it.

http://msdn.microsoft.com/msdnmag/issues/02/04/Valid/default.aspx
0
 
LVL 9

Expert Comment

by:Rodney Helsens
ID: 12615722
In case you didn't find it.. it seems that these javascript calls may turn on individual validators.. 'valReqTitle, valREAdd & valReqAdd'

<script language=javascript>
function TurnOffValSave()
{
    ValidatorEnable(valReqTitle, false);
    ValidatorEnable(valREAdd, false);
    ValidatorEnable(valReqAdd, false);
}
</script>

<input onServerClick="Save_Click" onclick="javascript:TurnOffValSave(); if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();"     type="submit" name="Save"
    value="Save Changes >>" id="Save" />

0
 
LVL 9

Expert Comment

by:Rodney Helsens
ID: 12615741
Found another example which may be of help to you..
activate/deactivate required fields with a javascript function like this..

ValidatorEnable(document.all("_ctl0_Requiredfieldvalidator1"), True); --> activates Requiredfieldvalidator1
ValidatorEnable(document.all("_ctl0_Requiredfieldvalidator1"), False); --> deactivates
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:RonanL
ID: 12625774
thanks - the example is VB and I am having problems implementing an equivalent C# solution...
0
 
LVL 9

Expert Comment

by:Rodney Helsens
ID: 12626239
The example above is also a pure javascript solution, you just add the TooOFfValSave() method in the aspx page, and add the onclick handlers to your buttons

There are some converters out there to help out with this..
www.instantcsharp.com - free 100 lines or less snippet converter
www.vbconversions.com
www.ellkay.com
http://www.vbaccelerator.com/home/Resources/Links/NET/CSharpToVB/article.asp

If these don't work out, post the code giving you trouble and one of us can help you convert it
0
 

Author Comment

by:RonanL
ID: 12626406
Thanks!

I actually got it converted no problem but for some reason the code wasn't being called. Essentially the solution extends the Button webcontrol by adding some javascript code to stop validators for particular fields from firing. The code built fine and the page renders, but when I view the source there is no sign of the javascript code. This code is there when I build and run the example. Anyway, I've decided to give up on the client side validation and call the required field validator's validate method in the server side onclick methods.

Thanks again for all your help......I'll give you the point now.
0
 

Author Comment

by:RonanL
ID: 12626453
Here's the C# version of the code in the example.....if anyone is interested

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace XXXXX.XXXXX.XXXXXX
{
      /// <summary>
      ///            Summary description for NoFormValButton.
      /// </summary>
      public class NoFormValButton : System.Web.UI.UserControl
      {
      protected override void Render(System.Web.UI.HtmlTextWriter output)
      {
         System.Web.HttpRequest theRequest = System.Web.HttpContext.Current.Request;
         if (NoFormValList != "")
         {
            if ((theRequest.Browser.Browser.ToString() == "IE") & (System.Convert.ToInt32(theRequest.Browser.MajorVersion) >= 4))
            {
               string[] astrList;
               string strControl;
               output.WriteLine();
               output.WriteLine("<script language=javascript>");
               output.WriteLine("<!--");
               output.WriteLine("function TurnOffVal" + base.ID + "()");
               output.WriteLine("{");
               astrList = NoFormValList.Split(',');
               for (int intLoop = 0; intLoop <= astrList.GetUpperBound(0); intLoop++)
               {
                  strControl = astrList[intLoop].ToString();
                  output.WriteLine("ValidatorEnable(" + strControl + ", false);");
               }
               output.WriteLine("}");
               output.WriteLine("//-->");
               output.WriteLine("</script>");
               output.AddAttribute("onServerClick", base.ID + "_Click");
               output.AddAttribute("onClick", "javascript:TurnOffVal" + base.ID + "(); if (typeof(Page_ClientValidate) == 'function')" + "Page_ClientValidate();");
            }
         }
         base.CausesValidation = false;
         base.Render(output);
      }

      public void DisableServerSideValidation()
      {
         Control ctrlValidator;  
         string[] astrList;
         if (NoFormValList != "")
         {
            astrList = NoFormValList.Split(',');
            for (int intLoop = 0; intLoop <= astrList.GetUpperBound(0); intLoop++)
            {
               ctrlValidator = Page.FindControl(astrList[intLoop].ToString());
               if (!(ctrlValidator == null))
               {
                  if (ctrlValidator.GetType().ToString().ToUpper() == "SYSTEM.WEB.UI.WEBCONTROLS.REGULAREXPRESSIONVALIDATOR")
                  {
                     ((RegularExpressionValidator)ctrlValidator).IsValid = true;
                  }
                  else if (ctrlValidator.GetType().ToString().ToUpper() == "SYSTEM.WEB.UI.WEBCONTROLS.REQUIREDFIELDVALIDATOR")
                  {
                     ((RequiredFieldValidator)ctrlValidator).IsValid = true;
                  }
                  else if (ctrlValidator.GetType().ToString().ToUpper() == "SYSTEM.WEB.UI.WEBCONTROLS.RANGEVALIDATOR")
                  {
                     ((RangeValidator)ctrlValidator).IsValid = true;
                  }
                  else if (ctrlValidator.GetType().ToString().ToUpper() == "SYSTEM.WEB.UI.WEBCONTROLS.COMPAREVALIDATOR")
                  {
                     ((CompareValidator)ctrlValidator).IsValid = true;
                  }
                  else if (ctrlValidator.GetType().ToString().ToUpper() == "SYSTEM.WEB.UI.WEBCONTROLS.CUSTOMVALIDATOR")
                  {
                     ((CustomValidator)ctrlValidator).IsValid = true;
                  }
                  System.Environment.Exit(0);
               }
            }
         }
      }
 
      string NoFormValList
      {
         get
         {
            Object o = ViewState["NoFormValList"];
            if (o == null)
            {
               return string.Empty;
            }
            else
            {
               return o.ToString();
            }
         }
         set
         {
            ViewState["NoFormValList"] = value;
         }
      }
      }
}
0
 
LVL 9

Expert Comment

by:Rodney Helsens
ID: 12627130
glad to help... just curious.. i know you are abandoning this approach, so disregard these questions if you have completely lost interest.. no worries...

but did you create your button syntax with the proper NoFormValList.. id's from the validators you wish to suppress, also is this button being placed in a control, or in a aspx page?


<nfvc:NoFormValButton id="Save"
 runat=Server text="Save Changes >>" 
 NoFormValList="valReqTitle,valREAdd,valReqAdd">
</nfvc:NoFormValButton>
0
 

Author Comment

by:RonanL
ID: 12628014
I created the button with the syntax you have shown on an aspx page.
0
 
LVL 9

Expert Comment

by:Rodney Helsens
ID: 12628499
Ok, just wondering because I noticed in the code that if the element
NoFormValList="valReqTitle,valREAdd,valReqAdd" was empty, the javscript code would not be inserted.

The NoFormValList should be set to the ID's of the ValidationControls you wish to suppress for this button.. i think

anyway, glad to help
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

840 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