Dynamically changing the Button initial

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rag84dec
    New Member
    • Mar 2007
    • 100

    #1

    Dynamically changing the Button initial

    Hi,
    I have an asp code which creates a grid or matrics (5 x 5 matrics).Each cell contains a button which will have an initial
    .The initial will be read from a database.

    say the database has these entries...table name is "TestSuite"
    ---------- ------- --------
    | Initial | Name |
    ---------- ------- --------
    | S | TestSuite1 |
    -------------------------------
    | R | Suite2
    -------------------------------
    |G | Suite3
    -------------------------------
    | T | Suite4
    -------------------------------


    Initially the button should contain "S" or the first entry of the database and on clicking the button it should change to the next initial say "R".

    On clicking the button it should keep changing to the next entry till the last and again from first.


    How can i do??...With javascript function??....E arlier i had a combo box which had all the initial in it.User had an option to select a particular "Initial'....Ca n any one please help me in having a dynamic button??....
  • CroCrew
    Recognized Expert Contributor
    • Jan 2008
    • 564

    #2
    Hello rag84dec,

    Looking at your question, if it were me I would do it with client side scripting so I would not have to refresh the page on every click of the button. Here is an example that you could use. The code is using Microsoft Access because you did not specify what database you are using. Let me know if I need to explain in detail what is going on in the page, I will be happy to do so.

    Hope this helps~

    [code=asp]
    <%
    Set Conn = Server.CreateOb ject("ADODB.Con nection")
    Conn.Open "DRIVER={Micros oft Access Driver (*.mdb)}; DBQ=" & Server.MapPath( "Relative path to your Access database")

    Set MyRS = Server.CreateOb ject("ADODB.Rec ordset")
    SQL = "SELECT * FROM TheTable"
    MyRS.CursorType = 1
    MyRS.LockType = 3
    MyRS.Open SQL, Conn

    Do Until (MyRS.EOF)
    i=(i+1)
    If (MyRS.RecordCou nt => (i+1)) Then
    TempButtonArray = TempButtonArray & "'" & MyRS("FieldOne" ) & "', "
    Else
    TempButtonArray = TempButtonArray & "'" & MyRS("FieldOne" ) & "'"
    End If
    MyRS.MoveNext
    Loop

    MyRS.close
    Conn.close
    %>
    <html>
    <head>
    <title>Exampl e</title>
    <script language="JavaS cript">
    var dbButtonArray = new Array(<%=TempBu ttonArray%>);
    var CurrentButtonVa lue = 0;
    function ChangeButton()
    {
    if (CurrentButtonV alue == (dbButtonArray. length-1))
    {
    CurrentButtonVa lue = 0;
    }
    else
    {
    CurrentButtonVa lue = (CurrentButtonV alue + 1);
    }
    document.xform. xButton.value=d bButtonArray[CurrentButtonVa lue];
    }
    </script>
    </head>
    <body>
    <form method="post" action="Post_Up date.asp" name="xform">
    <input type="button" name="xButton" value="Click Me" onclick="Change Button();">
    </form>
    </body>
    <script language="JavaS cript">document .xform.xButton. value=dbButtonA rray[0];</script>
    </html>
    [/code]

    Comment

    Working...