asp.net CustomValidator fires, but ServerValidateEventArgs Value always empty - asp.net

I'm having a problem implementing a CustomValidator, I have multiple TextBoxes with a MaskedEditExtender, they all should contain a date ("dd-MM-yyyy"). To check this date I want to use the CustomValidator, but the e.Value passed to my MyValidate function is always empty, while the TextBox is not.
code:
<asp:TextBox ID="Gereed" runat="server" CssClass="date" />
<asp:CustomValidator ID="cd1" runat="server" TargetControlID="Gereed" />
<asp:MaskedEditExtender ID="md1" runat="server" TargetControlID="Gereed"
Mask="99-99-9999" ClearMaskOnLostFocus="false"/>
code behind:
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
cd1.ValidateEmptyText = True
AddHandler cd1.ServerValidate, AddressOf ValidateDate
End Sub
Protected Sub ValidateDate(ByVal sender As Object, ByVal e As ServerValidateEventArgs)
e.IsValid = MyValidate(e.Value, "dd-MM-yyyy")
End Sub
I had a ClientValidationFunction that has the same problem.
Does anyone know a solution to this? I guess I'm missing something, but I don't know what, a similar solution in another website works perfectly.

TargetControlID is not a property of CustomValidator, it should be ControlToValidate. Somehow there was no errormessage, normal Validators throw an exception if ControlToValidate was not found, but the CustomValidator does not.

Related

Textbox text changed postback

So I wanted to make my textbox in asp.net to postback every time a text it typed onto it but i cant seem to figure out how. Does any one knows? here is the code of the event handler
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFiltrarNomeUtente.TextChanged
If chkFiltroUtente.Checked And Me.cbxLocal.Text <> "" Then
Me.loadFormInfo()
End If
End Sub
and the textbox
<asp:TextBox ID="txtFiltrarNomeUtente" runat="server" AutoPostBack="true">
so any ideas on how to do it? thanks
You need to add the OnTextChanged event to TextBox txtFiltrarNomeUtente.
<asp:TextBox ID="txtFiltrarNomeUtente" runat="server" AutoPostBack="true" OnTextChanged="TextBox2_TextChanged"></asp:TextBox>

Is it possible to disable an ImageButton when processing?

Is it possible to disable an ImageButton when running a process?
I found a way to disable a Button when processing, doing this:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
Dim strProcessScript As String = "this.value='Processing...';this.disabled=true;"
btnProcess.Attributes.Add("onclick", (strProcessScript + ClientScript.GetPostBackEventReference(btnProcess, "").ToString))
End If
End Sub
But it didn't work for ImageButton.
Also, I tried this code for disabling an Imagebutton:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
Dim strProcessScript As String = "this.value='Processing...';this.disabled=true;"
btnProcess.Attributes.Add("onclick", (strProcessScript + ClientScript.GetPostBackEventReference(btnProcess, "").ToString) + ";return false;")
End If
End Sub
Neither of both ones makes ImageButton to be disabled when processing, but it works for Button.
this suits better for full postback solution, if you are using ajax, I'd recommend to consider a jquery plugin or any other javascript plugin you can search for, saying that well hereĀ“s a small work around:
you need to use the onClientClick event that's provided OOB for the image button, it's easier to manage due to it's directly tied to the onlick event over the client and there's no nee to registar a custom client event when clicking the button.
all you need to do is passing your code to the onClientClick event
<asp:ImageButton runat="server" ID="BtnSubmit"
OnClientClick="this.disabled = true; this.value = 'Processing...';"
UseSubmitBehavior="false"
OnClick="BtnSubmit_Click"
Text="Submit Me!" />
take a look to this solution

Hide control in Page_Load

I am using label control in form designing.But i want to hide that particular label control on page load.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label10.Visible = False
End Sub
What should i do? I'm new to vb.net.
Try setting the visibility property within the aspx code rather than within the load event, this may solve your issue.
<asp:Label ID="lblValidation" runat="server" BackColor="Red"
Text="Please fill in all of the date fields below to proceed" Visible="False"></asp:Label>
Hope this helps!

Addhandler, button.click not firing using VB.NET

