DotNetNuke CustomValidator strange behavior - asp.net

I have a strange problem with a dotnetnuke module I'm developing.
I want to use a asp custom validator to validate some input. To keep it simple I'll check only if the field was not empty and at least a few characters long. (I know there are other standard validators that I can use).
The problem is that my code works ok locally (development), but not on production.
The only difference I know is that I use DNN 6 instead of DNN 5.
No matter what I type in on production site, it always shows me the validation error message.
These are the relevant parts of the webpage:
ASCX:
<div>
<asp:UpdatePanel ID="UpdatePanelValidationSummaryHome" ChildrenAsTriggers="False" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:ValidationSummary ID="ValidationSummary1" CssClass="validationSummary" runat="server"
EnableClientScript="False" ShowSummary="true" ShowMessageBox="false" />
<asp:CustomValidator ID="CustomValidatorActiveTab" runat="server" Display="None"
ErrorMessage="Error the field ... was not correct..." OnServerValidate="CustomValidatorActiveTab_ServerValidate"></asp:CustomValidator>
<asp:Button ID="btnZoeken" CssClass="btnZoeken" CausesValidation="true" runat="server" Text="<%$ Resources:GLOBAL, btnZoeken %>"
OnClick="btnZoeken_Click" />
Code behind
private bool ValidateTab_Ondernemingsnummer()
{
if (!String.IsNullOrEmpty(txtOndernemingsnummer.Text) && txtOndernemingsnummer.Text.Length >= 3)
{
return true;
}
return false;
}
protected void CustomValidatorActiveTab_ServerValidate(object source, ServerValidateEventArgs e)
{
int activeTab = GetActiveIndexAccordion();
switch (activeTab)
{
//Zoeken op ondernemingsnummer
case 0:
if (!ValidateTab_Ondernemingsnummer())
{
e.IsValid = false;
}
else
{
e.IsValid = true;
}
break;
}
Thanks for any help.
Any ideas?

SOLVED:
I used dotnetnuke logging to see when and why e.isValid is set to false.
My custom validator control was being called twice!!
The first time it was validated ok, the second time it was not.
My solution was to disable custom server validator control in the markup and enable it just after doing the submit (and don't forget to turn it off).
Like this:
<asp:UpdatePanel ID="UpdatePanelValidationSummaryHome" ChildrenAsTriggers="False"
UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:ValidationSummary ID="ValidationSummary1" CssClass="validationSummary" runat="server"
EnableClientScript="False" ShowSummary="true" ShowMessageBox="false" />
<asp:CustomValidator ID="CustomValidatorActiveTab" runat="server" Display="None"
EnableClientScript="false" Enabled="false" ErrorMessage="ERROR ONDERNEMINGSNUMMER"
OnServerValidate="CustomValidatorActiveTab_ServerValidate"></asp:CustomValidator>
Enabled = false is important here!
Then in the button click
protected void btnZoeken_Click(object sender, EventArgs e)
{
CustomValidatorActiveTab.Enabled = true;
CustomValidatorActiveTab.Validate();
if (Page.IsValid)
{
CustomValidatorActiveTab.Enabled = false;
I still don't know why the CustomValidatorActiveTab_ServerValidate was being called twice.
It has something to do with DNN 5 I suppose (and maybe it was fixed in DNN 6).
I hope this helped someone.

The reason the validator is called twice is due to the link button.
Generating the GridView onBubbleEvent, which also causes the validator to validate.

Related

Asp.net C# - checkbox control won't stay true after code-behind runs

Front HTML
<asp:CheckBox ID="chkSend" runat="server" Text="Send?" />
The code that triggers the code behind
<asp:DropDownList ID="ddlStatus" OnSelectedIndexChanged="ddlStatus_SelectedIndexChanged" AutoPostBack="true"
runat="server" DataValueField="Id" DataTextField="Status" />
The code that runs in the code behind
protected void ddlStatus_SelectedIndexChanged(object s, EventArgs e)
{
this.chkSend.Checked = True;
}
When get back to the front end , the checkbox isn't checked. Any Ideas?
If it helps, this particular view is using Multi-views to which I'm new to.

asp.net file upload inside formview always return false (not in updatepanel)

I am using fileupload control inside formview edittemplate
<asp:FileUpload ID="fileup_profilfoto" runat="server" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Sadece şu formatlar (.jpg, .bmp, .png, .gif)" ValidationExpression="^.*\.(jpg|JPG|png|PNG|bmp|BMP|gif|GIF)$" ControlToValidate="fileup_profilfoto" ForeColor="#00C0CC"></asp:RegularExpressionValidator>
It was working.But I added an updatepanel then it didnt work,and then I remowed update panel.But it's still return false (hasfile)
protected void frmviewProfil_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
try
{
FileUpload fileup_profilfoto = (FileUpload)frmviewProfil.FindControl("fileup_profilfoto");
if (fileup_profilfoto.HasFile)
{
//do something
}
else
{
//do something
}
}
}
always goes else scopes.
hi use triggers to achive that
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="conditional">
<Triggers>
<asp:PostBackTrigger ControlID="Button1" />
</Triggers>
<ContentTemplate>
<asp:Label ID="Label1" runat="server"></asp:Label><br /><br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
code behind
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
Label1.Text = FileUpload1.FileName;
}
}
Did you do anything to the properties of the fileupload control, for example setting the autopost back value to false? Try setting this to true if it is false.
I came across this question when I had this problem and figured I'd post what my problem and solution were as well.
Make sure the file you are trying to upload is larger than 0 bytes. I was trying to upload some blank text files for testing and each file had the FileName property set correctly, but HasFile was always false. Adding some text to the files gave it some content and the file was able to be uploaded successfully.

How to display messagebox for Custom Validator error message?

In my user control i am having the code as:
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:CustomValidator ID="cvOrgTypeGrid" runat="server" Enabled="false" Display="Dynamic"
ErrorMessage="Please add rules to get eligible members" >
</asp:CustomValidator>
In Code behind, I am having a function as:
public void Validate(bool isOrgType)
{
if (!isOrgType) return;
if (Rules.Any()) return;
cvOrgTypeGrid.Enabled = true;
cvOrgTypeGrid.IsValid = false;
}
I would like to get the error message "Please add rules to get eligible members" in messagebox.
Anybody please help out.
You can use a Validation Summary Control, check this msdn link

how to make a validation code for ASP.NET form with VB coding

im trying to build a form+attachment that needs to be send to email.
Im using a VB background code (attachementemail.aspx.vb)
and my front (b-16.aspx)
I want the page to check that the user entered a email, name, phonenumber and attachment.
what command do I put in the axp.vb
and what on the .aspx
tried just about anything.
The simplest way is to use validators eg RequiredFieldValidator for mandatory fields. You can also implement CustomValidators for custom logic.
See http://msdn.microsoft.com/en-us/e5a8xz39.aspx for the available validators
At it's basic level you could use RequiredFieldValidator and CustomValidation in your form. You can use some regex logic for email, I use this but there are many out there:
Regex(#"\w+([-+.]\w+)#\w+([-.]\w+).\w+([-.]\w+)*")
Personally I use client side javascript before it hits the server and then I re-validate the entries once it hits the server. If your using the postback events then you'll need update panels and a scriptmanager (not sure if you are aware of this already, so apologies if teaching you to suck eggs!).
Here is an example:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
Code behind (sorry this is in c#)
protected void Button1_Click(object sender, EventArgs e)
{
if (RequiredFieldValidator1.IsValid)
{
Label1.Text = "Has content";
}
else
{
Label1.Text = "Not valid";
}
}
Note that the required field validator has it's own methods to display a "hey you haven't entered content here my friend" message, but i have added that to the label instead.

No error message displayed for custom validator

I have a requirement that one of multiple fields is required. Using custom validator the even fires, false is returned, but no error message is display and the form validates.
What am I missing? I have tried with and without ValidationSummary.
Thanks!
<asp:CustomValidator ID="CustomValidator1" OnServerValidate="validatePhone" EnableClientScript="false" runat="server" ErrorMessage="Home or Cell Phone is Required" ></asp:CustomValidator>
<asp:ValidationSummary ID="ValidationSummary1" DisplayMode="BulletList" runat="server" ForeColor="Red" Font-Size="X-Small" Font-Bold="true" />
protected void validatePhone(object sender, ServerValidateEventArgs e)
{
e.IsValid = string.IsNullOrEmpty(txtCellPhone.Text) && string.IsNullOrEmpty(txtHomePhone.Text) ? false : true;
}
You have to set the ControlToValidate to some TextBox.
<asp:CustomValidator ID="CustomValidator1" OnServerValidate="validatePhone" EnabEnableClientScript="false" runat="server" ErrorMessage="Home or Cell Phone is Required" ControlToValidate="txtHomePhone"/>
Check out this article. Basically you need to wire up the client side validation.
Add the following just before the closing form tag changing the control names as needed:
<%-- This configures the validator to automatically--%>
<%-- update when either of these controls is changed --%>
<script type="text/javascript">
<!--
ValidatorHookupControlID("<%= MyControl1.ClientID %>",
document.getElementById["<%= CustomValidator1.ClientID %>"]);
ValidatorHookupControlID("<%= MyControl2.ClientID %>",
document.getElementById["<%= CustomValidator1.ClientID %>"]);
//-->
</script>
Alternatively use this control
Issue was completely my fault. On my submit button the final thing I do is a Response.Redirect. The message was coming up, but then the Thank you page was being presented. Now only doing the Response.Redirect if the customvalidator returns true.

Resources