Skip to main content
replaced http://sharepoint.stackexchange.com/ with https://sharepoint.stackexchange.com/
Source Link
Source Link
Ali Jafer
  • 17.8k
  • 1
  • 28
  • 41

please follow this to get/set webpart properties!

Question about webpart properties

also like to note that every time you do a refresh the properties are reset ;) so what you want todo is not set anything in the ascx page but rather within the webpart itself!

any hoo as you requested (NOT the above method but sticking to (parent to ascx)):

in your visual webpart do:

public class CVWebPart: WebPart 
{   
    internal int _A; 
    private const int c_A = 1;

    public CVWebPart()
    {
        _A = c_A;
    }

    [SPWebCategoryName("who CV id")]
    [DefaultValue(c_A)]
    [Personalizable(PersonalizationScope.Shared)]
    [WebDisplayName("A")]
    [WebDescription("")]
    [WebBrowsable(true)]

    public int A
    {
        get { return _A; }
        set { _A = value; }
    }
}

then do the following:

private const string _ascxPath = @"~/_ControlTemplates/CVus.ascx";

protected override void CreateChildControls()
{ 
    try 
    { 
        CVcontrol control = (CVcontrol)Page.LoadControl(_ascxPath);
        ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(control); 

        control.ParentWebPart = this;

        Controls.Add(control);
    }
}

now in the ascx.cs add this:

public CVWebPart ParentWebPart 
{ 
    get; set; 
}

above im assuming that CVWebPart is the class name of your ascx.cs file!

now add the following in the page load for the ascx.cs file to pass the value from the parent to the ascx textbox control or whatever you want be it an internal string/varible or object like i have done below:

protected void Page_Load(object sender, EventArgs e) 
{ 
    txtMessage.Text = ParentWebPart.Message; 
}

just for clarification, within the aspx of my ascx i have the textbox as follows!

<asp:TextBox runat=”server” ID=”txtMessage” />

the above will work as requested!

as a side not i dont like this method! the reason being is ascx i belive is designed to make the seperation of code and UI (user interface). So for example say iv done all this code and made the UI in a normal webpart and then the designer decides to change all layout... that means i have to go through the code and ammend things and will be time consuming! so to get around this I would use a visual webpart so i can do the code for the backend (visual webpart with the webpart properties) this is visible to sharepoint! and then attach an ascx that i have made to the webpart! now say the desinger doesnt like somthing than you only need to update the ascx whereever its stored without needing to touch the actual webpart itself! so it takes time off me, make it easier for designer/developer to ammend UI without effecting codebehind. Those are just a few reasons why i dont like to add code to an ascx appart from the UI code and nothing else! it will save alot of hassle and code as iv shown above! all that code could be done alot less ;) !