VB.net disable CompareValidator from code behind - asp.net

Is it possible to disable CompareValidator / RequiredFieldValidator from code behind in VB.net?
I have 3 field (dropdown and two dated picker) and once the month from dropdown is selected I want to disable validation / required on datepicker inputs.
Any suggestions much appreciated!
Please note have already tried this, and it does not work:
Protected Sub DashbodropardMonthsDropDown_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DashbodropardMonthsDropDown.SelectedIndexChanged
' If Month is selected from DD menu
Dim MonthDD As Integer = DashbodropardMonthsDropDown.SelectedValue
' If nothing selected or selected "--select" with value 0
If MonthDD = 0 Then
'Validate DatePicker
CompareValidatorT1.Enabled = True
RequiredFieldValidator2.Enabled = True
RequiredFieldValidator1.Enabled = True
Else
'Do Not Validate DatePicker
CompareValidatorT1.Enabled = False
RequiredFieldValidator2.Enabled = False
RequiredFieldValidator1.Enabled = False
End If
End Sub

You can enable or disable your Validators from code behind by doing something like this
CompValidator.Enabled=false
EDIT:
Setting AutoPostBack=true for the DropDownList helped

Related

Hide And Show Button in Visual Basic

Greet, I new with Visual Basic and I wan't to ask something. Is it possible to make hide and show button with only one button. For example, this 'X' button could show or hide this 'A' button without using another button to show and to hide button? I already learn to do this using Checked property as a toggle only like this coding.
If mnuViewTextboxes.Checked = True Then
TextBox1.Visible = True
Else
TextBox1.Visible = False
End If
add timer to your form and do something like
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If mnuViewTextboxes.Checked = True Then
TextBox1.Visible = True
ElseIf mnuViewTextboxes.Checked = false Then
TextBox1.Visible = False
End If
End Sub

Enabling/Disabling RequiredFieldValidator

I have 2 combo box drop downs. On page load the 2nd drop down with validator are disabled. If the user selects any value from the first drop down I enable it. The 2nd drop down enables but the validator does not.
page load
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
ComBox_GER2.Enabled = False
Valid_GER2.Enabled = False
End Sub
Is fired ComBox_GER1.SelectedIndexChanged
If ComBox_GER1.SelectedIndex = -1 Then
Else
ComBox_GER2.Enabled = True
Valid_GER2.Enabled = True
End If
in every Page_Load add
AjaxManager.AjaxSettings.AddAjaxSetting(ComBox_GER1,ComBox_GER2)
AjaxManager.AjaxSettings.AddAjaxSetting(ComBox_GER1,Valid_GER2)
This should work.
Use ValidatorEnable(ValidatorControlName,Boolean) javascript function
//Example:
ValidatorEnable(document.getElementById('<%= Valid_GER2.ClientID %>'), true);

If Statement to display Label or Button in aspx

I am new to aspx so can someone help me with the following small bit of code. I am trying to create an If statment in aspx.net using Vb, but I can't get it to work correctly.
I am trying to ask a questions and the user has to select a yes checkbox or no checkbox and depending on the answer either a label will appear saying "Not permitted" or a button will appear to link to main form.
I am having problems making either the button or label appear when the user selects choice.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Main_FormBtn.Visible = False
Refer_Label.Visible = False
If CheckBoxNo.Checked = True Then
Refer_Label.Visible = True
ElseIf CheckBoxYes.Checked = True Then
Main_FormBtn.Visible = True
End If
End Sub
Any help would be greatly appricated
thanks
If it is YES OR NO Category Better you use RadioButton Not Checkbox..
Try like below it will help you...
YESRadio_CheckedChanged EVENT
Main_FormBtn.Visible = True
Refer_Label.Visible = False
NORadio_CheckedChanged EVENT
Refer_Label.Visible = True
Main_FormBtn.Visible = False
PAGE_LOAD EVENT
Main_FormBtn.Visible = False
Refer_Label.Visible = False
Before that Set AutoPostBack="true" for RadioButtons
If this is meant to be a form where users go through and answer a number of questions and sub-options appear accordingly there's a number of issues you need to consider. If you want the boxes/labels to show hide when a user clicks something you might need to use javascript/jquery unless you do a postback, in which case you might need to consider that a user might already have selected a number of options/filled out some part of the form.
If you want to share the overall structure of the page it will be easier to give proper advice.
Well as this is on page load, why would you even need this IF statement here? I'm thinking that you actually want this statement within the Click event of the checkbox, not on the page load. This code will only run on the load, not the click.
Double click your ASP button and put this code within the event handler there. Point both of the two checkbox click events to the same method like so;
Page load - You only want to set these to false on the first load.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Main_FormBtn.Visible = False
Refer_Label.Visible = False
End If
End Sub
Click event
Private Sub Checkbox_Click(sender As System.Object, e As System.EventArgs) Handles CheckBoxNo.Click, CheckBoxYes.Click
Main_FormBtn.Visible = False
Refer_Label.Visible = False
If CheckBoxNo.Checked = True Then
Refer_Label.Visible = True
ElseIf CheckBoxYes.Checked = True Then
Main_FormBtn.Visible = True
End If
End Sub
For this to work, it will require a postback. Unless you fancy going into Javascript land.

