Solved

Preventing databind when page is invalid

Posted on 2006-06-13
5
433 Views
Last Modified: 2008-02-01
I have a page set up as follows:

A TextBox that accepts a value, which will be used as a parameter in the ObjectDataSource
A CustomValidator on that textbox (it checks to make sure the string represents a date)
A GridView, with an associated ObjectDataSource, having a SelectParameter set to the TextBox.

My problem is that the GridView rebinds on every postback, causing a problem if the textbox is invalid, the database will throw an exception.  Ideally, I would like the page to simply display the form again, with the old GridView data, and an error message from the Validators.  I thought this would be built in, because why would anybody want to use an invalid control as the source of a parameter.

If I'm confusing you, I can post snippets of my code to help clarify.
0
Comment
Question by:marker126
  • 4
5 Comments
 
LVL 7

Expert Comment

by:kGenius
ID: 16897391
First you can read this post on 4guysfromrolla: http://aspnet.4guysfromrolla.com/demos/IsPrime.Client.aspx

You should check "if (Page.IsValid) { }" before you do you're databind...

Hope this help,
you can always post the code if you want more help.

kGenius
0
 

Author Comment

by:marker126
ID: 16897572
Maybe I should clarify my situation:

I'm using ASP.NET 2.0, and the GridView is set at design-time to use the ObjectDataSource.  I have written no code to actually control the binding of the GridView, it has all been done for me (which is one of my biggest complaints about 2.0, a great deal of the details have been hidden)

<asp:TextBox ID="txtBeginDate" runat="server" CausesValidation="True"></asp:TextBox>

<asp:CustomValidator ID="valBeginDate" runat="server"
   ControlToValidate="txtBeginDate"></asp:CustomValidator>

<asp:GridView ID="gvData" runat="server" AutoGenerateColumns="True"></asp:GridView>

<asp:ObjectDataSource ID="dsData" runat="server"
      OldValuesParameterFormatString="original_{0}"
      SelectMethod="GetData"
      TypeName="dsOrders.OrdersTableAdapter">
   <SelectParameters>
      <asp:ControlParameter ControlID="txtBeginDate" Name="BeginDate" PropertyName="Text"
            Type="DateTime" />
   </SelectParameters>
</asp:ObjectDataSource>

So when txtBeginDate/valBeginDate is invalid, I want to prevent the GridView/ObjectDataSource from using the textbox to bind data.
0
 
LVL 7

Expert Comment

by:kGenius
ID: 16901969
You could write a javascript with a function called ValidateDate(sender, args) { }

This function must return args.IsValid=false; when the date is not valid.

ie
<SCRIPT LANGUAGE="javascript" >
function ValidateDate(sender, args) {

  bValidDate= //check date here returns boolean;

  if (bValidDate) {
    args.IsValid=true;
    return;
  } else {
    args.IsValid=false;
    return;
  }
}
</SCRIPT>


Add an extra attribute to the customvalidator
<asp:CustomValidator ID="valBeginDate" runat="server" ClientValidationFunction="ValidateDate"
   ControlToValidate="txtBeginDate"></asp:CustomValidator>

That should do...
When the function rejects the arguments as invalid no postback will occur. And no databinding or what so ever that happens server-sided.

kGenius
 
 
0
 
LVL 7

Accepted Solution

by:
kGenius earned 500 total points
ID: 16902170
I did some homework for you:

create This Customvalidator: <asp:CustomValidator ID="valBeginDate" runat="server" ClientValidationFunction="ValidateDate" ControlToValidate="txtBeginDate"></asp:CustomValidator>


Add this javascript to your header: (this one checks if a date is in mm/dd/yyyy format)
This works perfect with me...


<script language="javascript">

// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
      var i;
    for (i = 0; i < s.length; i++){  
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
      var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){  
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
      // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
      for (var i = 1; i <= n; i++) {
            this[i] = 31
            if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
            if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(dtStr){
      var daysInMonth = DaysArray(12)
      var pos1=dtStr.indexOf(dtCh)
      var pos2=dtStr.indexOf(dtCh,pos1+1)
      var strMonth=dtStr.substring(0,pos1)
      var strDay=dtStr.substring(pos1+1,pos2)
      var strYear=dtStr.substring(pos2+1)
      strYr=strYear
      if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
      if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
      for (var i = 1; i <= 3; i++) {
            if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
      }
      month=parseInt(strMonth)
      day=parseInt(strDay)
      year=parseInt(strYr)
      if (pos1==-1 || pos2==-1){
            return false
      }
      if (strMonth.length<1 || month<1 || month>12){
            return false
      }
      if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
            return false
      }
      if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
            return false
      }
      if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
            return false
      }
return true
}

function ValidateDate(sender, args){
alert(args.Value);
      if (isDate(args.Value)==false){
            args.IsValid=false;
            return;
      }
    args.IsValid=true;
            return;
 }
</script>
0
 
LVL 7

Expert Comment

by:kGenius
ID: 16902190
just one last correction you should remove "alert(args.Value);" in the javascript (in ValidateDate function).
That one was for testing
kGenius
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Set cookies HttpOnly and Secure 4 112
How can I use this extension method? 8 47
What .NET website keeps me current? 9 57
Word Directory is not in the drop down list 4 25
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

11 Experts available now in Live!

Get 1:1 Help Now