savache27
asked on
Dynamic button onclick to run code
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("Contac t")), ",")
'Open DB connection (I didnt do this)
Try
Myconn = New SqlConnection(Configuratio nSettings. AppSetting s("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!
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()"
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("Contac
'Open DB connection (I didnt do this)
Try
Myconn = New SqlConnection(Configuratio
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
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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
ASKER