tenksnit

I have found a few posts about this particular subject but to no avail.

At the moment I am declaring 2 text boxes and a button in a particular web part within the RenderWebPart() function.

When testing it on my sharepoint site, it seems that the event handler for the button isn't called and nothing happens.

I took a wild guess that when it does the RenderWebPart() after the post back, the controls are created anew and are thus blank. So I wrapped the declarations in if(!Page.IsPostBack).

After I tested it, it caused an error on the sharepoint (MOSS 2007) page saying:

The "xxxxx" Web Part appears to be causing a problem. Object reference not set to an instance of an object.

I'm guessing that after the PostBack, it can't find the controls I just created. Even if it did, the actions within my Button.Click probably wouldn't work still.

I think I'm going about this a completely wrong way, can anybody point me in the right direction



Re: SharePoint - Development and Programming Retaining Web Part values after postback

Ishai Sagi

Do not create controls in the RenderWebPart event. this is the wrong way to do server controls.

Instead, use the CreateChildControls event. This is where you create them, and connect them to events.

See my article on server-side-controls-and-data-binding:

http://www.sharepoint-tips.com/2007/03/server-side-controls-and-data-binding.html

While that article does not explain the concept of server side controls, it does cover where and when to declare your controls, when to instantiate them and how to use them.






Re: SharePoint - Development and Programming Retaining Web Part values after postback

tenksnit

Thanks, your article helped alot.

Your website is also very informative, thanks again.





Re: SharePoint - Development and Programming Retaining Web Part values after postback

Bhavdip Shah

Error



The "test" Web Part appears to be causing a problem. Object reference not set to an instance of an object.

Web Parts Maintenance Page: If you have permission, you can use this page to temporarily close Web Parts or remove personal settings. For more information, contact your site administrator.

Troubleshoot issues with Windows SharePoint Services.





Re: SharePoint - Development and Programming Retaining Web Part values after postback

Rajani Ramineni

Hi,

iam trying to follow the same step as metioned in the article but still the click event of my button is not triggered.

when i tried to debug i could see that CreateChildControls method is triggered and the linking of click event of the button is removed and button click event is never getting raised.

Can anyone say wat can be the problem .Thanks

Regards

Rajani





Re: SharePoint - Development and Programming Retaining Web Part values after postback

Andy-H

This works for me:

tipsQuotesControl = this.Page.LoadControl( tipsQuotesControlUrl );

LinkButton btnPrevious = tipsQuotesControl.FindControl( "lnkButPrevious" ) as LinkButton;

if ( btnPrevious != null )

btnPrevious.Click += new EventHandler( btnPrevious_Click );

if ( tipsQuotesControl != null )

this.Controls.Add( tipsQuotesControl );

....

protected void btnPrevious_Click( object sender, EventArgs e )

{

...

}





Re: SharePoint - Development and Programming Retaining Web Part values after postback

zedekiah

Are you testing in the Web Gallery It wont post back until you add it to a page.






Re: SharePoint - Development and Programming Retaining Web Part values after postback

Rajani Ramineni

Hi zedekiah,

Thanks a lot, that was the problem i was testing it in webpart galary. Now tried adding it in a page it is working fine thanks a lot for the help.

Regards

Rajani