namespace CallbackTest
{
    public class Test: System.Web.UI.WebControls.WebParts.WebPart, ICallbackEventHandler
    {
        private string callbackData;
        private Label callbackLabel;


        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            this.callbackLabel.RenderControl(writer);
        }
  
       
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            callbackLabel = new Label();
            callbackLabel.Text = "no callback occurred yet";
            callbackLabel.Attributes.Add("onmouseover", "alert('mouseover'); serverRequest('" + callbackLabel.ClientID + "', '');");
            callbackLabel.ID = "callbackLabelControl";
            this.Controls.Add(callbackLabel);


            // Creating the scripts which need to be called
            string clientScript = "function serverRequest(args, context){{ " +
            Page.ClientScript.GetCallbackEventReference(this, "args", "getResult", "context") + "; }} function getResult(result, context){ eval(result); }";
            // Register the client script
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MessageAlertCallback", clientScript, true);
        }


        public string getRes(Int64 id)
        {
            return "alert('callback completed successfully');";
            //var el = document.getElementById('" + callbackLabel.ClientID + "'); el.innerHTML = 'Callback completed successfully';
        }

        public void RaiseCallbackEvent(string eventArgument)
        {
            Int64 id = 0; 

            callbackData = getRes(0) + "alert('" + eventArgument + "');";
        }

        public string GetCallbackResult()
        {
            return callbackData;
        }
        
    }
}

I am trying to pass the ClientID of my Label to the client (in GetCallbackResult) so that when the client receives the data, it can use getElementById to find where it came from. So far I am having no luck. Can anyone help?

Btw the alerts and such are just for testing purposes, and they are in fact being fired, just ignore them.

Well, this is embarrassing, but I solved the problem. Of course I spend a couple hours on it and can't solve it, but as soon as I post a question.... *sigh*

updated code w/ problem solved.

protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            callbackLabel = new Label();
            callbackLabel.Text = "no callback occurred yet";
            callbackLabel.ID = "callbackLabelControl";
            this.Controls.Add(callbackLabel);
            callbackLabel.Attributes.Add("onmouseover", "alert('mouseover'); serverRequest('" + callbackLabel.ClientID + "', '');");
         


            // Creating the scripts which need to be called
            string clientScript = "function serverRequest(args, context){{ " +
            Page.ClientScript.GetCallbackEventReference(this, "args", "getResult", "context") + "; }} function getResult(result, context){ eval(result); }";
            // Register the client script
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Callback", clientScript, true);
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.