Writing a custom validator for a dropdownlist that is using autopostback. Seems to ignore the validation altogether. Why is it ignored and is there an easy fix?
Note I did not use ControlToValidate
asp.net:
<asp:UpdatePanel ID="updatePanel" runat="server" UpdateMode="Conditional" Visible="true" RenderMode="Inline">
<ContentTemplate>
<asp:DropDownList ID="ddlCommandAssign" runat="server" AutoPostBack="true">
</asp:DropDownList>
<asp:CustomValidator id="val_command_assigned" runat="server"
ErrorMessage="* "
display="Static"
OnServerValidate="commandAssigned"
/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlCommandAssign"
EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
Behind Code:
Sub commandAssigned(ByVal source As Object, _
ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
Dim s As String
s = ddlCommandAssign.SelectedValue
'if s = "1" then
' args.IsValid = true
'else
' args.IsValid = False
'end if
args.IsValid = False
End Sub
For debugging purposes, I want it to fail every time.
It doesn't seem to be executing the behind code at all.
For debugging, I added the line response.redirect("dummy.html") ... which never gets called, which also indicates (I think) that the validator never gets called.
Remove the update panel and try to do the validation at client-side itself using javascript.
CLIENT-SIDE VALIDATION
JavaScript event definition,
function ValidateFunction(sender,args)
{
var ddlCommandAssign= document.getElementById('<%=ddlCommandAssign.ClientID %>');
if (ddlCommandAssign.options[control.selectedIndex].value=='0')
{ args.IsValid = false;//This shows the validation error message and stops execution at client side itself.}
else { args.IsValid = true;//This will return to the server side. }
}
Aspx section:
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="1">select</asp:ListItem>
<asp:ListItem Value="2">sdasda</asp:ListItem>
</asp:DropDownList>
<asp:CustomValidator ID="valCustmID" runat="server" ErrorMessage="*" ForeColor="Red"
ValidationGroup="group1" ClientValidationFunction="ValidateFunction"></asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="group1" />
NOTE: Both the custom validator and the triggering button should have same validation group.
SERVER-SIDE VALIDATION
If you really want the validation server side see the code below:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="1">select</asp:ListItem>
<asp:ListItem Value="2">sdasda</asp:ListItem>
</asp:DropDownList>
<asp:CustomValidator ID="CustomValidator1" OnServerValidate="commandAssigned" runat="server" ErrorMessage="*" ValidationGroup="group1"></asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="group1" />
</ContentTemplate>
</asp:UpdatePanel>
NOTE: Both the custom validator and the triggering button should have same validation group.
code behind event looks as below:
protected void commandAssigned(object source, ServerValidateEventArgs args)
{
if (DropDownList1.SelectedItem.Value == "1")
args.IsValid = false; //since you gave controlToValidate="DropDownList1" this will display the error message.
else
args.IsValid = true;
}
Hope this helps..