parameter:currentwebform to a vb class

G

Guest

Hello,
I'd like to use a visual basic class.function to iterate through the html
controls
and verify that the user entered something in the controls. But I cannot
figure out how to pass the current webform to the function.
Would anyone know how to do this?
I currently have this code in a function:
For Each child In c.Controls
If child.GetType.ToString = "System.web.ui.webcontrols.textbox" Then
txtbox = CType(child, TextBox)
' no blanks allowed.
If txtbox.Text.Equals("") Then
txtbox.BackColor = Color.Magenta
txtbox.Text = "required."
End If
End If

If child.GetType.ToString =
"System.web.ui.webcontrols.dropdownList" Then
ddl = CType(child, DropDownList)
' no blanks allowed.
If ddl.SelectedValue.Equals("") Then
ddl.BackColor = Magenta
End If
End If


Next
End If
Next

Thanks,
 
B

bpd

Hello,
I'd like to use a visual basic class.function to iterate through the html
controls
and verify that the user entered something in the controls. But I cannot
figure out how to pass the current webform to the function.
Would anyone know how to do this?
I currently have this code in a function:
For Each child In c.Controls
If child.GetType.ToString = "System.web.ui.webcontrols.textbox" Then
txtbox = CType(child, TextBox)
' no blanks allowed.
If txtbox.Text.Equals("") Then
txtbox.BackColor = Color.Magenta
txtbox.Text = "required."
End If
End If

If child.GetType.ToString =
"System.web.ui.webcontrols.dropdownList" Then
ddl = CType(child, DropDownList)
' no blanks allowed.
If ddl.SelectedValue.Equals("") Then
ddl.BackColor = Magenta
End If
End If

Next
End If
Next

Thanks,


Why not just use the requiredFieldValidator on the controls?
 
B

bpd

Hello,
I'd like to use a visual basic class.function to iterate through the html
controls
and verify that the user entered something in the controls. But I cannot
figure out how to pass the current webform to the function.
Would anyone know how to do this?
I currently have this code in a function:
For Each child In c.Controls
If child.GetType.ToString = "System.web.ui.webcontrols.textbox" Then
txtbox = CType(child, TextBox)
' no blanks allowed.
If txtbox.Text.Equals("") Then
txtbox.BackColor = Color.Magenta
txtbox.Text = "required."
End If
End If

If child.GetType.ToString =
"System.web.ui.webcontrols.dropdownList" Then
ddl = CType(child, DropDownList)
' no blanks allowed.
If ddl.SelectedValue.Equals("") Then
ddl.BackColor = Magenta
End If
End If

Next
End If
Next

Thanks,


Why not just use the requiredFieldValidator on the controls?
 
G

Guest

BPD,
Because I have to do the same thing on 6 pages and I'm too lazy for gui's.
I'd rather call a function to do all the work for me.
Plus the function would be run on the server, not the client's machine.
Chieko
 
B

bpd

BPD,
Because I have to do the same thing on 6 pages and I'm too lazy for gui's.
I'd rather call a function to do all the work for me.
Plus the function would be run on the server, not the client's machine.
Chieko






- Show quoted text -

Not sure, but you might be able to use Page.

protected void some_procedure()
{
if (process_page_procedure(Page))
{
do something...
}
}

static bool process_page_procedure(Page p)
{
do something with p.Form.Controls
}
 
G

Guest

Thanks,
I'll give it a try.
Chieko

bpd said:
Not sure, but you might be able to use Page.

protected void some_procedure()
{
if (process_page_procedure(Page))
{
do something...
}
}

static bool process_page_procedure(Page p)
{
do something with p.Form.Controls
}
 
G

Guest

Here's the whole function that you can call from the code behind in the
xxx.aspx.vb page:

In a vb class write a function similar to this:

Public Function SetWebControls(ByRef webpage As System.Web.UI.Page) As Boolean
Dim bRet As Boolean = True
Dim d As System.Web.UI.Control
Dim dChild As System.Web.UI.Control
Dim txtbox As System.Web.UI.WebControls.TextBox

Dim ddlDropDownList1 As System.Web.UI.WebControls.DropDownList
Dim ddlDropDownList2 As System.Web.UI.WebControls.DropDownList
Dim ddlDropDownList3 As System.Web.UI.WebControls.DropDownList

Dim type As String 'for debugging
Dim type2 As String ' for debugging
Dim textboxname As String ' for debugging.
Dim dsStates As New DataSet
Dim dsCountries As New DataSet
Dim dsSecurityQuestions As SqlClient.SqlDataReader

Dim security As New ISEERegistrationBusiness.Security.Security
Dim i As Integer 'counter

For Each d In webpage.Controls
If d.Controls.Count > 0 And d.GetType.ToString = _
"System.Web.UI.HtmlControls.HtmlForm" Then
type2 = (d.GetType.ToString) ' for debugging
For Each dChild In d.Controls
' set the textboxes
If dChild.GetType.ToString = "System.Web.UI.WebControls.TextBox"
Then
txtbox = CType(dChild, System.Web.UI.WebControls.TextBox)
txtbox.Text = "required"
txtbox.MaxLength = 40
End If
' set the drop down list : ddlDropDownList1
If dChild.GetType.ToString =
"System.Web.UI.WebControls.DropDownList" Then
If dChild.ID.ToString = "ddlDropDownList1" Then
ddlDropDownList1 = CType(dChild, System.Web.UI.WebControls.DropDownList)

' do something ie: For i = 0 To
dsStates.tables(0).rows.Count - 1
ddlDropDownList1.Items.Add(New
System.Web.UI.WebControls.ListItem(dsStates.Tables(0).Rows(i).Item("State")))
Next
End If
End If

' set the drop down list : ddlDropDownlist2
If dChild.ID.ToString = "ddlDropDownlist2" Then
ddlDropDownList2 = CType(dChild,
System.Web.UI.WebControls.DropDownList)
' doSomething like :
For i = 0 To dsCountries.Tables(0).Rows.Count - 1
ddlDropDownList2.Items.Add(New
System.Web.UI.WebControls.ListItem(dsCountries.Tables(0).Rows(i).Item(0)))
Next
End If
End If

'set the drop down list : ddlDropDownList3
If dChild.ID.ToString = "ddlDropDownList3" Then
ddlDropDownList3 = CType(dChild,
System.Web.UI.WebControls.DropDownList)
'Do something like:
While dsSecurityQuestions.Read

ddlDropDownList3.Items.Add(dsSecurityQuestions.Item("securityQuestionID") & "
" & dsSecurityQuestions.Item("securityQuestion"))
End While
End If
End If

End If
Next
End If
Next
return bRet
End Function

' call the function from the xxx.aspx.vp page.
If not SetWebControls(Me) then
response.Write "Page could not be loaded."
End If
 

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

Top