Solved

Dynamic button onclick to run code

Posted on 2007-12-03
3
842 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
Comment Utility
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
Comment Utility
Thank you for the explanation. I've decided not to build the button dynamically.
0
 
LVL 37

Expert Comment

by:samtran0331
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now