looping through formview controls

J

jobs

Imports System.Data.SqlClient
Imports System.Data


Partial Class CreateRoute
Inherits System.Web.UI.Page
Private DATAFEED As RTEWEB.DataFeed

Protected Overrides Sub OnPreRender(ByVal e As EventArgs)

End Sub

Protected Overrides Sub OnLoad(ByVal e As EventArgs)
If Not IsPostBack Then
Dim up As New RTEWEB.UserProfile(Page.User.Identity)
'If Not up.IsAllowed(up.CurrentUser,
Request.AppRelativeCurrentExecutionFilePath.ToString) Then
' Response.Redirect("~/NoAccess.htm")
'End If
Else
StatusLabel.Text = Nothing
End If
End Sub



Sub GetControl(ByVal prt As Control)

For Each c As Control In prt.Controls

If c.GetType().ToString =
"System.Web.UI.WebControls.TextBox" Then

Dim tb As TextBox = c
Response.Write(tb.Text)


End If

If c.Controls.Count > 0 Then
GetControl(c)


End If

Next

End Sub

Function ValidData(ByVal whichformview As Control) As Boolean
Dim returnvalue As Boolean = True
Dim what, what2, what3, what4 As String
For Each c As Control In whichformview.Controls
what = c.ClientID
what2 = c.GetType().ToString
what3 = CType(RouteFormView.FindControl("iRouteDescr"),
TextBox).Text
what4 = CType(RouteFormView.FindControl("iRouteCode_req"),
TextBox).Text

If c.GetType().ToString =
"System.Web.UI.WebControls.TextBox" Then
If c.ClientID.Contains("_date") Then
End If
If c.ClientID.Contains("_num") Then
End If
If c.ClientID.Contains("_req") Then
If CType(c, TextBox).Text = Nothing Then
StatusLabel.Text = +c.ID + "is Required" +
vbCrLf
c.Focus()
returnvalue = False
End If
End If
End If
Next
Return returnvalue

End Function



Function ValidData2(ByVal whichformview As FormView) As Boolean

If IsPostBack Then
Dim returnvalue As Boolean = True
StatusLabel.Text = Nothing
'If Not
IsNumeric(CType(whichformview.Row.FindControl("RateTextBox"),
TextBox).Text.ToString) Then
' StatusLabel.Text = +"Invalid Rate entered" + vbCrLf
'CType(whichformview.Row.FindControl("RateTextBox"),
TextBox).Focus()
'returnvalue = False
'End If
If CType(whichformview.Row.FindControl("routecode"),
TextBox).Text = Nothing Then
StatusLabel.Text = +"Route Code is Required" + vbCrLf

CType(whichformview.Row.FindControl("RouteCodeTextBox"),
TextBox).Focus()
returnvalue = False
End If
If Not
Regex.IsMatch(CType(whichformview.Row.FindControl("StartDateTextBox"),
TextBox).Text, "^\d{1,2}/\d{1,2}/\d{4}\s\d{1,2}:\d{2}:\d{2}\s(AM|PM)$")
Then

CType(whichformview.Row.FindControl("StartDateTextBox"),
TextBox).Focus()
StatusLabel.Text = +"Start Date not in ""mm/dd/yyyy
12:00:00 AM"" Format" + vbCrLf
returnvalue = False
End If
If Not
Regex.IsMatch(CType(whichformview.Row.FindControl("EndDateTextBox"),
TextBox).Text, "^\d{1,2}/\d{1,2}/\d{4}\s\d{1,2}:\d{2}:\d{2}\s(AM|PM)$")
Then
CType(whichformview.Row.FindControl("EndDateTextBox"),
TextBox).Focus()
StatusLabel.Text = +"End Date not in ""mm/dd/yyyy
11:59:59 PM"" Format" + vbCrLf
returnvalue = False
End If
Return returnvalue
Else
Return False
End If

End Function

Function Already(ByVal vtable As String, ByVal vfield As String,
ByVal vvalue As String) As Boolean
Dim MySQL As String = "select distinct " + vfield + " from " +
vtable + " where " + vfield + " = '" + vvalue + "'"
Dim Conn As SqlConnection = New
SqlConnection(ConfigurationManager.ConnectionStrings("RTEWEBConnectionString").ConnectionString.ToString)
Dim Cmd As New SqlCommand(MySQL, Conn)
Conn.Open()
Cmd.ExecuteScalar()
If Cmd.ExecuteScalar() = 0 Then
Return True
Else
Return False
End If
Conn.Close()
End Function


