By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,358 Members | 2,811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,358 IT Pros & Developers. It's quick & easy.

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

P: n/a
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("<SC RIPT 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?!

Jul 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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" <an*******@discussions.microsoft.com> wrote in message
news:07**********************************@microsof t.com...
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("<SC RIPT 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?!

Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.