Mixing .NET function into Javascript call doesn't work right... Help

G

Guest

ok, so I am having problems passing in an ASPX function into the Javascript in the codebehind page. I am simply using a confirm call which when they press "OK" they call this ASPX function, when they press "Cancel" they call another ASPX function.

My code now is:

System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">" & vbCrLf)
System.Web.HttpContext.Current.Response.Write("if (confirm('Are you sure you want to delete the user " & UserID & "')) " & DeleteUser(UserID) & "; else " & list_users(ddSites.SelectedValue) & ";")
System.Web.HttpContext.Current.Response.Write("</SCRIPT>")

which also works as:

Dim strScript As String
strScript = "<script>"
strScript = strScript & "if (confirm('Are you sure you want to delete the user')) " & DeleteUser(UserID) & "; else " & list_users(ddSites.SelectedValue) & ";"
strScript = strScript & "</script>"
RegisterClientScriptBlock("ClientScript", strScript)

It works with one pretty important problem. No matter which order I put the DeleteUser function (on Ok or Cancel button)
it still goes to it and Deletes the user. Obviously, this isn't supposed to happen. If they choose the Cancel button, they should just reload the userlist, without the delete taking place.

If I completely remove the DeleteUser function from the call and make both list_user like this:

strScript = strScript & "if (confirm('Are you sure you want to delete the user')) " & list_users(ddSites.SelectedValue) & "; else " & list_users(ddSites.SelectedValue) & ";"


Then it ignors the Delete function, like it should and just reloads the datagrid. Then of course, the problem is... it never allows you to delete.


By the way... using {} in my Javascript call doesn't seem to matter....

Help?!
 
M

Munsifali Rashid

Your question is not very clear. You cannot call a server-side function
from client-side javascript code. By the looks of it, you are calling the
server-side DeleteUser function as you construct the string, which is why
the user gets deleted irrespective of what is chosen in the javascript
alert.

It appears that you have a sites dropdownbox, users dropdownbox, and delete
button. Populate your boxes in the page_load subroutine. Set your sites
dropdown autopostback property to true, so that you populate the users
dropdown whenever the sites dropdownbox is changed (the selected item is
changed). Attach the client side check to the onclick handler of the
button.


Private Sub PageLoad(sender As Object, e As System.EventArgs) Handles
Page.Load

If (Not Page.IsPostBack) Then

'Populate sites dropdown here
'...
End If

'Add onclick handler to delete button
btnDelete.Attributes.Add("onClick", "return confirm('Are you sure you
want to delete this user?')")

End If


Private Sub ddSites_SelectedIndexChanged(sender As Object, e As
System.EventArgs) Handles ddSites.SelectedIndexChanged

'Populate users dropdown here

End Sub


Private Sub btnDelete_Click(sender As Object, e As System.EventArgs) Handles
btnDelete.Click

'Get user ID
Dim userID As Int = Convert.ToInt32(ddUsers.SelectedItem.Value)

'Delete user
'...

End Sub


The code above hasn't been checked, so there's probably some bugs in it, but
it should give you an idea of an alternative approach to the problem.

Hope this helps,

Mun

--
Munsifali Rashid
http://www.munsplace.com/




cheezebeetle said:
ok, so I am having problems passing in an ASPX function into the
Javascript in the codebehind page. I am simply using a confirm call which
when they press "OK" they call this ASPX function, when they press "Cancel"
they call another ASPX function.
My code now is:

System.Web.HttpContext.Current.Response.Write("<SCRIPT
LANGUAGE=""JavaScript"">" & vbCrLf)
System.Web.HttpContext.Current.Response.Write("if (confirm('Are you sure
you want to delete the user " & UserID & "')) " & DeleteUser(UserID) & ";
else " & list_users(ddSites.SelectedValue) & ";")
System.Web.HttpContext.Current.Response.Write("</SCRIPT>")

which also works as:

Dim strScript As String
strScript = "<script>"
strScript = strScript & "if (confirm('Are you sure you want to delete the
user')) " & DeleteUser(UserID) & "; else " &
list_users(ddSites.SelectedValue) & ";"
strScript = strScript & "</script>"
RegisterClientScriptBlock("ClientScript", strScript)

It works with one pretty important problem. No matter which order I put
the DeleteUser function (on Ok or Cancel button)
it still goes to it and Deletes the user. Obviously, this isn't supposed
to happen. If they choose the Cancel button, they should just reload the
userlist, without the delete taking place.
If I completely remove the DeleteUser function from the call and make both list_user like this:

strScript = strScript & "if (confirm('Are you sure you want to delete the
user')) " & list_users(ddSites.SelectedValue) & "; else " &
list_users(ddSites.SelectedValue) & ";"
Then it ignors the Delete function, like it should and just reloads the
datagrid. Then of course, the problem is... it never allows you to delete.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top