Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

My initial goal is to achieve the following

  1. Render dynamic TextBox in custom editor webpart

  2. Manage Text (, visibility, height, width etc) of the TextBox from editor part.

  3. We can consider Sharepoint webpart or asp.net webpart framework

I have tried following way ( with asp.net webpart framework) but I can not achieve the initial goal.

//CustomWebPart.cs
 private string _text;
 [WebBrowsable(false)]
 [Personalizable(PersonalizationScope.Shared)]
 public string Text
 {
    get { return _text; }
    set { _text = value; }
 }
 protected override void CreateChildControls()
 {
    TextBox txt = new TextBox();
    txt.Text = Text;
    Controls.Add(txt);
 }

//CustomEditor.cs
 private TextBox txtExample;
 protected override void OnInit(EventArgs e)
 {
    base.OnInit(e);
    txtExample= new TextBox();
 }
 protected override void CreateChildControls()
 {
    Controls.Add(txtExample);
 }
 public override bool ApplyChanges()
 {
     EnsureChildControls();
     CustomWebPart part = WebPartToEdit as CustomWebPart;
     if (part != null)
     {
          part.SqlQuery = txtExample.Text;
     }
 }
 public override void SyncChanges()
 {
    EnsureChildControls();
    CustomWebPart part = WebPartToEdit as CustomWebPart;
    if (part != null)
    {
        txtExample.Text = part.SqlQuery;           
    }
 }

Problem is very clear:

After changing the text from editor webpart, once I click on OK or Apply button the system first triggers the CreateChildControls() event for CustomWebpart.cs before firing ApplyChanges(), even before firing CreateChildControls() for CustomEditor.cs , as a result Textbox of the webpart can not update on time.

So, how can we render asp.net server side controls in webpart and manage( changing text, visibility, width, height, etc) from editor part. ?

Please help.

share|improve this question

3 Answers 3

You should be the Visual Web part template in Visual Studio 2010 which gives you a User-Control onto which you could arrange your controls. Then from tool-part you gain access very easily to whatever controls on the same manner you already did, by casting the the Web part and find your user control.

If you still would like to keep up on your way have a look at this article doing the same http://waelmohamed.wordpress.com/2010/03/14/create-custom-web-part-properties-using-custom-tool-parts-with-a-complete-example-code/ or this http://zimmergren.net/technical/how-to-custom-web-part-properties-toolpart

share|improve this answer
    
Hi C.Marius, thanks for your response, but still I can not achieve my goal :( let repeat same thing again. 1. WebPart should be rendered with some TextBoxs(txtExample1, txtExample2) and Buttons(btnExample1, btnExample2) 2. On editor part their should have some custom properties to manage the webpart textboxs and buttons. let say a) IsTextExample1Visible? b) TextForExample1 c) IsTextExample2Visible? d) TextForExample2 e) IsButton1Visible? f) IsButton2Visible? After clicking Apply or Ok button, webpart textboxs and buttons should be update based on properties settings. –  Shahdat Sep 23 '12 at 15:53

Try to remove EnsureChildControls(); of ApplyChanges()

share|improve this answer
    
hmm, I tried but no luck –  Shahdat Oct 4 '12 at 10:08

try this:

 public override bool ApplyChanges() 
 { 
     EnsureChildControls(); 
     CustomWebPart part = WebPartToEdit as CustomWebPart; 
     if (part != null) 
     { 
          part.SqlQuery = txtExample.Text; 
     } 
     return true;
 } 

Your event is bool but your not returning anthing? return true; will return the changes, also are you inheriting from EditorPart in your class.

for further reference please look at:

http://sharepoint-snippets.com/custom-editorpart/

EDIT

Ok so make sure you follow the example on msdn!

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webpartpages.toolpart.applychanges.aspx

in the example above it will do exacly what you need and less code :) , its the WebPartReference (WebPart) object that will ammend the changes, just make sure you follow it through ;) .

to understand what events fire first look at this as a good reference :)

http://geekswithblogs.net/KunaalKapoor/archive/2012/07/18/sharepoint-webpart-life-cycle-events-and-event-sequence.aspx

hope it helps :)

share|improve this answer
    
Oh no, basically I return true in my code but forgot to copy here. Look, the main problem is CreateChildControls() of CustomWebPart.cs fired before of ApplyChanges() of editor part. Thanks –  Shahdat Oct 10 '12 at 15:56
    
ok why dont you call apply changes in prerender or render events? also move the EnsureChildControls(); at the end so its called last :) –  ali Sharepoint Oct 10 '12 at 16:25
    
which prerender or render event you mean, ApplyChanges() is override method is called automatically after clicking on Apply button only? no luck for moving EnsureChildControls() at end. thanks –  Shahdat Oct 10 '12 at 20:05
    
iv ammended my asnswer, msdn has an example on what your doing, also is there any posback accuring when you click on the apply button? –  ali Sharepoint Oct 11 '12 at 8:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.