custom webpart properties

Posted on 2010-01-02
Last Modified: 2012-05-08
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



    public class CustomProperties : System.Web.UI.WebControls.WebParts.WebPart


        protected override void OnPreRender(EventArgs e)



            Title = "code Title:Custom Properties";


        TextBox txtName;

        Button btnSet;

        int inttextWidth;


        [WebDisplayName("Enter textBox Width")]

        [WebDescription("Width of the TextBos")]


        public int InttextWidth


            get { return inttextWidth; }

            set { inttextWidth = value; }


       protected override void CreateChildControls()



           txtName = new TextBox();




           btnSet = new Button();

           btnSet.Text = "Set Width";

           btnSet.Click += new EventHandler(btnSet_Click);



       void btnSet_Click(object sender, EventArgs e)


           //throw new NotImplementedException();

           txtName.Width = inttextWidth;


       protected override void RenderContents(HtmlTextWriter writer)









Open in new window

Question by:agcps
    LVL 31

    Assisted Solution

    by:Jamie McAllister MVP
    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.

    Author Comment

    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."
    LVL 31

    Accepted Solution

    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.
    LVL 5

    Assisted Solution

    Please try invoking EnsureChildControls as first method in in render method()

    Author Closing Comment

    Need to try out this

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    A question that is asked often, is how to generate sequential numbers in InfoPath Forms. The best way to achieve this is to use a SQL database, along with a stored procedure and a web service to connect Forms Services to the DB. The first thing t…
    I have just recently built a new SharePoint 2007 farm on a complete Windows 2008 R2 server platform and part of my standard build procedure is to implement a warm up routine, usually in the form of a script that is scheduled every morning to launch …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now