Protected Sub RouteClear(ByVal sender As Object, ByVal e As
System.EventArgs)
Response.Redirect("CreateRoute.aspx")
End Sub
Protected Sub RouteInsert(ByVal sender As Object, ByVal e As
System.EventArgs)
End Sub
Protected Sub DestOninserting(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles
DestandRouteInsertSource.Inserting
If ValidData(RouteFormView) = True Then
If Already("Destination", "destcode",
CType(RouteFormView.Row.FindControl("RateTextBox"),
TextBox).Text.ToString) Then
StatusLabel.Text = +"Duplicate Destination Code" +
vbCrLf
e.Cancel = True
End If
Else
e.Cancel = True
End If

End Sub

Protected Sub DestOninserted(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles
DestandRouteInsertSource.Inserted
If e.Exception IsNot Nothing Then
If e.Exception.ToString.Contains("duplicate key") Then
StatusLabel.Text = "Duplicate Entry - No insert
performed"
Else
StatusLabel.Text = e.Exception.ToString.Substring(0,
250)
End If
e.ExceptionHandled = True
Else
StatusLabel.Text = "New Records Added"
End If

End Sub

End Class
 
M

MikeS

I think I am missing the point here.

Why is it that the validation conrols can't do most of this for you?

Are you looking for someone to refator this code and perhaps get it to
meet some shops standards, including removing the potentail sql
injection vulnerability?
 
M

MikeS

I apologize, I see your original post in another forum now where you
asked for no judgement and excluded the use of the validation controls.

I do think if you clean up you algorithms as much as possible the bugs
would be easier for you to find and fix, or for others to give you
advice on how to fix.
 
J

jobs

I posted the full source and only meant to post just the function.

Perhaps my approach is way off, but it does change the fact that I
think I should be able to loop through controls in formview:

Simply stated, how do I loop through formview controls? If you can,
disregard my intentions.

Thank you and my appoligies.

Function ValidData(ByVal whichformview As FormView) As Boolean
Dim returnvalue As Boolean = True
Dim what, what2, what3, what4 As String
For Each c As Control In whichformview.Controls


what = c.ClientID
what2 = c.GetType().ToString
what3 = CType(whichformview.FindControl("iRouteDescr"),
TextBox).Text
what4 = CType(whichformview.FindControl("iRouteCode_req"),
TextBox).Text

If c.GetType().ToString =
"System.Web.UI.WebControls.TextBox" Then
If c.ClientID.Contains("_date") Then
End If
If c.ClientID.Contains("_num") Then
End If
If c.ClientID.Contains("_req") Then
If CType(c, TextBox).Text = Nothing Then
StatusLabel.Text = +c.ID + "is Required" +
vbCrLf
c.Focus()
returnvalue = False
End If
End If
End If
Next
Return returnvalue

End Function
 
J

jobs

Perhaps you're right, so far it appeared I needed to ad a tag with
every validtion type to every textbox i needed to validate on... I
will look into it, but am still curious about the problem at hand..
even if misguided in my efforts.

sorry, I did not mean to post the send all that code... simply stated,
how do I loop through a formviews control collection?

thanks in advance for help or information!

this code has me stuck at ChildTable returning a single entry..

Function ValidData(ByVal whichformview As FormView) As Boolean
Dim returnvalue As Boolean = True
Dim what, what2, what3, what4 As String
For Each c As Control In whichformview.Controls ' (ONLY
RETURNS 1 ENTRY OF CHILDTABLE)


If c.GetType().ToString =
"System.Web.UI.WebControls.TextBox" Then
If c.ClientID.Contains("_date") Then
End If
If c.ClientID.Contains("_num") Then
End If
If c.ClientID.Contains("_req") Then
If CType(c, TextBox).Text = Nothing Then
StatusLabel.Text = +c.ID + "is Required" +
vbCrLf
c.Focus()
returnvalue = False
End If
End If
End If
Next
Return returnvalue

End Function
 
M

MikeS

I am not sure, but maybe you have to do it recursively.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If IsPostBack = True Then DoValidate(Me.Controls)
End Sub

Sub DoValidate(ByVal col As ControlCollection)
For Each c As Control In col
Response.Write(c.GetType.ToString & "<br />")
DoValidate(c.Controls)
Next
End Sub
 
J

jobs

Not sure if that's tested code, but could not get anything like that to
work. The whole formview thing is when you did a little into how it's
organized.
 
M

MikeS

I took that code straight out of a page where it works and prints out
all controls on the page. I was gussing you could put youe validation
in place of the response.writes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top