Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm sure I've just missed something and was hoping someone else had run across this and knows what I'm doing wrong. Just for a sample and to make it easier to understand I created a simple test project (Web Control Library). In here I took out the renders method and put code in the CreateChildControls() method to create a new textbox and add it to page..

 

Dim _txt as New TextBox

_txt.ID = "txtTest"

controls.add (_txt)

 

So I created a new ASP.NET Web application and added my control to the toolbar so I can drag it on my form.

 

My problem (and question) is when a postback event happens, say a button with no action is clicked and page reloads, whatever is in the textbox is getting cleared out. I know that CreateChildControls() fires for every control on the page when its loaded so how do you keep values in the textbox? If you drag a textbox control from the toolbox on a form and run that it keeps the value in it when it reloads. So I didn't know if my problem was with Viewstate or something totally different. Any help is appreciated, thanks in advance...

Posted

you can use CreateChildControls too!

 

e.g. your class:

public class TextControl : Control, IPostBackDataHandler, INamingContainer

 

 

you have to add:

public bool LoadPostData(string postDataKey, NameValueCollection postData)

{

return true:

}

 

protected override void Render(HtmlTextWriter writer)

{

this.EnsureChildControls();

base.Render (writer);

writer.Write("<input name='"+this.UniqueID+"' type='hidden'>");

}

 

That's all!

Posted

Having issues

 

WebJumper,

 

You said you can use CreateChildControls and then used Render in your example, Are you supposed to use both or were you just using Render in your Example ? I'm not too familiar with C#, what is base (base.render) ?

 

Am I supposed to create my textbox in the render sub by doing

writer.write ("<input type=textbox...">) and is that the same as the code in my previous example creating the textbox in CreateChildControls()

 

Sorry, having trouble figuring this out so I'm trying to understand how this is working.

Posted

Got It

 

well, i had Implements IPostBackEventHandler, so I changed that to DataHandler which added the LoadPostData and RaisePostDataChangedEvent. I didn't use Render and just left my CreateChildControls and it worked! I'm not totally sure how all that just worked but it did! Thanks for the tips!!!

Posted
i create my controls in createchildcontrols and the hidden textbox is for eventhandling in my controls. the raisepostback event is overritten in my controls and the raisepostdata will only be fired when i use the render method. therefore i create a hidden field, nobody sees it and the events work. just a workarround...
Posted

using render...

 

when you use the render are you also using CreateChildControls ? When I have both methods and

debug it goes thru CreateChildControls first and then the render sub and my textbox does not show

up. It only shows up if I use CreateChildControls() How do you use the hidden for eventhandling ? I think that could

be helpful.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...