Custom datatype usercontrol wrapper problem Options
ni5ni6
Posted: Monday, August 18, 2008 6:22:11 PM
Rank: Enthusiast

Joined: 12/18/2007
Posts: 34
Hey,
I'm really stuck here, and just about two days ago, it worked normally.
I made custom datatype with some really simple code, following Tim's blog post (
http://www.nibble.be/?p=24):
Code:
    public partial class testType : System.Web.UI.UserControl, umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor
    {
        public string keep;
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (IsPostBack)
            {
                keep = TextBox1.Text;

            }
            TextBox1.Text = keep;
        }

        #region IUsercontrolDataEditor Members

        public object value
        {
            get
            {
                return keep;
            }
            set
            {
                keep = value.ToString();
            }
        }

        #endregion
    }

The problem is that I always get empty TextBox1.Text string after postback.
I checked request values with Fiddler and everything seems just perfect. Sooo, it must be some server side code which is messing up with values..?
Anyone may experience something similar??
Ethan
Posted: Wednesday, August 20, 2008 5:27:08 PM
Rank: Newbie

Joined: 8/18/2008
Posts: 10
Location: Iowa
I've been running around trying to find/make a solution for this. I didn't solve the problem but I found a wacky sort of work around.

http://forum.umbraco.org/yaf_postst5890_Usercontrol-wrapper-for-custom-datatype.aspx
ImageIcon
Posted: Saturday, August 23, 2008 1:48:47 AM
Rank: Newbie

Joined: 3/3/2008
Posts: 12
well i have similar code to yours.... and same result it doesnt save.. been looking trough Tim's blog and FTP datatype i dont see an call's to save or other things that i dont have... any help would be appriciated
ImageIcon
Posted: Saturday, August 23, 2008 3:04:33 AM
Rank: Newbie

Joined: 3/3/2008
Posts: 12
i think it has to do with the way the usercontrol is included on the page. maybe something with viewstate... the info is infact postet back to the site and can be referenced with the "key" ...

protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
value = Request.Form["ctl21$TextBox1"];
}
else
{
TextBox1.Text = umbracoValue;
}
}

but if anyone have ideas as to whats going on please share...
rsoeteman
Posted: Saturday, August 23, 2008 8:58:47 AM

Rank: Aficionado

Joined: 4/3/2008
Posts: 103
Location: The Netherlands
Hi,

It's normal that the TextBox1 text value is empty on the postback in the Page_Load Event. This has to do with the way ASP.NET works. This is called the Page Lifecycle. A great overview of the Page Lifecycle you can find here. And this is a nice tutorial from MSDN.

Another problem that I see which has nothing to do with the error is that you are using the private field "keep" directly. There is a property "Value", that maintains the state. A best practice is to use the property instead of the private fields.

Now a possible solution to your problem is to store the TextBox value in the TextBox Changed event.

Code:

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
        Value = TextBox1.Text;
}


Hope this helped you.

Regards,

Richard

ASP.NET and Umbraco development - Soeteman Software
ImageIcon
Posted: Saturday, August 23, 2008 10:43:20 AM
Rank: Newbie

Joined: 3/3/2008
Posts: 12
hmm i used properties in page_load before....

so does The ftpUploadDataType from codegarden here is a snippet so not sure it is completely right

i tryed adding in your code but it never gets hit :S

Code:
if (Page.IsPostBack)
            {
                if (!String.IsNullOrEmpty(uploadFile.FileName))
                {
                    string file = Server.MapPath("/media/ftp/" + uploadFile.FileName);
                    uploadFile.SaveAs(file);
                    FileInfo fi = new FileInfo(file);
                    UploadFile(fi,
                        ConfigurationManager.AppSettings["ftpDataTypeUser"],
                                                ConfigurationManager.AppSettings["ftpDataTypePassword"],
                        ConfigurationManager.AppSettings["ftpDataTypeServer"]);
                    File.Delete(file);
                    umbracoValue = "http://" + ConfigurationManager.AppSettings["ftpDataTypeServer"] + "/" + uploadFile.FileName;
                }
            }

            FileName.Text = umbracoValue;
rsoeteman
Posted: Saturday, August 23, 2008 12:09:48 PM

Rank: Aficionado

Joined: 4/3/2008
Posts: 103
Location: The Netherlands
You can use properties in the page_load event. The only thing you cannot do is ask the submitted value of the textbox. That's just how ASP.NET works, the changed event comes after the load event. The upload control has a different behavior as normal textboxes, so it is possible to use this in the page_load event.

Hope this helps

Richard

ASP.NET and Umbraco development - Soeteman Software
ImageIcon
Posted: Saturday, August 23, 2008 1:00:26 PM
Rank: Newbie

Joined: 3/3/2008
Posts: 12
well not to argue this because i can see its not working and i really appriciate your help with this!

to my knowledge
ProcessPostData is run before page_load only if i cannot be processed before for instance if constrols are being initialized in page_load it will run in ProcessPostData 2.nd try.

