Print Page | Close Window

Problem looping through controls on a web

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=5417
Printed Date: 29 March 2026 at 7:00pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Problem looping through controls on a web
Posted By: jamesb
Subject: Problem looping through controls on a web
Date Posted: 01 September 2003 at 7:20am

I'm trying to write a function that loops through all the controls on a web form, and puts the values entered by the user into a string, which will then be passed into the database using a stored procedure.

For some reason that I simply can't fathom, the stringbuilder class loses the string it contains at certain points during the cycle through the controls on the page. The page contains 5 panel objects, and the stringbuilder loses the values stored in it when the "GetControlValues" function moves from one panel to the next. Why would this be?

Hope I've explained this well enough - all code is below - can anyone suggest what I'm doing wrong? Thanks for any help :)

 

Public Function GetControlValues(s as Object, SqlStr As String) 
  
 Dim ctl As Control
 Dim ReturnValue As String
 Dim SqlStringProp As String
 Dim StringBuild As New Stringbuilder
 
 StringBuild.Append(SqlStr)
 response.write("<b>Stringbuilder BEFORE:</b> " & StringBuild.ToString())  <<--- Stringbuilder has a value here
 
 For each ctl in s.Controls
 
   response.write("<b>Stringbuilder DURING:</b> " & StringBuild.ToString()) <<-- But is empty here *only* at the begining of a new panel
   
   ReturnValue=ShowControlDetails(ctl)
 
   If Len(ReturnValue) > 0 Then
    
    StringBuild.Append(" '")
    StringBuild.Append(ReturnValue)
    StringBuild.Append("'") 
   
   Else
   
    response.write("<b>ReturnValue:</b> No value returned")
   
   End If
   
   response.write("<b>Stringbuilder AFTER:</b> " & StringBuild.ToString())  
 
   GetControlValues(ctl, SqlStringProp)
  
 Next

End Function

 

Public Function ShowControlDetails(ctlControl As Object)

   Dim SqlString As String
   
   Select Case ctlControl.GetType().ToString()
     
     Case "System.Web.UI.WebControls.TextBox"
     SqlString=ctlControl.Text.ToString()
     
     Case "System.Web.UI.WebControls.DropDownList"
     SqlString=ctlControl.SelectedItem.Text.ToString()
          
     Case Else
     response.write("<b>This control isn't relevant?</b>")
   End Select
   
   return SqlString

End Function




Replies:
Posted By: Diep-Vriezer
Date Posted: 01 September 2003 at 8:15am

You are declaring the strings within a sub. Why don't you try and declare all the strings with Public?

Public SqlString As String, ctl As Control, and so on.

Maybe this would work, but it seems like a relative easy problem to me, and I'm quite a n00b.



-------------
Gone..


Posted By: jamesb
Date Posted: 01 September 2003 at 10:04am

*doh*

yes - was staring me in the face all along - thanks for pointing it out

 

 



Posted By: Diep-Vriezer
Date Posted: 01 September 2003 at 2:30pm
lol

-------------
Gone..



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net