ASP.NET 2.0 ICallbackContainer.GetCallbackScript()

M

megha_bansal

hello all

I have upgraded to ASP.NET 2 and since then i had the code below stopped
working after upgrading and modiftying the code:

using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.Text;
using System.Reflection;
using System.IO;
using System.ComponentModel;


namespace MsdnMag.Controls
{
public class CallbackValidator : CompositeControl, ICallbackContainer,
ICallbackEventHandler
{
//
**************************************************************************************************
// PRIVATE properties
private StringCollection _controlsToValidate;


//
**************************************************************************************************
// PROPERTY: ButtonText
// Gets or sets the text that appears as the caption of the button that
starts the operation
[Description("The text to be shown on the button")]
public string ButtonText
{
get { return Convert.ToString(ViewState["ButtonText"]); }
set { ViewState["ButtonText"] = value; }
}


//
**************************************************************************************************
// PROPERTY: ShowDetailedInfo
// Indicates whether ErrorMessage should be used instead of Text (if both
Text+tooltip is used)
[Description("If enabled, displays the full error message if the validation
fails")]
public bool ShowDetailedInfo
{
get { return Convert.ToBoolean(ViewState["ShowDetailedInfo"]); }
set { ViewState["ShowDetailedInfo"] = value; }
}


//
**************************************************************************************************
// PROPERTY: ControlsToValidate
// Gets the collection of controls to validate. Each control is specified
through its ID
[Browsable(false)]
public StringCollection ControlsToValidate
{
get {
if (_controlsToValidate == null)
_controlsToValidate = new StringCollection();
return _controlsToValidate;
}
}


//
**************************************************************************************************
// METHOD OVERRIDE: CreateChildControls
// Builds the UI of the control
protected override void CreateChildControls()
{
Controls.Clear();

// Create the button to start the callback operation
Button b = new Button();
b.Text = ButtonText;
b.CopyBaseAttributes(this);
if (ControlStyleCreated)
b.ApplyStyle(ControlStyle);

// Attach some script code to trigger the remote call
ICallbackContainer cont = (this as ICallbackContainer);
b.OnClientClick = cont.GetCallbackScript((IButtonControl) b, "");

// Add the button to the control's hierarchy for display
Controls.Add(b);

// Inject any needed script code into the page
EmbedScriptCode();
}


//
**************************************************************************************************
// METHOD OVERRIDE: Render
// Renders the UI of the control (Ensures the control behaves well at
design-time)
protected override void Render(HtmlTextWriter writer)
{
EnsureChildControls();
base.Render(writer);
}


//
**************************************************************************************************
// METHOD IMPLEMENTATION: ICallbackContainer.GetCallbackScript
// Define the script code to start the callback operation
public void GetCallbackScript(IButtonControl buttonControl, string argument)

{
// Prepare the input for the server code
int i = 0;
StringBuilder sb = new StringBuilder("");
foreach (string s in ControlsToValidate)
{
if (i>0)
sb.Append("|");
sb.Append(s);
i++;
}
string args = String.Format("'{0}'", sb.ToString());
string js = String.Format("javascript:{0};{1};{2}; return false;",
"__theFormPostData = ''",
"WebForm_InitCallback()",
Page.GetCallbackEventReference(this, args, "CallbackValidator_UpdateUI",
"null"));
return js;
}


//
**************************************************************************************************
// METHOD IMPLEMENTATION: ICallbackEventHandler.RaiseCallbackEvent
// Server part of the callback
public void RaiseCallbackEvent(string eventArgument)
{
// Execute the server-side code
// Receives a string like this: ctl|ctl|...ctl
// Sends back a string like this: ctl:valid:msg:tip|...ctl:valid:msg:tip
StringBuilder sb = new StringBuilder("");
string[] controlsToValidate = eventArgument.Split('|');
int i = 0;
foreach (string s in controlsToValidate)
{
BaseValidator val = NamingContainer.FindControl(s) as BaseValidator;
if (val != null)
{
val.Validate();

if (i > 0)
sb.AppendFormat("|");
sb.AppendFormat("{0}:{1}:{2}:{3}",
val.ClientID,
(val.IsValid ? "1" : "0"),
(val.IsValid ? "" : GetErrorText(val)),
(val.IsValid ? "" : GetTooltip(val)));
}
i++;
}

return sb.ToString();
}


//
**************************************************************************************************
// METHOD: EmbedScriptCode
// Insert the script code needed to refresh the UI
private void EmbedScriptCode()
{
Assembly dll = Assembly.GetExecutingAssembly();
StreamReader reader;
reader = new StreamReader(dll.GetManifestResourceStream("MsdnMag.
CallbackValidator.js"));
string js = reader.ReadToEnd();
reader.Close();
if (!Page.ClientScript.IsClientScriptBlockRegistered("CallbackValidator"))
Page.ClientScript.RegisterClientScriptBlock(typeof(CallbackValidator),
"CallbackValidator", js, true);
}

//
**************************************************************************************************
// METHOD: GetErrorText
// Return the error to display beside the validator
private string GetErrorText(BaseValidator val)
{
string msg = "";
string theText = val.Text;
string theError = val.ErrorMessage;

// If both are empty
if (String.IsNullOrEmpty(theText) && String.IsNullOrEmpty(theError))
{
msg = "*";
return msg;
}

// Text takes precedence
if (!String.IsNullOrEmpty(theText))
msg = theText;
else
if (!String.IsNullOrEmpty(theError))
msg = theError;

return msg;
}


//
**************************************************************************************************
// METHOD: GetTooltip
// Return the text to use as the tooltip of the primary error message
private string GetTooltip(BaseValidator val)
{
string theText = val.Text;
string theError = val.ErrorMessage;

if (String.IsNullOrEmpty(theText) || !ShowDetailedInfo)
return "";

if (!String.IsNullOrEmpty(theError))
return theError;

return "";
}
}
}

and I received following errors:

1. 'MsdnMag.Controls.CallbackValidator' does not implement interface member
'System.Web.UI.WebControls.ICallbackContainer.GetCallbackScript(System.Web.UI.

WebControls.IButtonControl, string)'. 'MsdnMag.Controls.CallbackValidator.
GetCallbackScript(System.Web.UI.WebControls.IButtonControl, string)' is
either static, not public, or has the wrong return type.

2. 'MsdnMag.Controls.CallbackValidator' does not implement interface member
'System.Web.UI.ICallbackEventHandler.GetCallbackResult()'.


please help its really very urgent!

thanks and warm regards!
Megha
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top