473,395 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
1 5032
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
1
by: cheezebeetle | last post by:
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...
7
by: Ubantu Rococo | last post by:
Hi all, Sorry for this stupid question, but I am having trouble mixing imagecopy etc. with HTML. What I am trying to do is copy an image, and then obtain co-ordinates from a database which will...
6
by: D | last post by:
Hello all...I have an issue with one of my java script functions that I'm hoping someone can easily help with. I have a web based application that we use to create/sign up for overtime. When we...
11
by: yangsuli | last post by:
i want to creat a link when somebody click the link the php script calls a function,then display itself :) i have tried <a href=<? funtion(); echo=$_server ?>text</a> but it will call the...
3
pbmods
by: pbmods | last post by:
AN INTRODUCTION TO FUNCTION OBJECTS LEVEL: INTERMEDIATE PREREQS: OBJECTS You've seen it before. You're setting up an XMLHttpRequest call, and you need to execute a function when it returns, so...
7
by: =?Utf-8?B?am9uZWZlcg==?= | last post by:
I'm hoping this is a classic question, but the answer to it would help me with a lot of things. As you can see below, this code will never display the "Getting data...." text in the lblCheck -...
3
by: Jimmy | last post by:
It is also possible for popup window to call function in main window by using the opener property. Will "opener.someFunctionInMain(param1, param2)" in the popup window work? It's possible for...
10
by: evicailieva | last post by:
A have a php scrip where I call a JavaScript function. I don't know why, but it doesn't work. At the beginning, when I was writing the script it was working but now it's not. I don't know wхat to do....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.