Solved

Dynamic button onclick to run code

Posted on 2007-12-03
3
844 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

786 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