locked
How to retreive attributes from WebControl using custom postback RRS feed

  • Question

  • User2032120163 posted

    Hi,

    I have created a custom WebControl which implements IPostBackDataHandler. I intend to use some javascript to set 2 attributes of this control, and then have the ability to access those attributes during postback. The problem is that after postback, my control is not in the postCollection list of values, so there is no data being posted back. I have injected javascript to change the value of an attribute on the client side once the control is rendered, as I thought perhaps the postback only brings back changed attributes, but still no luck. Is there not a way to return the attributes of your custom control through post back? Any help would be much appreciated.  Code below:

    [ToolboxData("<{0}:DraggableControl runat=server></{0}:DraggableControl>")]
        public class DraggableControl:WebControl,IPostBackDataHandler,INamingContainer
        {
            private int iX;
            private int iY;
    
            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                 if (Page != null)
                    Page.RegisterRequiresPostBack(this);
            }
    
            protected override void OnPreRender(EventArgs e)
            {
               
                base.OnPreRender(e);
                
                string sDragTest = "var p = document.getElementById('" + this.ClientID + "');";
                    sDragTest += "p.attributes.getNamedItem('value').value = 'asdf';alert('value attribute changed');";
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "dragTestScript", sDragTest, true);
    
              
            }
    
            protected override void RenderContents(HtmlTextWriter writer)
            {
                writer.Write("<img src=\"" + Page.ResolveUrl("~/Controls/StatusImage.ashx?r=0&b=0&g=255") + "\" />");
                writer.Write("<div style=\";top:26px;width:80px;text-align:center;background:white;border:solid 1px black;-moz-border-radius: 4px;-webkit-border-radius: 4px;border-radius: 4px;\">");
                writer.Write("0.123456");
                writer.Write("</div>");
            }
    
            protected override void AddAttributesToRender(HtmlTextWriter writer)
            {
                writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "80px");
                writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "25px");
                writer.AddStyleAttribute(HtmlTextWriterStyle.Position, "absolute");
                writer.AddStyleAttribute(HtmlTextWriterStyle.Left, iX.ToString() + "px");
                writer.AddStyleAttribute(HtmlTextWriterStyle.Top, iY.ToString() + "px");
                writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign, "center");
                //writer.AddAttribute(HtmlTextWriterAttribute.Id, this.UniqueID);
                writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
                writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
                writer.AddAttribute("xpos", iX.ToString() + "px");
                writer.AddAttribute("ypos", iY.ToString() + "px");
                writer.AddAttribute("value", "mytestvalue");
                base.AddAttributesToRender(writer);
            }
    
            protected override HtmlTextWriterTag TagKey
            {
                get
                {
                    return HtmlTextWriterTag.Div;
                }
            }
    
            #endregion
    
          
            #region IPostBackDataHandler Members
    
            public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
            {
                string s = postCollection[this.UniqueID]; //not using postDataKey
                if (s != null)
                {

    // THIS BIT IS NEVER REACHED BECAUSE OUR CONTROL IS NOT FOUND IN THE POSTCOLLECTION

                    s = HttpContext.Current.Server.HtmlDecode(s);
                    if (!string.IsNullOrEmpty(s))
                    {
                       // parse the modified attributes 
                    }
                   
                    return true;
                }
                return false;
            }
    
            public void RaisePostDataChangedEvent()
            {
                
            }
    
            #endregion
        }
    }
    
    
    Friday, September 9, 2011 7:10 AM

Answers

  • User3866881 posted

    string s = postCollection[this.UniqueID]; //not using postDataKey

    Because you are using something like a QueryString. So you can do this:

     string s = postCollection[this.ClientID];

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 10, 2011 9:58 PM