Partager via


ASP.NET V2 and Client Callbacks

 

Callback.ASPX page to invoke the client callback

<%@ Page MasterPageFile="~/Site.master" Language="C#" CompileWith="Callback.aspx.cs" ClassName="Callback_aspx" Title="Client Callback Manager" %>
<%@ Implements Interface="System.Web.UI.ICallbackEventHandler" %>

<asp:Content ID="TitleContent" ContentPlaceHolderID="Title" Runat="server">
Client Callback Manager</asp:Content>

<asp:Content ID="DescriptionContent" ContentPlaceHolderID="Description" Runat="server">
This page demonstrates how to use ASP.NET's Client Callback Manager to perform lightweight callbacks to a Web server</asp:Content>

<asp:Content ID="MainContent" ContentPlaceHolderID="Main" Runat="server">
<table><tr>
<td style="width: 100px; height: 10px">
Name</td>
<td style="width: 100px; height: 10px">
<asp:TextBox ID="Name" Runat="server"></asp:TextBox>
</td>
<td style="width: 100px; height: 10px">
</td>
</tr>
<tr>
<td style="width: 100px">
Address</td>
<td style="width: 100px">
<asp:TextBox ID="Address" Runat="server"></asp:TextBox>
</td>
<td style="width: 100px">
</td>
</tr>
<tr>
<td style="width: 100px">
City</td>
<td style="width: 100px">
<asp:TextBox ID="City" Runat="server"></asp:TextBox>
</td>
<td style="width: 100px">
</td>
</tr>
<tr>
<td style="width: 100px">
State</td>
<td style="width: 100px">
<asp:DropDownList ID="State" Runat="server">
<asp:ListItem Text="AL" />
<asp:ListItem Text="AK" />
<asp:ListItem Text="VIC" />
<asp:ListItem Text="NS" />
<asp:ListItem Text="SA" />
<asp:ListItem Text="CO" />
<asp:ListItem Text="CT" />
<asp:ListItem Text="DC" />
<asp:ListItem Text="DE" />
<asp:ListItem Text="FL" />
<asp:ListItem Text="GA" />
<asp:ListItem Text="HI" />
<asp:ListItem Text="IA" />
<asp:ListItem Text="ID" />
<asp:ListItem Text="IL" />
<asp:ListItem Text="IN" />
<asp:ListItem Text="KS" />
<asp:ListItem Text="KY" />
<asp:ListItem Text="LA" />
<asp:ListItem Text="MA" />
<asp:ListItem Text="MD" />
<asp:ListItem Text="ME" />
<asp:ListItem Text="MI" />
<asp:ListItem Text="MN" />
<asp:ListItem Text="MO" />
<asp:ListItem Text="MS" />
<asp:ListItem Text="MT" />
<asp:ListItem Text="NC" />
<asp:ListItem Text="ND" />
<asp:ListItem Text="NE" />
<asp:ListItem Text="NH" />
<asp:ListItem Text="NJ" />
<asp:ListItem Text="NM" />
<asp:ListItem Text="NV" />
<asp:ListItem Text="NY" />
<asp:ListItem Text="OH" />
<asp:ListItem Text="OK" />
<asp:ListItem Text="OR" />
<asp:ListItem Text="PA" />
<asp:ListItem Text="RI" />
<asp:ListItem Text="SC" />
<asp:ListItem Text="SD" />
<asp:ListItem Text="TN" />
<asp:ListItem Text="TX" />
<asp:ListItem Text="UT" />
<asp:ListItem Text="VA" />
<asp:ListItem Text="VT" />
<asp:ListItem Text="WA" />
<asp:ListItem Text="WI" />
<asp:ListItem Text="WV" />
<asp:ListItem Text="WY" />
</asp:DropDownList>
</td>
<td style="width: 100px">
</td>
</tr>
<tr>
<td style="width: 100px">
Post Code</td>
<td style="width: 100px">
<asp:TextBox ID="Zip" Runat="server"></asp:TextBox>
</td>
<td style="width: 100px">
<asp:Button ID="AutofillButton" Runat="server" Text="Autofill" />
</td>
</tr></table>
</asp:Content>

Callback.ASPX.CS file

public partial class Callback_aspx
{
static readonly string _script =
"<script language=\"javascript\">\n" +
"function __onCallbackCompleted (result, context)\n" +
"{{\n" +
" var len = result.length;\n" +
" var city = result.substring (0, len - 3);\n" +
" var state = result.substring (len - 2, len)\n" +
" document.getElementById ('{0}').value = city;\n" +
" var select = document.getElementById ('{1}');\n" +
" for (var i=0; i<select.options.length; i++) {{\n" +
" if (select.options[i].text == state) {{\n" +
" select.options[i].selected = true;\n" +
" break;\n" +
" }}\n" +
" }}\n" +
"}}\n" +
"</script>";

    void Page_Load (object sender, EventArgs e)
{
// Get a callback event reference
string cbref = GetCallbackEventReference (this,
String.Format ("document.getElementById ('{0}').value", Zip.ClientID),
"__onCallbackCompleted", "null", "null");

        // Wire the callback event reference to the Autofill button with
// an onclick attribute, and add "return false" to event reference
// to prevent a postback from occurring
AutofillButton.Attributes.Add ("onclick", cbref + "; return false;");

  // Register a block of client-side script containing __onCallbackCompleted
RegisterClientScriptBlock ("MyScript", String.Format (_script, City.ClientID,
State.ClientID));
}

    // Server-side callback event handler
string ICallbackEventHandler.RaiseCallbackEvent(string arg)
{
string state = arg.ToUpper();
if (state.StartsWith("3187"))
return "Brighton;VIC";
else if (state.StartsWith("SA"))
return "South Australia;SA";
else if (state.StartsWith("NS"))
return "New South Wales;NSW";
else
return "Unknown;TK";
}
}

Comments

  • Anonymous
    March 23, 2005
    Hi, dave
    I am trying to make a flicker free website using script callback in ASP.NET 1.1.
    I have a javascript function which is called when a button is clicked.

    request goes to the server and I render my user control and write it to the response stream.

    I have a span tag in which i am putting the render code of the data returned by my request.
    One problem that i am facing is, alignment.

    I have few text to which style should have been applied, but it is not doing, but it is doing when i post back data or the other text in the page.

    This is something that i am not able to figure out, any help or hint from you would be great for me.
    Thanks in advance.
    Regards,
    Sugandh

  • Anonymous
    March 23, 2005
    hey sorry to bug you,
    solved the problem.
    Regards,
    Sugandh
  • Anonymous
    March 24, 2005
    Cool, PS have you looked at SmartNavigation? Cheers Dave