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