Solved

Dynamic button onclick to run code

Posted on 2007-12-03
3
847 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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…
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

690 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