Different Required Field validation for two buttons on the same page

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!
RonanLAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rodney HelsensCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rodney HelsensCommented:
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
Rodney HelsensCommented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

RonanLAuthor Commented:
thanks - the example is VB and I am having problems implementing an equivalent C# solution...
0
Rodney HelsensCommented:
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
RonanLAuthor Commented:
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
RonanLAuthor Commented:
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
Rodney HelsensCommented:
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
RonanLAuthor Commented:
I created the button with the syntax you have shown on an aspx page.
0
Rodney HelsensCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.