how can i validate textbox1 and textbox 2.
here is the thing i needed.
if i fill in textbox1 then i must fill in textbox2 if either one is empty must show error message.
if both both textboxes are empty then no error, proceed to the result
how to do that, can anyone show me how to do
-the bellow code is where i just validate the date either greater than or not
Private Sub cvCmpDate_ServerValidate(ByVal source As Object, ByVal args As ServerValidateEventArgs) Handles cvCmpDate.ServerValidate
If CompareMyDate.CompareDate(txtDateStart.Text.Trim, txtDateEnd.Text.Trim) Then
cvCmpDate.ErrorMessage = "* Activation Start Date cannot greater than Activation End Date."
args.IsValid = False
End If
End Sub
Select Case True
Case String.IsNullOrWhitespace(txtDateStart.Text) AndAlso Not String.IsNullOrWhitespace(txtDateEnd.Text)
' Show Error
Case String.IsNullOrWhitespace(txtDateEnd.Text) AndAlso Not String.IsNullOrWhitespace(txtDateStart.Text)
' Show Error
Case CompareMyDate.CompareDate(txtDateStart.Text.Trim, txtDateEnd.Text.Trim)
cvCmpDate.ErrorMessage = "* Activation Start Date cannot greater than Activation End Date."
args.IsValid = False
End Select
Related
I have radio button list that calls a function.
If the function returns true then I want to change the value.
However if the function returns false then I do not want to change the value and keep the original selection value.
Currently it changes the value even when the statement returns false.
Any advice?
ASP Page
<asp:RadioButtonList ID="rblType" runat="server" AutoPostBack="True"
DataSourceID="SqlData" DataTextField="Type"
DataValueField="TypeID">
</asp:RadioButtonList>
VB file
Private selectionvalue As Integer
Protected Sub rblType_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles rblType.SelectedIndexChanged
Dim CheckListValidation As Boolean = CheckListBox()
If CheckListValidation = True Then
selectionvalue = rblType.SelectedItem.Value
Else
rblType.SelectedItem.Value = selectionvalue
End If
End Sub
Function CheckListBox() As Boolean
If lstbox.Items.Count <> "0" Then
If MsgBox("Are you sure you want to change option?", MsgBoxStyle.YesNo, " Change Type") = MsgBoxResult.Yes Then
Return True
Else
Return False
End If
Else
Return True
End If
End Function
The problem is when rblType_SelectedIndexChanged is executed, the selected item is already changed and the RadioButtonList doesn't "remember" the previously selected value. You need to keep the previously selected value between postbacks in order to achieve this.
I would suggest using ViewState. Create a property in code behind to represent the ViewState value:
Private Property PreviousSelectedValue() As String
Get
If (ViewState("PreviousSelectedValue") Is Nothing) Then
Return String.Empty
Else
Return ViewState("PreviousSelectedValue").ToString()
End If
End Get
Set(ByVal value As String)
ViewState("PreviousSelectedValue") = value
End Set
End Property
and in rblType_SelectedIndexChanged:
Protected Sub rblType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rblType.SelectedIndexChanged
Dim CheckListValidation As Boolean = CheckListBox()
If CheckListValidation = True Then
'save the currently selected value to ViewState
Me.PreviousSelectedValue = rblType.SelectedValue
Else
'get the previously selected value from ViewState
'and change the selected radio button back to the previously selected value
If (Me.PreviousSelectedValue = String.Empty) Then
rblType.ClearSelection()
Else
rblType.SelectedValue = Me.PreviousSelectedValue
End If
End If
End Sub
In some ASP.Net code-behind, I call the SelectRow method of a custom grid control that inherits from System.Web.UI.WebControls.GridView.
Making the call:
If (ProgressGrid.Rows.Count > 0) Then
ProgressGrid.SelectRow(0)
End If
As expected, this generates a SelectedIndexChanged event, which is picked up by the handler:
Protected Sub ProgressGrid_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ProgressGrid.SelectedIndexChanged
Using db As New DataContext
Dim course = (From c In db.CourseResults
Where c.MemberID = MemberID AndAlso c.ResultID = CInt(ProgressGrid.SelectedDataKey.Value)
Select c).Single
' more code here
End Using
End Sub
My problem is that ProgressGrid.SelectedDataKey is nothing inside my event handler, causing a null-reference error. While debugging with Visual Studio 2010, I can see from the call stack that the ProgressGrid.SelectRow(0) was hit and that the ProgressGrid.Rows.Count is greater than zero. So why are all the "Selected..." properties on the ProgressGrid object set to nothing or -1? Any idea what I am doing wrong?
The custom Grid class contains this property which overrides the default GridView behavior:
Public Overrides Property SelectedIndex() As Integer
Get
If AutoPostback Or AllowSelect = False Then
Return MyBase.SelectedIndex
Else
If HttpContext.Current Is Nothing Then
Return Nothing
Exit Property 'Exit if in design mode
End If
Dim index As String = Page.Request(Me.ClientID + "_SelectedRow")
If (String.IsNullOrEmpty(index)) Then
If (ViewState("SelectedIndex") Is Nothing) Then
Return -1
Else
Return ViewState("SelectedIndex")
End If
Else
ViewState.Add("SelectedIndex", index)
Return CType(index, Integer)
End If
End If
End Get
Set(ByVal value As Integer)
MyBase.SelectedIndex = value
End Set
End Property
The debugger is unable to display the details for MyBase and the first calls to MyBase.SelectedIndex = value have the debugger's quick watch window return a null reference error. Once I reach the event handler, break points in the above property indicate that MyBase.SelectedIndex is nothing, despite attempting to set it to zero.
I discovered that setting the ViewState in the custom grid's SelectedIndex property fixed my problem. This allowed ViewState to hold the new index value and return it when the Get method was called on the property.
Set(ByVal value As Integer)
MyBase.SelectedIndex = value
ViewState("SelectedIndex") = value
End Set
I have the following code...
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As EventArgs) Handles GridView1.RowUpdating
Dim SocioNumInfo As Integer = CInt(GridView1.Rows(GridView1.SelectedIndex).Cells(4).Text)
MsgBox(SocioNumInfo.ToString)
...
End Sub
Now, this code should read the cell, but it gives me the following error:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
The MsgBox is just there for me to check if the data is being read, at the end of the day it should turn that into a parameter so I can add to the DB... but still.. nope nothing. Is that the correct code to read directly from a cell in a choosen row? In the "Protected Sub" area I already tried SelectedIndexChanging and RowEditing, etc... and still nothing. Still throws the error at me.
If I try
Dim SocioNumInfo As String = CStr(GridView1.Rows(GridView1.SelectedRow).Cells(4).Text) it gives me a "cannot be converted to Integer" error.
If no row is selected then GridView1.SelectedIndex is -1. You might have to add a check
If GridView1.SelectedIndex <> -1 Then
' Use GridView1.SelectedIndex here
End If
However, it is easier to access the selected row like this:
Dim row = GridView1.SelectedRow
If Not row Is Nothing AndAlso IsNumeric(row.Cells(2).Text) Then
Dim SocioNumInfo As Integer = CInt(row.Cells(2).Text)
....
End If
Note that the cell index is zero based. row.Cells(4) is in the 5th column.
I am trying to pass a value from the field ReportID (which is a number field) on form 1 to the field ReportID on the form called Budget_fsub after I click a button to open that second form. I've tried this code to pass the value to the second form, and it looks like it works but when I go to the table which holds the values for the second form, the value has not been recorded. What am I doing wrong?
Form1 with button that opens form2:
Private Sub cmdEnterBudgetInfo_Click()
On Error GoTo Err_EnterBudgetInfo_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "Budget_fsub"
DoCmd.OpenForm stDocName, , , , acFormAdd, , Me![ReportID]
Exit_cmdEnterBudgetInfo_Click
Exit Sub
Err_EnterBudgetInfo_Click:
MsgBox Err.Description
Resume Exit_cmdEnterBudgetInfo_Click
End Sub
And here's the code on the form Budget_fsub:
Private Sub Form_Open(Cancel As Integer)
Dim defaultID As Long
defaultID = CLng(Nz(Me.OpenArgs, 0))
If defaultID = 0 Or IsNull(Me.OpenArgs) Then
Cancel = True
Exit Sub
End If
Me.ReportID.DefaultValue = defaultID
End Sub
So, here is a working solution. I assume you have bound form and ReportID textbox is bound to the column. Please note, I have never been fan of using macros to manipulate the data..
Here is code for your Form1:
Private Sub cmdEnterBudgetInfo_Click()
On Error GoTo Err_EnterBudgetInfo_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "Budget_fsub"
'close the form first
DoCmd.Close acForm, stDocName, acSaveYes
DoCmd.OpenForm stDocName, , , , acFormAdd, , Me![ReportID]
Exit_cmdEnterBudgetInfo_Click:
Exit Sub
Err_EnterBudgetInfo_Click:
MsgBox Err.Description
Resume Exit_cmdEnterBudgetInfo_Click
End Sub
Here is code for your sub form (Budget_fsub):
Option Compare Database
Option Explicit
'global variable
Dim repID As Long
'form load event
Private Sub Form_Load()
Me.ReportID.Value = repID
DoCmd.RunCommand acCmdSave
End Sub
'form open event
Private Sub Form_Open(Cancel As Integer)
SetReportID
If repID = 0 Then
Cancel = True
Exit Sub
End If
End Sub
'function to set reportID from the openargs
Private Function SetReportID()
repID = CLng(Nz(Me.OpenArgs, 0))
End Function
Hope this helps.
I have a question related to asp.net and vb.net. I have a repeater control that I bind some data to and allow users to update/change fields within certain text boxes. I added a validation control to trigger when the user doesn't enter a valid date or the text "TBD". On submit, I want to go through and highlight each field where its corresponding validator is not valid. This is my current code, but I am lost as to how to find the text box control.
Sub ValidateDateField(ByVal sender As Object, _
ByVal args As ServerValidateEventArgs)
'validate against three conditions - date, "TBD", and "N/A"
Dim dtValue = args.Value
If dtValue.ToUpper = "TBD" Or dtValue.ToUpper = "N/A" Then
args.IsValid = True
ElseIf IsDate(dtValue) Then
args.IsValid = True
Else
args.IsValid = False
Dim cont As WebControl = DirectCast(Page.FindControl(args.ToString), WebControl)
cont.BackColor = Drawing.Color.White
util.Client_Alert("Please Update Highlighted Fields")
End If
End Sub
I am completely lost as to how to get cont = textbox1row1 of my repeater control. Please advise. All examples I have seen so far directly state the control as in text1.BackColor =
So I figured out the answer. Sorry for the question. Don't know why I didn't think about it sooner. I found the naming container of the my custom val and then found the control based on its name in the repeater. in vb.net it was something like this:
Sub ValidateDateField(ByVal sender As Object, _
ByVal args As ServerValidateEventArgs)
'args holds value. validate against three conditions - date, "TBD", and "N/A"
Dim dtValue = args.Value
Dim cont As CustomValidator = sender
Dim myNC As Control = cont.NamingContainer
Dim strControl As String
strControl = cont.ControlToValidate
Dim txtbox As TextBox = _
DirectCast(myNC.FindControl(cont.ControlToValidate), TextBox)
If dtValue.ToUpper = "TBD" Or dtValue.ToUpper = "N/A" Or IsDate(dtValue) Then
args.IsValid = True
txtbox.BackColor = Drawing.Color.Empty
Else
args.IsValid = False
txtbox.BackColor = Drawing.Color.White
End If
End Sub