Partial Class Default_aspx
Implements ICallbackEventHandler
Public callbackStr As String
Public Function RaiseCallbackEvent(ByVal eventArgument As String) As _
String Implements _
System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
If eventArgument.StartsWith("1:") Then
"---strips away the command
eventArgument = eventArgument.Substring(2)
"---get city and state based on Zipcode
Select Case eventArgument
Case "95472" : Return "Sebastopol,CA"
Case "02140" : Return "Cambridge,MA"
Case Else
Return "ZipCode not valid."
End Select
ElseIf eventArgument.StartsWith("2:") Then
"---strips away the command
eventArgument = eventArgument.Substring(2)
"---get states and cities related to country
Select Case eventArgument
Case "Sing" : Return "Singapore,"
Case "US" : Return _
"Alabama,California,Maryland,Massachusetts,New York,Oklahoma,Wisconsin,"
Case "UK" : Return _
"Birmingham,Cambridge,Christchurch,Leeds,Sheffield,"
Case Else
Return ""
End Select
Else
Return "Command not recognized"
End If
End Function
1:02140
2:US
Alabama,California,Maryland,Massachusetts,New York,Oklahoma,Wisconsin,
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Load
ddlCountry.Attributes.Add("onChange", "GetStatesFromServer()")
callbackStr = Page.GetCallbackEventReference(Me, "Command", _
"CallBackHandler", "context", "onError")
End Sub
WebForm_DoCallback("__Page",Command,CallBackHandler,context,onError)
...
mmlt;/headmmgt;
mmlt;bodymmgt;
mmlt;scriptmmgt;
function GetStateFromZip(){
var Command = "1:" + document.forms[0].elements["txtZipCode"].value;
var context = new Object();
context.CommandName = "GetStateFromZip";
mmlt;%=callbackStr%mmgt;
}
function GetStatesFromServer() {
var Command = "2:" + document.forms[0].elements["ddlCountry"].value;
var context = new Object();
context.CommandName = "GetStatesFromCountry";
mmlt;%=callbackStr%mmgt;
}
function CallBackHandler(result, context) {
if (context.CommandName == "GetStateFromZip" ) {
var indexofComma = result.indexOf(",");
var City = result.substring(0,indexofComma);
var State = result.substring(indexofComma+1,result.length);
document.forms[0].elements["txtState"].value = State;
document.forms[0].elements["txtCity"].value = City;
} else
if (context.CommandName == "GetStatesFromCountry")
{
document.forms[0].elements["ddlState"].options.length=0;
while (result.length>0) {
var indexofComma = result.indexOf(",");
var State = result.substring(0,indexofComma);
result = result.substring(indexofComma+1)
opt = new Option(State,State);
document.forms[0].elements["ddlState"].add(opt);
}
}
}
function onError(message, context) {
alert("Exception :\n" + message);
}
mmlt;/scriptmmgt;
mmlt;form id="form1" runat="server"mmgt;
...
function GetStateFromZip(){
var Command = "1:" + document.forms[0].elements["txtZipCode"].value;
var context = new Object();
context.CommandName = "GetStateFromZip";
WebForm_DoCallback("__Page",Command,CallBackHandler,context,onError)
}
function GetStatesFromServer(){
var Command = "2:" + document.forms[0].elements["ddlCountry"].value;
var context = new Object();
context.CommandName = "GetStatesFromCountry";
WebForm_DoCallback("__Page",Command,CallBackHandler,context,onError)
}
mmlt;input id="Button1" type="button" value="Get City and State"
OnClick="GetStateFromZip()"
style="width: 144px; height: 24px"/mmgt;
mmlt;asp:DropDownList ID="ddlCountry" Runat="Server" mmgt;
mmlt;asp:ListItemmmgt;Select Countrymmlt;/asp:ListItemmmgt;
mmlt;asp:ListItem Value="US"mmgt;United Statesmmlt;/asp:ListItemmmgt;
mmlt;asp:ListItem Value="Sing"mmgt;Singaporemmlt;/asp:ListItemmmgt;
mmlt;asp:ListItem Value="UK"mmgt;United Kingdommmlt;/asp:ListItemmmgt;
mmlt;/asp:DropDownListmmgt;
mmlt;asp:DropDownList ID="ddlState" Runat="server"mmgt;
mmlt;/asp:DropDownListmmgt;
ddlCountry.Attributes.Add("onChange", "GetStatesFromServer()")
이전 글 : C#, using/foreach와 try/catch 확장
다음 글 : 프로그램의 설계시에 알아야 할 좋은 코딩 습관
최신 콘텐츠