how would you approach this... i know you suggested the on text changed event but it never fire for me.
i attach visual studio to the process slap in some break points and it never gets hit :(

and trying to do it on later events dont seem to work either so im starting to get clueless as how to handle this... beside looking up the key in request.Form ....

im interesting in knowing whether it will work like this for all contol types or just the textBox... as the upload woks differently

/jesper
rsoeteman
Posted: Sunday, August 24, 2008 8:35:30 AM

Rank: Aficionado

Joined: 4/3/2008
Posts: 103
Location: The Netherlands
Hi Jesper,

Here some answers on your questions. The reason I use the changed events etc. is because these are best practices(coming from the book). If (after trying the suggested options) you still have problems with your code, it's maybe a good idea to send your code to me and I refactor it a little bit for you and will make sure that it works with my suggestions. My emailadress is richard[AT]soetemansoftware[DOT]nl.

ImageIcon wrote:

to my knowledge
ProcessPostData is run before page_load only if i cannot be processed before for instance if constrols are being initialized in page_load it will run in ProcessPostData 2.nd try.


I think ProcessPostdata is responsible for raising the changed events. So your changed event will always be raised.

Quote:
how would you approach this... i know you suggested the on text changed event but it never fire for me.
i attach visual studio to the process slap in some break points and it never gets hit :(


Did you attach the Changed event manually? Otherwise try double click the Textbox in design mode and an event handler will be generated for you.

Quote:
and trying to do it on later events dont seem to work either so im starting to get clueless as how to handle this... beside looking up the key in request.Form ....


You could always try the onloadcomplete method. However the request.form method gets you in trouble in masterpages, usercontrols scenarios etc. These Id´s are never the same and some id´s are dynamicle generated, for instance (ctl21) in your example is a dynamic generated Id. You are never sure that the id is the same when you modify the page. That's why ASP.NET exists in the first place. It's a layer of abstraction so you don't have to worry about requesting data yourself, the framework handles this for you.

Quote:
im interesting in knowing whether it will work like this for all contol types or just the textBox... as the upload woks differently


No it's working on every control that implements the IPostbackDataHandler interface. Here you find an overview of the controls

Hope this helps,

Richard

ASP.NET and Umbraco development - Soeteman Software
ImageIcon
Posted: Sunday, August 24, 2008 10:26:58 PM
Rank: Newbie

Joined: 3/3/2008
Posts: 12
Thank you richard,

i knew about the dynamic id's and thats why this was an issue for me :)

i implementet the text changed Event and finally got it to work!!
:d/

Quote:
Quote:
im interesting in knowing whether it will work like this for all contol types or just the textBox... as the upload woks differently

Quote:

No it's working on every control that implements the IPostbackDataHandler interface. Here you find an overview of the controls


so controls that implements IPostbackDataHandler can be used in page_load?
if that is what you mean ill just note that TextBox is on the list from the link you send...

strange :S

but its a Viable solution to work with data in the Postback event handling stage instead of page_load the real thing here is i want to know when you can do what...

/cheers Jesper

since this post wasnt mine originally ill post my very simple code for the guy who startet the topic

Code:
using System;
using System.Web.UI;

namespace UmbracoControls
{
    public partial class testDataType : System.Web.UI.UserControl, umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor
    {
        public string umbracoValue;
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {

            }
            else
            {
                TextBox1.Text = umbracoValue;
            }
     
        }
       
       
    protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
          value = TextBox1.Text;
        }
       
        public object value
        {
            get
            {
                return umbracoValue;
            }
            set
            {
                umbracoValue = value.ToString();
            }
        }
    }
}

rsoeteman
Posted: Monday, August 25, 2008 8:38:14 AM

Rank: Aficionado

Joined: 4/3/2008
Posts: 103
Location: The Netherlands
Hi Jesper,

I'm glad that it worked out for you. One more answer

Quote:
so controls that implements IPostbackDataHandler can be used in page_load?
if that is what you mean ill just note that TextBox is on the list from the link you send...


No I don't mean that you can use this in the Page_load event. I Mean that it raises a changed event and that you can hook in on that changed event to get the value.

Cheers,

Richard

ASP.NET and Umbraco development - Soeteman Software
ni5ni6
Posted: Wednesday, August 27, 2008 4:11:07 PM
Rank: Enthusiast

Joined: 12/18/2007
Posts: 34
Hi Jesper and Richard,

It worked for me, although I am still confused why is this happening.

Anyway Jesper, thanks for you short overview, and I think there is one thing that has to be modified in Page_Load handler in order to succesfully preserve the value when you try to save the document without modifying text in textbox (triggering the TextChange event).

Code:
using System;
using System.Web.UI;

namespace UmbracoControls
{
    public partial class testDataType : System.Web.UI.UserControl, umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor
    {
        public string umbracoValue;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                // i think this has to stay as it was
                value = TextBox1.Text;
            }
            else
            {
                TextBox1.Text = umbracoValue;
            }

        }


        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            value = TextBox1.Text;
        }

        public object value
        {
            get
            {
                return umbracoValue;
            }
            set
            {
                umbracoValue = value.ToString();
            }
        }
    }
}


If this is an known issue when trying to make custom datatype controls in Umbraco (i didnt say its umbraco issue Shame on you ), maybe it should be nice to be stated in some of existing tutorials, or if you can just confirm this to me I will try to document this topic somehow.

Thank you guys once again. Applause
Cheers!

Nikola
Ethan
Posted: Monday, September 08, 2008 10:26:27 PM
Rank: Newbie

Joined: 8/18/2008
Posts: 10
Location: Iowa
I thought I had this figured out until I copied and pasted the above example.

I copied that verbatim, attached a debugger, the TextBox1_TextChanged event never fired, and everything persisted properly.

WTF?

It was my understanding after reading this post 30 times that I TextBox1.Text should be an empty string. I've even seen this before on previous code. Now all of the sudden the same code works like I'd expect?

Something is seriously not right here.
Users browsing this topic
Guest


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.