Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop through form elements vbscript

Status
Not open for further replies.

GoTerps88

Programmer
Apr 30, 2007
174
US
I want to be able to loop through form elements of type checkbox on form. How would I accomplish this in VBScript?

A sample would be great. I wasn't able to located on online.

Thanks...
 
How does this relate to ASP? In order to loop through the form elements, the form elements have to exist first. The elements won't exist until after all the server side code executes.






[monkey][snake] <.
 
This function would run on a button click after the page has been loaded, so the form elements would exist. I've done this in Javascript.
 
It seems like you want to do something with client-side script.

Although it is possible to write client-side script using VBScript, it is not advisable. You're usually better off doing the client-side work in JavaScript. I prefer to write server-side logic in VBScript and client-side logic in JavaScript so I never have to wonder if I'm looking at server or client code.
 
Hi,
In this VBScript snippet I loop through all the controls on a page and, if they are of a particular type and are in a visible panel,take some action( Ignore the actions taken just look at the code that does the scanning for controls..)
Code:
 Dim Fctl,Ptl as Control
          Dim ValStr,NamStr As String
          Dim ix As Integer
          For each Fctl in form1.Controls
           If  Fctl.GetType() Is GetType(Panel) Then
          
           	If Ctype(Fctl,Panel).Visible = True  Then
           		
           		For Each Ptl in Ctype(Fctl,Panel).Controls
           		
                If Ptl.GetType() Is GetType(ListBox) Then
                	
                	NamStr = NamStr & CType(Ptl, ListBox).ID & ","
                 	For ix = 0 To 	CType(Ptl, ListBox).Items.Count - 1
                 	
                	  If CType(Ptl, ListBox).Items(ix).Selected Then             
                     	ValStr = ValStr & CType(Ptl, ListBox).Items(ix).Value & ","
                    	VPrmpt = VPrmpt & CType(Ptl, ListBox).Items(ix).Value & "," 
                    End If
                    
                  Next  ' ix                
                   If Session("Viewer") =  "Actx" Then                    
                     VPrmpt = Left(VPrmpt,Len(VPrmpt) - 1)
                     Prmpts = CwrPrefix & CType(Ptl, ListBox).ID & "=" & VPrmpt
                     FullStr = FullStr & Prmpts
                   End If 
                   VPrmpt=""            
                   ValStr = Left(ValStr,Len(ValStr) - 1) & ";"                      
             
              'End If   ' Finished with testing Type of Control in Panel
              ElseIf Ptl.GetType() Is GetType(TextBox) Then     
                   	NamStr = NamStr & CType(Ptl, TextBox).ID & ","     
                  	ValStr = ValStr & CType(Ptl, TextBox).Text & ";"
                  	VPrmpt = VPrmpt & CType(Ptl, TextBox).Text & ";" 
               	 If Session("Viewer") =  "Actx" Then                    
                    VPrmpt = Left(VPrmpt,Len(VPrmpt) - 1)
                    Prmpts = CwrPrefix & CType(Ptl, TextBox).ID & "=" & VPrmpt
                    FullStr = FullStr & Prmpts
                 End If 
                 VPrmpt=""
           End If
         Next   ' Ptl inPanel Control
      End If
     End If
    Next

[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
Ah yes but it is client-side logic, not ASP logic.

What I mean is, although it could be included in a file with a .ASP extension, it is not actually executed on the server by the ASP engine... it could run just as well in an .HTM file because the logic is run by the IE browser.
 
Hi,
This is .NET 2.0 by the way ( sorry for not specifying) and all the actions are set to run at Server..Maybe it is a definitional thing but I have always thought that that meant server-side actions, not client.
snippet:
Code:
<form id="form1" runat="server">
 <td>
  		<button id="Button1"
              OnServerClick="ShowChoice"
              runat="server">

   Run Report

    </button></td>
</form>



[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
Ah, ASP.NET is a totally different creature... it has its own forum: forum855

Basically the way [tt]runat="server"[/tt] works in ASP.NET is it tells the server script to make an object in memory that resembles the HTML tag you stick it in. So for example if you put it in an HTML tag for a textbox it will create an object with a Value property and if you put it on an IMG tag it will create an object with a SRC property. Then you can manipulate the value in code and the ASP.NET will send the final resulting HTML to the browser.

So in classic ASP you might do this:
Code:
<% If x > 10 Then %>
      <img src="big.gif">
<% else %>
      <img src="small.gif">
<% end if %>


But in ASP.NET you could do this:
Code:
<script runat="server">
    If x > 10 Then 
      foo.Src = "big.gif"
    Else
      foo.Src = "small.gif"
    End If        
</script>

<img id="foo" runat="server" />


Both the ASP and ASPX will produce an HTML image tag with the proper value of SRC so the browser will display them the same. The advantage to the ASPX version is it helps keep the logic a bit more separated from the HTML.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top