PDA

Click to See Complete Forum and Search --> : Calling a VB.NET function (code behind) from a JavaScript function.


Wokawidget
Aug 25th, 2005, 04:44 AM
I have a popup window, which returns some values to a javascript function in the parent calling web page.
My JScript function is:

function SelectedVisitors(VisitorKeys)
{
alert(VisitorKeys)
}

Where visitorkeys is like "34,6,234,1"

Basically I need to call a vb.net function is code behind to save these values to the DB, and then refresh the grid on that page too.

Woka

EricDalnas
Aug 25th, 2005, 11:00 AM
perhaps copying the data to a hidden control and raising a postback would work
http://weblogs.asp.net/mnolton/archive/2003/06/04/8260.aspx

mendhak
Aug 26th, 2005, 12:32 AM
perhaps copying the data to a hidden control and raising a postback would work
http://weblogs.asp.net/mnolton/archive/2003/06/04/8260.aspx
That's just what I was about to suggest. :)

Wokawidget
Aug 26th, 2005, 03:23 AM
Cheers for the replies.
Right...OK. Bit of a cheap hack/frig don't you think?
How about...
Code to create JS:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
doPostBack(Me)
End Sub

Private Sub doPostBack(ByVal oPage As Page)

If oPage.IsClientScriptBlockRegistered("__doPostBack") Then Return

oPage.RegisterClientScriptBlock("__doPostBack", GetPostBackScript)

oPage.RegisterHiddenField("__EVENTTARGET", "")
oPage.RegisterHiddenField("__EVENTARGUMENT", "")

End Sub

Private Function GetPostBackScript() As String

Dim Java As New StringBuilder(1024)

With Java
.Append("function __doPostBack(eventTarget, eventArgument) " + nl)
.Append("{ " + nl)
.Append(" var theform; " + nl)
.Append(" " + nl)
.Append(" if (window.navigator.appName.toLowerCase().indexOf('microsoft') > -1) " + nl)
.Append(" { " + nl)
.Append(" theform = document.Form1; " + nl)
.Append(" } " + nl)
.Append(" else " + nl)
.Append(" { " + nl)
.Append(" theform = document.forms['Form1']; " + nl)
.Append(" } " + nl)
.Append(" " + nl)
.Append(" theform.__EVENTTARGET.value = eventTarget.split('$').join(':'); " + nl)
.Append(" theform.__EVENTARGUMENT.value = eventArgument;theform.submit(); " + nl)
End With

Return Java.ToString

End Function

Now you need a hidden control in your page to handle the postback and store some data:

<input type="hidden" id="VisitorKeys" value="" runat=server NAME="VisitorKeys">

And now the JS function that is called from a button click event or something:

function SelectedVisitors(VisitorKeyValues)
{
document.forms[0].VisitorKeys.value=VisitorKeyValues
__doPostBack(document.forms[0].VisitorKeys.value, '')
}

We now need a function in the code behind to handle this postback.
because we are using a hidden input control we can use an event of this:

Private Sub VisitorKeys_ServerChange(ByVal sender As Object, ByVal e As System.EventArgs) Handles VisitorKeys.ServerChange
Dim Keys As String = VisitorKeys.Value
'Handle code here
End Sub


make sense?

Woka