Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 744
  • Last Modified:

custom webpart properties

Hi EE,
I have created a simplewebpart.
The webpart has a text box whose width is set as a custom webpart property (Editor Part)which can be editable.
Now I wan to achive this only by setting its properties but was not able to do so have kept a button also to make the webpart work.
now once I set the text box width property in Editor Part and then click on the button it changes its width.
But I want this to work with out the button ie when I change in Editor Part and click ok of the editor part it should effect the text bod width.
Please find my code in attachment


namespace CustomProperties
{
    [Guid("bf6fb9b-c39a-4798-bac1-a193e334d98")]
    public class CustomProperties : System.Web.UI.WebControls.WebParts.WebPart
    {

        protected override void OnPreRender(EventArgs e)
        {
            //base.OnPreRender(e);
            Title = "code Title:Custom Properties";
        }
        TextBox txtName;
        Button btnSet;
        int inttextWidth;

       [WebBrowsable(true)]
        [WebDisplayName("Enter textBox Width")]
        [WebDescription("Width of the TextBos")]
        [Personalizable(PersonalizationScope.User)]
        public int InttextWidth
        {
            get { return inttextWidth; }
            set { inttextWidth = value; }
        }

       protected override void CreateChildControls()
       {
           //base.CreateChildControls();
           txtName = new TextBox();
           txtName.Text="";
           txtName.Width=inttextWidth;
           Controls.Add(txtName);

           btnSet = new Button();
           btnSet.Text = "Set Width";
           btnSet.Click += new EventHandler(btnSet_Click);
           Controls.Add(btnSet);

       }

       void btnSet_Click(object sender, EventArgs e)
       {
           //throw new NotImplementedException();
           txtName.Width = inttextWidth;
       }

       protected override void RenderContents(HtmlTextWriter writer)
       {
           //base.RenderContents(writer);
           txtName.RenderControl(writer);
           btnSet.RenderControl(writer);
       }


        
    }
}

Open in new window

0
agcps
Asked:
agcps
  • 2
  • 2
3 Solutions
 
Jamie McAllister MVPArchitectCommented:
I don't have my environment to hand but there are a couple of things I'd try;

Set the width to be the property value rather than the internal variable name - this is good practise;
Change txtName.Width=inttextWidth; to be txtName.Width=InttextWidth;

Set the Width value in the RenderContents method as well, I have a hunch that there might be a timing issue on the property value.
0
 
agcpsAuthor Commented:
Can you please let me know more clearly
"Set the Width value in the RenderContents method as well, I have a hunch that there might be a timing issue on the property value."
0
 
Jamie McAllister MVPArchitectCommented:
Try putting the code txtName.Width=inttextWidth; into RenderContents method as well, as I just have a hunch that there might be a timing issue as to either when the custom property value is available or when the control will take the value setting.
0
 
mo1oneCommented:
Please try invoking EnsureChildControls as first method in in render method()
0
 
agcpsAuthor Commented:
Need to try out this
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now