abhi81
Programmer
- Jan 13, 2007
- 18
Hi All,
I was trying to implement the callback functionality to dynamically populate a second dropdown based on the value of the first 1 .....a very simple task but all my tries failed.
I have the first dropdown populated from a table for countries and second dropdown again populated by a table for states in that country. I have them both populated during the page load based on the default values.
Now i want the state dropdown to reload with new values from the table based on the country when the country is changed through a callback rather than a postback. I tried to manipulate the msdn code but wasn;t successfull.
Note:
->i am trying to implement it on page based on a master page.
->am using vb.net in vs2005
Also before trying this i used the sample code provided by microsoft ( ) and tested it on a new page and it worked fine as described however when i copied the same code over to a page which was based on a master page it was giving me a javascript error saying "ReceiveServerData is undefined"(javascript function to receive the result back from server).
Can some one please provide me with proper guidance/code to get this accomplished(even on a page based on a master page)? Am trying to implement callback for the first time.
I am also putting the case that i tried:
Default2.aspx
<%@ Page Language="VB" MasterPageFile="~/Administrator/SuperAdmin.master" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Administrator_Default2" title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script type="text/javascript">
function LookUpStock()
{
var lb = document.getElementById("ListBox1");
var product = lb.options[lb.selectedIndex].text;
CallServer(product, "");
}
function ReceiveServerData(rValue)
{
document.getElementById("ResultsSpan").innerHTML = rValue;
}
</script>
<div>
<asp:ListBox ID="ListBox1" Runat="server"></asp:ListBox>
<br />
<br />
<button type="Button" onclick="LookUpStock()">Look Up Stock</button>
<br />
<br />
Items in stock: <span id="ResultsSpan" runat="server"></span>
<br />
</div>
</asp:Content>
Default2.aspx.vb
Partial Class Administrator_Default2
Inherits System.Web.UI.Page
Implements System.Web.UI.ICallbackEventHandler
Protected catalog As ListDictionary
Protected returnValue As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim cbReference As String
cbReference = Page.ClientScript.GetCallbackEventReference(Me, _
"arg", "ReceiveServerData", "context")
Dim callbackScript As String = ""
callbackScript &= "function CallServer(arg, context) { " & _
cbReference & "} ;"
Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), _
"CallServer", callbackScript, True)
' Populate List Dictionary with invented database data
catalog = New ListDictionary()
catalog.Add("monitor", 12)
catalog.Add("laptop", 10)
catalog.Add("keyboard", 23)
catalog.Add("mouse", 17)
ListBox1.DataSource = catalog
ListBox1.DataTextField = "key"
ListBox1.DataBind()
End Sub
Public Sub RaiseCallbackEvent(ByVal eventArgument As String) _
Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
If catalog(eventArgument) Is Nothing Then
returnValue = "-1"
Else
returnValue = catalog(eventArgument).ToString()
End If
End Sub
Public Function GetCallbackResult() _As String Implements _
System.Web.UI.ICallbackEventHandler.GetCallbackResult
Return returnValue
End Function
End Class
The exact same code runs fine if i implement it on a standalone page. But since this is based on a master page may be there is some other way to do it that i am not aware of.
I was trying to implement the callback functionality to dynamically populate a second dropdown based on the value of the first 1 .....a very simple task but all my tries failed.
I have the first dropdown populated from a table for countries and second dropdown again populated by a table for states in that country. I have them both populated during the page load based on the default values.
Now i want the state dropdown to reload with new values from the table based on the country when the country is changed through a callback rather than a postback. I tried to manipulate the msdn code but wasn;t successfull.
Note:
->i am trying to implement it on page based on a master page.
->am using vb.net in vs2005
Also before trying this i used the sample code provided by microsoft ( ) and tested it on a new page and it worked fine as described however when i copied the same code over to a page which was based on a master page it was giving me a javascript error saying "ReceiveServerData is undefined"(javascript function to receive the result back from server).
Can some one please provide me with proper guidance/code to get this accomplished(even on a page based on a master page)? Am trying to implement callback for the first time.
I am also putting the case that i tried:
Default2.aspx
<%@ Page Language="VB" MasterPageFile="~/Administrator/SuperAdmin.master" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Administrator_Default2" title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script type="text/javascript">
function LookUpStock()
{
var lb = document.getElementById("ListBox1");
var product = lb.options[lb.selectedIndex].text;
CallServer(product, "");
}
function ReceiveServerData(rValue)
{
document.getElementById("ResultsSpan").innerHTML = rValue;
}
</script>
<div>
<asp:ListBox ID="ListBox1" Runat="server"></asp:ListBox>
<br />
<br />
<button type="Button" onclick="LookUpStock()">Look Up Stock</button>
<br />
<br />
Items in stock: <span id="ResultsSpan" runat="server"></span>
<br />
</div>
</asp:Content>
Default2.aspx.vb
Partial Class Administrator_Default2
Inherits System.Web.UI.Page
Implements System.Web.UI.ICallbackEventHandler
Protected catalog As ListDictionary
Protected returnValue As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim cbReference As String
cbReference = Page.ClientScript.GetCallbackEventReference(Me, _
"arg", "ReceiveServerData", "context")
Dim callbackScript As String = ""
callbackScript &= "function CallServer(arg, context) { " & _
cbReference & "} ;"
Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), _
"CallServer", callbackScript, True)
' Populate List Dictionary with invented database data
catalog = New ListDictionary()
catalog.Add("monitor", 12)
catalog.Add("laptop", 10)
catalog.Add("keyboard", 23)
catalog.Add("mouse", 17)
ListBox1.DataSource = catalog
ListBox1.DataTextField = "key"
ListBox1.DataBind()
End Sub
Public Sub RaiseCallbackEvent(ByVal eventArgument As String) _
Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
If catalog(eventArgument) Is Nothing Then
returnValue = "-1"
Else
returnValue = catalog(eventArgument).ToString()
End If
End Sub
Public Function GetCallbackResult() _As String Implements _
System.Web.UI.ICallbackEventHandler.GetCallbackResult
Return returnValue
End Function
End Class
The exact same code runs fine if i implement it on a standalone page. But since this is based on a master page may be there is some other way to do it that i am not aware of.