?
Solved

Dynamic button onclick to run code

Posted on 2007-12-03
3
Medium Priority
?
849 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

752 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