NitroMuse NitroMuse - 1 year ago 73
jQuery Question

How to cast a string to a WebControl.Button

I have over 100 buttons that I need to set the BackColor and Text directly from a database, if I can figure this out I can replace a 400+ line Select statement with about 10 lines of code. Neither of the below conversion methods work, if they did I could replace the

with the var
in the If clause.

Sub Page_Load(sender As Object, e As EventArgs)
Dim taR As New DsReadyTableAdapters.taReady
Dim dtR As New DsReady.ReadyDataTable
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

I have also tried using a
clause in the
For Each
statement to narrow the results to just the buttons, but to no avail. I am willing to do this with jQuery also but have not found a straightforward method of pulling the data from the database that is as easy as what I am using. Your help is greatly appreciated.

Answer Source

Actually I found that jQuery offers a straightforward easy way to accomplish this, iterating through HTML elements, buttons in this case. Add a class name to the buttons you're trying to find. Thanks for your help.

function dealer() {  // for Deal
        var i = 0;
        $('.deal').each(function(){ //need class="deal"
            var chit = deal[i];
            if (chit.length == 2) {
                var v = chit.slice(0, 1);
                var c = chit.slice(1);
            } else { // 3
                var v = chit.slice(0, 2);
                var c = chit.slice(2);
            (this.value) = v; // Number
            switch (c) {
                case "r":
                    ( = "red";  // Color
                case "b":
                    ( = "lightblue";
                case "g":
                    ( = "gray";
                case "o":
                    ( = "orange";
            } // switch
        }); //deal
    }; // dealer
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download