Solved

Dynamic button onclick to run code

Posted on 2007-12-03
3
845 Views
Last Modified: 2010-08-05
Hi,

I'm working in VS using vb.net with 1.1 framework. I am dynamically creating a button, but I cannot get the button to run the Function I need it to run. This is the code I am using to create the button in my code behind page:
  vHTML = vHTML & "<tr>"
                vHTML = vHTML & "<td colspan='7' align='center'>"
                vHTML = vHTML & "<input type='Submit' name ='Submit' onClick=""MarkContacted()"" value='Mark as Contacted'>"
                vHTML = vHTML & "</td>"
                vHTML = vHTML & "</tr>"
                vHTML = vHTML & "</table>"

This is the code for the function I need it to run:

 Private Function MarkContacted()
        Dim Myconn As SqlConnection
        Dim Mycomm As SqlCommand
        Dim sql As String
        Dim Success As Boolean
        Dim vDate As String
        vDate = Request("frmContactDate")    

        Dim i
        Dim vIdArray
        'Make sure something is in the collection
        If Request("Contact") = "" Then
            Response.Write("Nothing was checked!<br>")
        Else
            'Split it by comma
            vIdArray = Split(Trim(Request("Contact")), ",")
            'Open DB connection (I didnt do this)
            Try
                Myconn = New SqlConnection(ConfigurationSettings.AppSettings("connstr"))
                Myconn.Open()
                'Loop through the collection
                For i = 0 To UBound(vIdArray)
                    'build sql string
                    sql = "UPDATE tbl_eeocm_data SET [LastDateOfContact] = '" & Request("frmContactDate") & "' WHERE ID = " & vIdArray(i)
                    Mycomm = New SqlCommand(sql, Myconn)
                    Mycomm.ExecuteNonQuery()
                    Success = True
                    'Response.Write("update where id = " & vIdArray(i) & "<br>")
                    Response.Write(sql)
                    'Update record here (I didnt do this)
                Next
                'close the connection (I didnt do this)
                Myconn.Close()
            Catch Ex As Exception
                Success = False
                Response.Write("<br>" & sql & "<br><br>")
                Response.Write("<p><strong>An Error Occurred:</strong> " & Ex.ToString() & "</p>" & vbCrLf)
                Response.Write(sql & "<br>")
            Finally
                Myconn.Close()
            End Try
        End If
        ' ===============================================================
    End Function


Can anyone tell me what I am doing wrong?? I would really appreciate the help. Thanks!
0
Comment
Question by:savache27
  • 2
3 Comments
 
LVL 37

Accepted Solution

by:
samtran0331 earned 500 total points
ID: 20396195
when you do this:
vHTML = vHTML & "<input type='Submit' name ='Submit' onClick=""MarkContacted()"" value='Mark as Contacted'>"


the onclick event is a client-side javascript, so the rendered html thinks that MarkContacted is javascript not server code.

Ideally, in ASP.Net, you shouldn't have to build an html table as a string...what's the "bigger picture" here?
0
 

Author Comment

by:savache27
ID: 20396900
Thank you for the explanation. I've decided not to build the button dynamically.
0
 
LVL 37

Expert Comment

by:samtran0331
ID: 20396935
Just keep in mind that you can add buttons dynamically, but to make life easier, they should be "<asp:button..>" controls and not html <input tags
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question