locked
Why doesn't LoadPostData work? RRS feed

  • Question

  • User-1488931086 posted

    Hi——

    I've created a customized control and here's snippet of codes——My question is——When I set a breakpoint at LoadPostBack function to debug the app, nothing happens……why?????? 

    You know that I've really GOT THE SUBMIT BUTTON……:(

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Reflection;
    using System.Drawing;
    using System.Web.UI.HtmlControls;
    using System.Drawing.Imaging;
    using System.IO;

    namespace VaidationCode
    {
    [ToolboxData("<{0}:ValidationCode runat=server></{0}:ValidationCode>")]
    public class ValidationCode:CompositeControl,IPostBackDataHandler
    {

    HtmlInputSubmit btn = null;   //submit button……

    protected override void CreateChildControls()
    {
    Controls.Clear();
    btn = new HtmlInputSubmit();
    btn.ID = "checkbutton";
    btn.Value = ButtonName;
    Controls.Add(btn);

    ChildControlsCreated = true;
    }


    protected override void Render(HtmlTextWriter writer)
    {
    btn.RenderControl(writer);
    }

    public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
    {
       //Do something here……
    }

    public void RaisePostDataChangedEvent()
    {
      //Do something here……
    }
    }
    }
    }

    Tuesday, May 1, 2012 8:43 AM

All replies

  • User-1415340296 posted

    Hello,

    I saw your code snippet posted. But I did not find "LoadPostData" function being called any where? How could then the breakpoint be executed.

    Tuesday, May 1, 2012 10:04 AM
  • User-1488931086 posted

    But I did not find "LoadPostData" function being called any where? How could then the breakpoint be executed.

    But the LoadPostData should be called automatically because I'm using submit button,and when I run the page with the control,I should catch up the event to be raised and the function should be called……:(

    Tuesday, May 1, 2012 9:01 PM
  • User-1488931086 posted

    Now I've solved the problem by using this——

    namespace CustomControlTest
    {
        [DefaultProperty("Text")]
        [ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
        public class ServerControl1 : CompositeControlIPostBackDataHandler
        {
            HtmlInputSubmit submitBtn = new HtmlInputSubmit();

            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                Page.RegisterRequiresPostBack(this);
            }
            protected override void CreateChildControls()
            {
                submitBtn.ID = "submit";
            }

            public bool LoadPostData(string postDataKeySystem.Collections.Specialized.NameValueCollection postCollection)
            {
                throw new NotImplementedException();
            }

            public void RaisePostDataChangedEvent()
            {
                throw new NotImplementedException();
            }

            protected override void Render(HtmlTextWriter writer)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.NamesubmitBtn.UniqueID);
                writer.AddAttribute(HtmlTextWriterAttribute.Type"submit");
                writer.RenderBeginTag(HtmlTextWriterTag.Input);
            }
        }
    }
    Plz notice the bold statement……Why should I use it?
    And I notice that MSDN it doesn't use such method……Why?
    http://msdn.microsoft.com/en-us/library/system.web.ui.ipostbackdatahandler.aspx
    Tuesday, May 1, 2012 10:36 PM
  • User-1488931086 posted

    Hello again——My addition:

    I notice that if I use directly output Html codes by using "output.Write……",Everything goes well with me:

    namespace CustomControlTest
    {
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
    public class ServerControl1 : CompositeControl, IPostBackDataHandler
    {

    HtmlInputSubmit btn = new HtmlInputSubmit();

    protected override void OnInit(EventArgs e)
    {
    base.OnInit(e);
    //NOTICE!!!!!!!========Page.RegisterRequiresPostBack(this);      
    }
    protected override void CreateChildControls()
    {
    btn.ID = "mytext";
    Controls.Add(btn);
    ChildControlsCreated = true;
    }

    //BreakPoint here!……

    public virtual bool LoadPostData(string postDataKey,
    NameValueCollection postCollection)
    {

    return true;
    }


    public virtual void RaisePostDataChangedEvent()
    {
    //OnTextChanged(EventArgs.Empty);
    }


    protected override void Render(HtmlTextWriter output)
    {
    //If using this, breakpoint can be reached……
    output.Write("<INPUT type= 'submit' name = " + this.UniqueID
    + " value = 'aaa' />");

    //But this doesn't enter the breakpoint……Until I use "NOTICE====" Part……:(
    text.RenderControl(btn);
    }

    }
    }

    Tuesday, May 1, 2012 11:06 PM