I have a CustomValidator that checks if text entered in textboxes matches certain fields in a database. This was all working great before, but I have modified my page quite a bit since then and it is no longer working. I didn't think I changed anything that would affect this, but apparently I did. All my other validators (required field validators) are working correctly, but my CustomValidator isn't responding.
So anyway, here is my code:
CustomValidator:
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtCoursePrefix" ErrorMessage="Course number is already taken."></asp:CustomValidator>
VB codebehind:
Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
'Checking for duplicate course numbers
'get values
Dim checkPrefix = txtCoursePrefix.Text
Dim checkNum = txtCourseNum.Text
'db connectivity
Dim myConn As New OleDbConnection
myConn.ConnectionString = AccessDataSource2.ConnectionString
myConn.Open()
'select records
Dim mySelect As New OleDbCommand("SELECT 1 FROM tableCourse WHERE prefix=? AND course_number=?", myConn)
mySelect.Parameters.AddWithValue("#checkPrefix", checkPrefix)
mySelect.Parameters.AddWithValue("#checkNum", checkNum)
'execute(Command)
Dim myValue = mySelect.ExecuteScalar()
'check if record exists
If myValue IsNot Nothing Then
CustomValidator1.SetFocusOnError = True
args.IsValid = False
End If
End Sub
Everything is working up until CustomValidator1.SetFocusOnError = True and args.IsValid = False. I have tested the If statement and it's working correctly, it returns true and anything else I put inside of it executes.
Things you should know when using customvalidators:
If you are validating using a ValidationGroup, don't forget to add it to your CustomValidator.
Set the ControlToValidate property.
A CustomValidator control never fires when the ControlToValidate control is empty unless you set ValidateEmptyText=true.
When using ClientValidationFunction="customClientValidationFunction" use the following signature:
function customClientValidationFunction(sender, arguments) {
arguments.IsValid = true; //validation goes here
}
You should set the property ValidateEmptyText="true" on the CustomValidator. The client and server functions will always be called in that case.
It solved the problem for me.
If the handler is getting called, and you're successfully setting the args.IsValid to false, then what that does is it sets Page.IsValid to false. But unfortunately, that doesn't stop the form from being sumbitted. What you need to do is check that Page.IsValid property in your code that handles your form submit, like in the submit button handler.
So in addition to the code you posted, which sounds like it is working correctly, make sure that you have something like this for your submit handler (C# example):
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (!Page.IsValid)
{
// by simply returning, the error message for the CustomValidator will be displayed
return;
}
// do processing for valid form here
}
Use this
OnServerValidate="CustomValidator1_ServerValidate"
like an example and it will work....
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtCoursePrefix" ErrorMessage="Course number is already taken." OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
Gaurav Agrawal
First of all, put validation group on validators and the button. If that too doesnt work, put OnClientClick='CheckValidate();' and declare the function which will call page_clientvalidate method along with the parameter.. validation group. This would surely work. If that is not working put debugger in the javascript method and debug the same
Related
What I am trying to do is create a conditional statement that will alert the user if a text box is changed after initially filling that field.
For instance if they enter their name into the text box and then at a later point enter a different name into that text box, I want it to alert the user.
Below is my code of both the text box control as well as a .textchanged event.
Text Box Control:
<div class="txtSearch">
<asp:TextBox ID="txtSearchOC" runat="server" Width="250px" Text="" AutoPostBack="True" TabIndex="13" Font-Bold="True"
ForeColor="#822402" BorderColor="#822402" AutoCompleteType="Disabled" ClientIDMode="Static" />
</div>
.textChanged Event Code:
Protected Sub txtSearchOC_TextChanged(sender As Object, e As EventArgs) Handles txtSearchOC.TextChanged
Dim searchOCText As String = txtSearchOC.Text
Dim isTheSame As Boolean = False
If searchOCText = txtSearchOC.Text Then
isTheSame = True
End If
If isTheSame = True AndAlso searchOCText <> txtSearchOC.Text Then
Call txtSearchOC_Warning()
End If
End Sub
My thought process was to store the first name in a variable called "searchOCText" and then have a boolean that would hold true if searchOCText = txtSearchOC.Text, I would then use that boolean value to test if the text in the text box had changed, but I'm not terribly sure where to go from here as I am pretty new to programming.
So far I have tested the procedure "txtSearchOC_Warning()" and on its own it works and displays the message I am wanting to display. However when I try to call it from the TexChanged procedure nothing happens and I am at a loss as to why. So I am left to believe that the problem lies within my conditional and that I need to start digging there.
Any and all help would be greatly appreciated.
It's better to use JS to perform this validation since it's more efficient and doesn't require a PostBack to trigger. Check the following simple script:
<script>
let oldVal=document.getElementById("<%=txtSearchOC.ClientID%>").value;
function checkChange(val) {
if(oldVal!="")//To check if this change is a first time change
{
alert("The input value has changed to: " + val);
}
oldVal=val;
}
</script>
And your Text Box definition will be:
<asp:TextBox ID="txtSearchOC" runat="server"
onClientClick="checkChange(this.value)" AutoPostBack="false" ...
The problem with your approach is that searchOCText will be always equal to txtSearchOC after the PostBack. Line Dim searchOCText As String = txtSearchOC.Text
If you still want to use this approach you may store searchOCText in a Session like this:
Try
If Session("searchOCText").ToString()= txtSearchOC.Text Then
isTheSame = True
End If
Catch
End Try
Session("searchOCText")=txtSearchOC.Text
Looks like your code has some small mistakes:
TextChanged raised on programmatically setting value and any typing, every stroke. Better to use the LostFocus event.
Variable isTheSame is redundant (always True) because the event raises after text changing.
Actually, your message will never be shown when changing the user contents or not, because you compare user input with the same user input (inequality searchOCText <> txtSearchOC.Text always be False).
I think it is the simplest solution.
Public Class ExtTextBox
Inherits TextBox
Private LastUserInput As String
Protected Sub ExtTextBox_LostFocus(sender As Object, e As EventArgs) Handles Me.LostFocus
Select Case True
Case LastUserInput Is Nothing
LastUserInput = Text
Case Text <> LastUserInput
If MsgBox("Apply change?", MsgBoxStyle.YesNo Or MsgBoxStyle.Question) = MsgBoxResult.Yes Then
LastUserInput = Text
Else
Text = LastUserInput
End If
End Select
End Sub
End Class
Variable LastUserInput (initially Nothing) stores user input.
The first branch of Select Case stores the first user input in LastUserInput.
The second branch updates stored input or restored text according to user decision where the user points to any other element after any edit excluding the first one. You can put any code into the second branch to process new input.
I have an issue with the following code, the markup is as follows:
<asp:GridView ID="GridView" runat="server"
AutoGenerateEditButton="True"
OnRowDataBound="GridView_RowDataBound"
OnRowEditing="GridView_RowEditing"
OnRowUpdating="GridView_RowUpdating"
CssClass="gridv">
<Columns>
<asp:TemplateField HeaderText="ASN">
<ItemTemplate>
<asp:Label ID="lblASN" runat="server" Text='<% #Eval("ASN")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtASN" runat="server" Text='<%# Bind("ASN")%>' CssClass="form-control"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
...
However when I run this code to get the new changed values from the textboxes that were successfully generated and populated, I only get the initial values not the news that user has entered, the code behind this is:
Protected Sub GridView_RowUpdating(sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView.RowUpdating
Try
Dim row As GridViewRow = GridView.Rows(e.RowIndex)
Dim ID As Integer = DirectCast(row.FindControl("txtID"), TextBox).Text
Dim sASN As String = DirectCast(row.FindControl("txtASN"), TextBox).Text
Dim sDescription As String = DirectCast(row.FindControl("txtDescription"), TextBox).Text
Dim sManufacturer As String = DirectCast(row.FindControl("ddlmanufacturer"), DropDownList).SelectedValue
GridView.EditIndex = -1
Catch
End Try
ShowEmpDetails()
End Sub
So when I click the update button I use a message box to write the variables above and the values that I get are the same ones that got initially written to the textboxes, not the text that the user has changed?
I have worked out this code from a similar example in which this works with no issues, I honestly can not figure out what I am doing wrong?
As requested Page_Load event is calling this function:
Private Sub ShowEmpDetails()
Dim query As String = "SELECT * from inventory.all_items"
Dim cmd As MySqlCommand
cmd = New MySqlCommand(query)
cmd.Connection = myConn
Dim sda As New MySqlDataAdapter
sda.SelectCommand = cmd
Dim dt As New DataTable
sda.Fill(dt)
GridView.DataSource = dt
GridView.DataBind()
End Sub
Ok... there are so many things wrong with your coding approach that I don't know where to begin. Sorry.
Lets' start again and I'll explain how to properly pass values between the DOM and your code-behind.
Firstly, you need to understand how the DOM populates and builds the HTML for the browser to know what's going on.
I would test your project in Firefox and use the Inspector tool (right-click wep page). That tool is gold and has saved my already bald-head from revealing my skull!! :-)
As you know, the GridView control binds both the "view" and "edit" portions of the control into the same code. I can see you have Eval() for the view portion of the control (or the mode of the control I should say) and you have Bind() for the Edit mode. That is good. I personally hate BoundControls, as you cannot really see what's going on under the hood.
Next, avoid using AutoPostBack like the plaque! It's just ugly.
Get familiar with AjaxControlToolKit (there are others too, but start with the Ajax), and the ASP:UpdatePanel.
So in your case something like this ...
<asp:UpdatePanel ID="upADDMAIN" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="GridView" runat="server"
AutoGenerateEditButton="True"
OnRowDataBound="GridView_RowDataBound"
OnRowEditing="GridView_RowEditing"
OnRowUpdating="GridView_RowUpdating"
CssClass="gridv">
Try and put as much functionality back into the defaults of the GridView control. So go back to your DESIGNER mode in VStudio and add all the functionality you need like EDIT, UPDATE, DELETE, etc in the design mode of the GridView. This will also make sure your SQLDataSource is updated at the same time with the right SQL for the task.
Now why are you using OnRowEditing and OnRowUpdating?
My rule-of-thumb is always to keep things to a minimum and give as much control to ASP.net as possible. This avoids re-inventing the wheel with code-behind stuff that ASP.net can handle straight out of the box.
I generally use OnDataBound(), OnRowDataBound(), and OnRowUpdating() to both read the data and pre-UPDATE the data before the Update() gets called by the controls.
ie:
protected void gvLogins_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow gvRow = (GridViewRow)e.Row;
{
if (gvRow.RowType == DataControlRowType.DataRow)
{
and
protected void gvLogins_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//apply values to the SQL parameters for UPDATE, etc
GridViewRow gvRow = (GridViewRow)gvLogins.Rows[e.RowIndex];
to do some pre-Update updates outside of the GridView for example.
I never do prerendering or preloading of data in the PageLoad(). That is just re-inventing the wheel, when by default most ASP.net controls already have connectivity and updating built in!
Oh and to get the values of controls inside a GridView... just use FindControl() but in the right place! ie: the DataBound() events etc.
DropDownList ddlAgent = (DropDownList)gvRow.FindControl("ddlAgent");
HiddenField hfAgentID = (HiddenField)gvRow.FindControl("hfAgentID"); //from overall state,as EDIT mode defaults the hfAgentID to 0!
if (ddlAgent != null && hfAgentID != null)
ddlAgent.SelectedValue = hfAgentID.Value;
Sorry I only use C#, not VB.
Good luck.
I have an ASP.NET Web Forms application.
I have a Form with various TextBoxand right now I have several asp:RequiredFieldValidator linked to them belonging to the same ValidationGroup.
Now I have to apply to some TextBox an additional validation with related error message.
In the specific I got to check whether the text inside the TextBox is a Guid or not. Morevoer this check has to be done on the fly, meaning that as soon as the user moves the cursor from the TextBox the validation has to be performed, without the need to press submit.
How can I call the IsGuid(string guid) function from Javascript?
How can I attach two different error messages as validation (for instance I want to be displayed a message if TextBox.Text has carachters not allowed and lenght < N)
Is it easier to implement this validation with jQuery or with the ASP.NET validators?
If somebody else has any other idea for its implementation, please feel free to propose. Thanks!
You can use ReqularExpressionValidator control.
Here a regex
^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$
You can apply as many Validators as you need to the same control, in this case your TextBox.
In this scenario, a Custom Validator is the way to go because it enables you to do the validation with any function you develop to cover your needs. Please, have a look at this simple tutorial.
UPDATE 1: Server Side Validation
This is how it calls the server side function in the declaration of the CustomValidator:
<asp:CustomValidator runat="server" id="custPrimeCheck"
ControlToValidate="txtPrimeNumber"
OnServerValidate="PrimeNumberCheck"
ErrorMessage="Invalid Prime Number" />
Example of the "PrimeNumberCheck" VB function:
Sub PrimeNumberCheck(sender as Object, args as ServerValidateEventArgs)
Dim iPrime as Integer = Cint(args.Value), iLoop as Integer, _
iSqrt as Integer = CInt(Math.Sqrt(iPrime))
For iLoop = 2 to iSqrt
If iPrime mod iLoop = 0 then
args.IsValid = False
Exit Sub
End If
Next
args.IsValid = True
End Sub
UPDATE 2: Client Side Validation
This is how it calls the server side function in the declaration of the CustomValidator:
<asp:CustomValidator runat="server" id="custPrimeCheck"
ControlToValidate="txtPrimeNumber"
ClientValidationFunction="CheckPrime"
ErrorMessage="Invalid Prime Number" />
Example of the "CheckPrime" JavaScript function:
function CheckPrime(sender, args)
{
var iPrime = parseInt(args.Value);
var iSqrt = parseInt(Math.sqrt(iPrime));
for (var iLoop=2; iLoop<=iSqrt; iLoop++)
if (iPrime % iLoop == 0)
{
args.IsValid = false;
return;
}
args.IsValid = true;
}
Thanks to #AdrianIftode for making me aware of this.
I have a custom validator:
<asp:CustomValidator ID="QuestionValidator" runat="server" ErrorMessage="Please select an option" ClientValidationFunction="QuestionValidator_ServerValidate" OnServerValidate="QuestionValidator_ServerValidate" ValidateEmptyText="true"></asp:CustomValidator>
I have a list of questions provided by a datalist, which I need to ensure a user selects an answer to each question. I then have my validation functions:
Protected Sub QuestionValidator_ServerValidate(ByVal source As Object, ByVal args As ServerValidateEventArgs)
Dim SelectedItem As Boolean = False
For Each c As Control In Page.Master.FindControl("form1").Controls
If TypeOf c Is RadioButton Then
Dim rb As RadioButton = DirectCast(c, RadioButton)
If rb.GroupName = "AnswerOptions" AndAlso rb.Checked = True Then
SelectedItem = True
End If
End If
Next
args.IsValid = SelectedItem
End Sub
<script type="text/javascript" language="javascript">
function QuestionValidator_ServerValidate() {
return true;
}
</script>
When I run the page, there is no validation and no error message. Please can you point out where I am going wrong, I'm suspicious it is at Page.Master.FindControl("form1").Controls.
I have previously done such validation by looping through controls via form1.controls but this is unavailable as the page uses a form passed down via the master page.
There could be a flaw in your code. Because as per the logic if out of 50 radio buttons on your page even if just one is selected your validation will pass. Also the groupname for all the RBs is same for each item. Not sure if ASP.NET renames those and if not then all RBs with be grouped into one for all the questions.
For the looping part you can loop through DataList.Items collection instead of looping through all controls on the form:
Dim item As DataListItem
For Each item In DataList1.Items
Dim ctrl As Control
For Each ctrl In item.Controls
'do your rb state check here
Next ctrl
Next item
I would try looping through either "Page.NamingContainer" or "Page.Controls" and see how that goes.
Maybe I missed something, but shouldn't You also set property ControlToValidate="ID_of_the_control_to_validate" in the custom validator?
I think you need to give ValidationGroup for the CustomValidator and all the Controls Involved in the Validaiton process
I'm sure this question has been asked a million times, however I haven't been able to find an answer that solves my problem.
I am programmatically adding some custom user controls to a PlaceHolder which I have on a simple aspx page. All of the user controls Postback's work correctly except for one which has a Gridview on it.
For some reason any postback that gets fired from within this control, does not call the specified event on the first click, however all future clicks it will work fine. I have no idea why this is the case, but many solutions I have found, suggest adding an ID to the ascx User Control, however this doesn't work in my case.
I've taken a look at the source file for the page that gets generated before and after the first click, javascript used for calling the postback changes, i.e
Before first click: onclick="javascript:__doPostBack('tmpControlID$sgvPrimaryEmploymentHistory','Select$0')"
After first click: onclick="javascript:__doPostBack('OFFHome1$tmpControlID$sgvPrimaryEmploymentHistory','Select$0')"
OFFHome1 is the parent user control which exists on the aspx page. All other controls are added to a placeholder in this control, i.e.
<%# Control Language="vb" AutoEventWireup="false" CodeBehind="OFFHome.ascx.vb" Inherits="UmbracoUserControls.OFFHome" %>
<asp:PlaceHolder ID="phOFFSection" runat="server"></asp:PlaceHolder>
Nothing to complicated. Then in the code behind the controls are loaded into the placeholder using the following:
Private Sub LoadNextOFFStep()
Dim ControlName As String = "TestControl.ascx"
phOFFSection.Controls.Clear()
If ControlName IsNot Nothing AndAlso ControlName <> String.Empty Then
Dim NewControl As Object = LoadControl(ControlName)
With NewControl
.ID = USERCONTROLNAME
Dim StepCompleted As Boolean = .FillControl()
If StepCompleted Then
Exit Sub
End If
Dim AllowSkip As Boolean = .AllowSkip()
btnSkip.Visible = AllowSkip
End With
phOFFSection.Controls.Add(NewControl)
End If
End Sub
Again, nothing overly complicated. The USERCONTROLNAME is just a const with the value "tmpControlID" in it.
The control that is giving me trouble is a little complicated, I was originally using a custom GridView control that we have created, but have removed it and replaced it with the standard asp one to see if the problem still occurs, and it does.
Any button, on control which fires off a postback will fail the first time, and all future click will work correctly. On the first click the Page_Load event will get called, but that is it.
What am I doing wrong??
After far too much time spent on this, I have finally worked it out.
It was to do with the order of events, however just not where I had thought. The FillControl function was getting called before User Control had been added to the PlaceHolder. I changed this so that it gets called after the User Control was added to the PlaceHolder and now it works first time.
Basically the code looks like this now:
Private Sub LoadNextOFFStep()
Dim ControlName As String = "TestControl.ascx"
phOFFSection.Controls.Clear()
If ControlName IsNot Nothing AndAlso ControlName <> String.Empty Then
Dim NewControl As Object = LoadControl(ControlName)
With NewControl
.ID = USERCONTROLNAME
Dim AllowSkip As Boolean = .AllowSkip()
btnSkip.Visible = AllowSkip
End With
phOFFSection.Controls.Add(NewControl)
Dim StepCompleted As Boolean = CType(phOFFSection.Controls(0), Object).FillControl()
If StepCompleted Then
LoadNextOFFStep()
Exit Sub
End If
End If
End Sub
Thanks for everyone's help.
Well... the answer to the first question is simple: It fails the first time because the ID of the control (or rather, in the postback script) is different on subsequent page loads. It works on subsequent clicks because the control ID stays the same.
Now as to WHY that is... much tougher! But probably something to do with the order of operations here.
Try explicitly setting the NamingContainer value for NewControl:
With NewControl
.NamingContainer = OffHomeOne; // whatever
.ID = USERCONTROLNAME