I am experiencing a problem with buttons and AddHandler. It only works if I use AddHandler Button1.click, AddressOf... in Page_load, but if I create the button dynamically in one of the sub routines, the event doesn't fire.
For example,
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
</asp:DropDownList>
<asp:ScriptManager id="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel id="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="False">
<contenttemplate>
<asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder>
</contenttemplate>
</asp:UpdatePanel>
<asp:UpdatePanel id="UpdatePanel2" runat="server" UpdateMode="Conditional">
<contenttemplate>
<asp:Label id="Label2" runat="server" Text="Label"></asp:Label>
</contenttemplate>
</asp:UpdatePanel>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Label1.Text = Date.Now
ScriptManager1.RegisterAsyncPostBackControl(DropDownList1)
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Label2.Text = "Panel refreshed at " + Date.Now.ToString()
End Sub
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
Dim b As New Button
b.Text = "Click"
ScriptManager1.RegisterAsyncPostBackControl(b)
AddHandler b.Click, AddressOf Button1_Click
PlaceHolder1.Controls.Add(b)
UpdatePanel1.Update()
End Sub
The dropdownlist works, but the button doesn't. What am I doing wrong?
You have to regenerate your dynamically created controls on every postback (at last in Page_Load, better in Page_Init). You have to set the ID of the controls accordingly because ASP.Net needs it to identify which control caused a Postback and to handle the appropriate events.
You could save the number of created buttons in ViewState and use this to regenerate them on Page_Load. Increase the number when you add a new button. Use this number also to make the Button's ID unique(append it to the ID) to ensure that its the same on every postback.
For further informations, have a look the Page-Lifecycle and ViewState with dynamically added controls.
Edit: As Joel commented, if you only need one Button you can set it's ID statically, but you have to regenerate it on postback f.e. to handle its click-event.
Just to aid anyone who has this problem and isn't quite sure how to implement. Here's a quick example.
This example starts out by displaying a dropdownlist. When user selects something from the dropdown, another dropdownlist appears.
I typed this off the top of my head, so it MAY contain errors, but you get the idea =)
In the aspx file, add a placeholder:
And in your codebehind:
...
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
'Store control count in viewstate
If Not IsPostBack Then ViewState("ControlCounter") = 1
'Rebuild dynamic controls on every postback so when page's life cycle hits Page_Load,
'selected values in the viewstate (asp.net default behavior) can be loaded into the dropdowns
Build_Dynamic_Controls()
End Sub
Protected Sub Build_Dynamic_Controls()
'Clear placeholder
myPlaceholder.Controls.Clear()
'This is where the control counter stored in the viewstate comes into play
For i as Integer = 0 To CInt(ViewState("ControlCounter") -1
Dim ddlDynamic as New DropDownList With {
.ID = "ddlDynamicDropdown" & i,
.AutoPostBack = True
}
'This is the event that will be executed when the user changes a value on the form
'and the postback occurs
AddHandler ddlDynamic.SelectedIndexChanged, AddressOf ddlDynamic_SelectedIndexChanged
'Add control to the placeholder
myPlaceholder.Controls.Add(ddl)
'Put some values into the dropdown
ddlDynamic.Items.Add("Value1")
ddlDynamic.Items.Add("Value2")
ddlDynamic.Items.Add("Value3")
Next i
End Sub
Protected Sub ddlDynamic_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
'When a dropdown value is changed, a postback is triggered (autopostback=true)
'The event is captured here and we add another dropdown.
'First we up the control count:
ViewState("ControlCounter") = CInt(ViewState("ControlCounter")) + 1
'Now that the "total controls counter" is upped by one,
'Let's recreate the controls in the placeholder to include the new dropdown
Build_Dynamic_Controls()
End Sub
...

Why won't my ASP.NET CustomValidator validate?

I must be doing something wrong. I can't seem to execute my CustomValidator's ServerValidate method.
I've got a Visual Basic ASP.NET page with a CustomValidator...
<asp:TextBox ID="TextBox1" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="TextBox1"
ErrorMessage="Friendly message goes here."
Display="Dynamic" />
<asp:Button ID="Button1" runat="server"
Text="Submit"
CausesValidation="True" />
For this test, I've got the validation set to always fail...
Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
args.IsValid = False
End Sub
But, when the button is clicked, the CustomValidator1_ServerValidate() method never executes!
Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Page.Validate()
If Page.IsValid Then
'it executes the code here!
End If
End Sub
Not even if I explicitly validate that control...
CustomValidator1.Validate() 'does nothing?
What am I doing wrong?
Add the property:
ValidateEmptyText="True"
Are you putting the validator control submit button in the same validation group?
Firstly, You seem to be missing the OnServerValidate attribute in your markup above.
Secondly, I would check to ensure that CustomValidator1_ServerValidate has been set up as an eventhandler for the ServerValidate event for Textbox1. I have had occasions where I have changed the name of the validate method in the markup and code-behind, but the IDE has not auto updated the subscribing method name passed to the eventhandler delegate
I know it's a daft question (or might sound like it!). But have you actually entered or changed the value in the textbox? I think the validator won't trigger without the contents of the textbox changing.

Resources