[javascript]Validate Gridview Footer Checkbox

[Asp.net / Vb.Net]
How can I validate my gridview footer checkbox using Javascript.
I have a gridview with footer having 8 checkboxes -(chkActive1,chkActive2,.....chkActive3) -
The condition I want to jave is - The user need to check atleast 1 checkbox or else the trasaction would not allow
thanks..
so simple, every check box have unique id check in view source there is some patent in name in controls when you check viewsource of that page, just use that, its simple way.
If I misunderstood you then please correct me, I think this could be done via using a custom validator in asp.net
Since you haven't mentioned I am assuming you are using CheckBoxList Control.
On .aspx page use custom validator
<asp:CustomValidator ID="customValidatorForCheckboxlist" runat="server"
ErrorMessage="Required Field" ValidationGroup="valSurvey"
OnServerValidate="CheckifCheckBoxIsEmpty" SetFocusOnError="true" Display="Dynamic"></asp:CustomValidator>
On codebhind using this logic to iterate through your gridview row and check for footer row. Inside footer row iterate through all checkboxes and then see if they are selected or not. I am using a boolean field to check if anything was selected. Jump out if anything is selected.
Protected Sub CheckifCheckBoxIsEmpty(ByVal sender As Object, ByVal e As ServerValidateEventArgs)
Dim valbool As Boolean = False
For Each gvrow As GridViewRow In gridview_1.Rows
'Check for footer row.
If gvrow.RowType = DataControlRowType.Footer
For Each ct As Control In gvrow.Cells(1).Controls
If ct.GetType.ToString().Equals("System.Web.UI.WebControls.CheckBoxList") Then
Dim _checkboxlist As CheckBoxList = DirectCast(ct, CheckBoxList)
For Each ListItem1 As ListItem In _checkboxlist.Items
If ListItem1.Selected = True Then
valbool = True
Exit For
Else
valbool = False
End If
Next
End If
Next
End If
Next
If valbool = False Then
e.IsValid = False
Else
e.IsValid = True
End If
End Sub

VB - button click event not working properly

In my windows app i have a button which is only visible when a user selects a certain value on a DropDownList.
For some reason the button does not work when i click on it.
I've enabled the button on page load and it works, however when i choose the value on the Dropdownlist the button does not work.
Is there something im missing here? any feedback would be greatly appreciated. Thanks
Code:
Protected Sub DropDownList4_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DropDownList4.SelectedIndexChanged
If DropDownList4.SelectedValue = "Yes" Then
btnInsert.Visible = True
Endif
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnInsert.Click
Response.Redirect("Menu.aspx")
DropDownList4.SelectedValue = "Yes"
txtfirstName.Text = ""
txtSurname.Text = ""
txtJobTitle.Text = ""
txtCountry.Text = ""
txtWork.Text = ""
DropDownList7.SelectedValue = ""
End Sub
From what we can see from your provided code all you are doing is changing visibility. Are you disabling the button anywhere else? Also Check the properties of the button in the design view to ensure that you did not accidentally changed the Enabled property to "False". It has been a while since I have done any web applications but if you are programically changing the enabled value of the button, I would suggest adding an Enabled = True line to your selected index change subroutine:
If DropDownList4.SelectedValue = "Yes"
Then
btnInsert.Visible = True
btnInsert.Enabled = True
Endif
It has been a while for me so I can't remember if it is .Enabled or something else.
There is also the possibility that you have a Panel or some such over your button and that is preventing you from actually clicking on the button.
Also, put a break point on the first line of your button click event and make sure that you are not getting there as opposed to entering the code and it not running the way you expect.
Try SelectedItem.Value instead, like this
Protected Sub DropDownList4_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DropDownList4.SelectedIndexChanged
If DropDownList4.SelectedItem.Value = "Yes" Then
btnInsert.Visible = True
Endif
End Sub

Resources