问题:我想更改一些固定命名按钮上的文本。
当我运行例程时,我可以看到vButtonName
是正确的但是Dim Button As Button没有获取值?!
我先放了.aspx.vb代码然后是.aspx按钮代码
如果我对按钮名称进行硬编码,则文本会发生变化。
.aspx.vb代码:
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代码:
<asp:Button ID="btnRiskRatingFilterAText" runat="server" Text="Class A" CssClass="ButtonTextual" OnCommand="btnRiskRating_Click"/>
答案 0 :(得分:1)
好的,所以我不得不更改FindControl以找到主控件,然后是内容持有者:
Dim按钮As Button = Master.FindControl(&#34; ContentPlaceHolder1&#34;)。FindControl(vButtonName)
编辑:我还根据评论更新了代码的布局,以改善连接的开启/关闭
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