views:

615

answers:

2

I was able to make a simple webpart by following what was on this website http://www.codeguru.com/csharp/.net/net_asp/webforms/article.php/c12293/ But now I would like to add controls like TextBoxes, Buttons, TreeViews ... How can I do that? The place I coded into was just a class library!! How can I use a designer and a page for coding?

+1  A: 

Try not to override the Render method of the WebPart class, but instead override the CreateChildControls method like this:

protected TextBox txtName;
protected Button btnSubmit;

// create child control
protected override void CreateChildControls() {
    txtName = new TextBox();
    this.Controls.Add(txtName);
    btnSubmit = new Button();
    btnSubmit.Text = "Submit Name";
    this.Controls.Add(btnSubmit);
}
Robin Meuré
thanks for your reply man. unfortunately i tried it out but it didnt work. the controls didnt appear but only the text hello world appeared. shall i remove this method completely?
Ahmad Farid
Where have you deployed the WebPart dll . In GAC ? After changing the WebPart dll you will have to restart the IIS to get the changes working.
Kusek
+1  A: 

You need to All the required controls in the Code Controls.Add in side the CreateChildControls method and you will not be able to use designer to design the controls as you do for the Custom (Till Visual Studio 2010 is Released - It has option called Web Part Designer).Refer to this link for example of how to add controls using code. If you want to add multiple control, arranging the controls and applying the style sheet will be a tough. I recommand you to use the SmartPart, what it does is that it helps you to load any usercontrol you created as webPart. So you dont need to worry to Add Controls using code, place them, style them.

Kusek
+1 I agree, use an ascx usercontrol and host that inside your webpart.
Johan Leino
SmartPart is great man. thanks :)but now i have another problem because i cant see the design in visual studio. any help?http://stackoverflow.com/questions/1158248/can-not-find-project-web-item-error
Ahmad Farid