0

I've been trying to implement ICallbackEventHandler in a WebControl. I was able to do it easily in a normal Asp page. Could you please tell me what is the issue with the following code?

HTML Code:

<body>
    <form id="form1" runat="server">
        <div>
            <web:CustomC ID="Custom1" runat="server" />
            <br />
        </div>
    </form>
</body>

C#:

namespace CustomControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:CustomC runat=server></{0}:CustomC>")]
    public class CustomC : WebControl, System.Web.UI.ICallbackEventHandler
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }
        protected string tempvar;
        protected override void RenderContents(HtmlTextWriter output)
        {
            output.RenderBeginTag(HtmlTextWriterTag.Div);
            output.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID + "_t");
            output.RenderBeginTag(HtmlTextWriterTag.Label);
            output.Write("Enter: ");
            output.RenderEndTag();
            output.AddAttribute(HtmlTextWriterAttribute.Type, "button");
            output.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID + "_i");
            output.AddAttribute(HtmlTextWriterAttribute.Value, "Click Me");
            output.AddAttribute("OnClick", "CallServer('test','');");
            output.RenderBeginTag(HtmlTextWriterTag.Input);
            output.RenderEndTag();
            output.RenderEndTag();
        }

        protected override void OnPreRender(EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(
                typeof(Page),
                "HandleResult",
                "function HandleResult(arg) {" +
                "var lbl_id = this.ClientID + '_t'; var lbl = document.getElementById(lbl_id);" +
                lbl.innerHTML = arg;" +
            "}", true);

            ClientScriptManager cm = Page.ClientScript;
            string cbReference = cm.GetCallbackEventReference(this, "arg", "HandleResult", "context");
            string cbScript = "function CallServer(arg, context){" + cbReference + ";}";
            cm.RegisterClientScriptBlock(this.GetType(), "CallServer", cbScript, true);

        }

        public void RaiseCallbackEvent(string eventArgument)
        {
            tempvar= eventArgument;
        }

        public string GetCallbackResult()
        {
            return "Valid Data is:" + tempvar ;
        }



    }
}
  • Does your control render properly in SharePoint? – Slaven Semper Oct 9 '19 at 5:37
0

Sample test demo:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestWeb.WebControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:WebCustomC runat=server></{0}:WebCustomC>")]
    public class WebCustomC : WebControl, ICallbackEventHandler
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        protected string tempvar;
        protected override void RenderContents(HtmlTextWriter output)
        {
            output.RenderBeginTag(HtmlTextWriterTag.Div);
            output.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID + "_t");
            output.RenderBeginTag(HtmlTextWriterTag.Label);
            output.Write("Enter: ");
            output.RenderEndTag();
            output.AddAttribute(HtmlTextWriterAttribute.Type, "button");
            output.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID + "_i");
            output.AddAttribute(HtmlTextWriterAttribute.Value, "Click Me");
            output.AddAttribute("OnClick", "CallServer('test','');");
            output.RenderBeginTag(HtmlTextWriterTag.Input);
            output.RenderEndTag();
            output.RenderEndTag();
        }

        protected override void OnPreRender(EventArgs e)
        {
            ClientScriptManager scriptManager = Page.ClientScript;

            String cbReference = scriptManager.GetCallbackEventReference(this, "returnValue", "GetResult", "context");

            String callbackScript = "function CallServer(returnValue, context) {" + cbReference + "; } function GetResult(returnValue, context) {alert(returnValue)}";

            scriptManager.RegisterClientScriptBlock(this.GetType(), "CallServer", callbackScript, true);

            if (Page.IsCallback)
            {
                ////IF any operation do it here.
            }

        }

        private string returnValue;
        public string GetCallbackResult()
        {
            return returnValue;
        }

        public void RaiseCallbackEvent(string eventArgument)
        {
            returnValue = DateTime.Now.ToString();
        }
    }
}

page:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestPage.aspx.cs" Inherits="TestWeb.TestPage" %>

<%--<%@ Register Src="~/UserControls/CustomC.ascx" TagName="CustomC" TagPrefix="web"%>--%>
<%@ Register Namespace="TestWeb.WebControls" TagPrefix="web" Assembly="TestWeb" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <web:WebCustomC ID="Custom1" runat="server" />
        </div>
    </form>
</body>
</html>

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.