Reputation: 1024
Issue: I want to change the text on some fixed named buttons.
When I run the routine I can see that vButtonName
is correct however Dim Button As Button does not pick up the value?!
I have put first the .aspx.vb code then the .aspx button code
The text changes if I hardcode the button name.
.aspx.vb Code:
Dim ConnectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("SQL_DEMO").ToString()
For i As Integer = 0 To arr.Count - 1
Using connection = New SqlConnection(ConnectionString)
Using command As New SqlCommand("SELECT COUNT(OverallRiskCategory) FROM TblAsbestos WHERE OverallRiskCategory = @Category", connection)
' Open your connection '
connection.Open()
' Add your parameter '
command.Parameters.AddWithValue("@Category", arr(i).ToString())
' Execute your query '
Dim result = command.ExecuteScalar()
Dim vButtonName As String = "btnRiskRatingFilter" & arr(i).ToString() & "Text"
Dim button As Button = FindControl(vButtonName)
If Not button Is Nothing Then
button.Text = String.Format("Class " & arr(i).ToString & " (" & result & ")")
End If
connection.Close()
End Using
End Using
Next
.aspx code:
<asp:Button ID="btnRiskRatingFilterAText" runat="server" Text="Class A" CssClass="ButtonTextual" OnCommand="btnRiskRating_Click"/>
Upvotes: 0
Views: 781
Reputation: 1024
Okay so I had to change the FindControl to find the master control then the contentholder:
Dim button As Button = Master.FindControl("ContentPlaceHolder1").FindControl(vButtonName)
Edit: I've also updated the layout of the code as per comments to improve the connections opening/closing
Dim arr As New ArrayList
arr.Add("A")
arr.Add("B")
arr.Add("C")
arr.Add("D")
arr.Add("NA")
arr.Add("UN")
' Try
Dim ConnectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("SQL_DEMO").ToString()
Using connection = New SqlConnection(ConnectionString)
' Open your connection '
connection.Open()
For i As Integer = 0 To arr.Count - 1
Using command As New SqlCommand("SELECT COUNT(OverallRiskCategory) FROM TblAsbestos WHERE OverallRiskCategory = @Category", connection)
' Add your parameter '
command.Parameters.AddWithValue("@Category", arr(i).ToString())
' Execute your query '
Dim result = command.ExecuteScalar()
Dim button As Button = Master.FindControl("ContentPlaceHolder1").FindControl("btnRiskRatingFilter" & arr(i).ToString() & "Text")
If Not button Is Nothing Then
button.Text = String.Format("Class " & arr(i).ToString & " (" & result & ")")
End If
End Using
Next
connection.Close()
End Using
'Catch ex As Exception
'btnRiskRatingFilterAText.Text = "Unable to load"
' End Try
Upvotes: 1