我有超过100个asp.net按钮,我需要直接从数据库设置BackColor和Text,如果我能弄清楚这一点,我可以替换400+行Select语句大约有10行代码。下面的转换方法都不起作用,如果他们这样做,我可以用If子句中的替换为c
。如何投射一个字符串到WebControl.Button
Sub Page_Load(sender As Object, e As EventArgs)
Dim taR As New DsReadyTableAdapters.taReady
Dim dtR As New DsReady.ReadyDataTable
taR.Fill(dtR)
Dim x As Integer = 1
Dim ss As String
Dim t As Type
For Each b In form1.Controls
t = b.GetType
If t.Name = "Button" Then
Dim c As System.Web.UI.WebControls.Button = CType(b.ID, System.Web.UI.WebControls.Button) 'tried both, doesn't work
Dim c As System.Web.UI.WebControls.Button = DirectCast(b.ID, System.Web.UI.WebControls.Button) 'tried both, doesn't work
ss = CStr(b.ID).Substring(6)
If ss > 122 And ss < 126 Then
b.ID.BackColor = Drawing.ColorTranslator.FromHtml(dtR(x).Color) 'b.ID is string so doesn't work
b.ID.Text = dtR(x).Num
x += 1
End If
End If
Next b
End Sub
我一直在使用一个Controls.OfType
子句中For Each
语句来缩小结果只是按键也试过,但无济于事。我也愿意用jQuery来做到这一点,但还没有找到一种简单的从数据库中提取数据的方法,就像我使用的一样简单。非常感谢您的帮助。
您究竟在哪里可以在代码中使用For Each语句?我试图迭代所有的按钮,所以我不明白我可以如何使用'your_button_name'。谢谢 – NitroMuse
“尝试”不足以说明答案。纯代码解决方案被认为是质量差的内容,并可能导致您的答案被删除。 –
把它放在你的循环中,your_button_name = b.ID – Jack