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

I have a webpart that is loading a user control, but I am not able to get the value of a webpart property in the loaded user control.

The web part code goes like this:

public class CVWebPart: WebPart
{
    internal int _A;
    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; }
    }
}

Create child control for webpart here is :

protected override void CreateChildControls()
{
    try
    {
        CVcontrol = (UserControl)Page.LoadControl(@"~/_ControlTemplates/CVus.ascx");
        ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(control);
        CVcontrol.Attributes.Add("A", A.ToString());
        Controls.Add(CVcontrol);
    }
    catch {}
}

In my user control a is always "1"

public partial class CVuc: UserControl
{   
    this.Attributes["A"].ToString();  
}

It is always 1 when i change value from webpart property nothins change

share|improve this question
add comment

3 Answers 3

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 ;) !

share|improve this answer
    
control.ParentWebPart = this; has no ParentWebPart in it Sir –  Customized Name May 3 '13 at 12:21
    
you need to add the ascx to your visual webpart! the dll i mean as reference and then you can call public CVWebPart ParentWebPart { get; set; } –  ali Sharepoint May 3 '13 at 12:29
    
ok im confused! do you want to get the values from user control into webpart OR do you want to get values from webpart into user control? –  ali Sharepoint May 3 '13 at 12:31
    
webpart to user control ; –  Customized Name May 3 '13 at 13:22
    
no work still sir –  Customized Name May 7 '13 at 8:55
show 2 more comments

Try that code for your Web Part:

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

    public CVWebPart() {}

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

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

I think the reason of described behavior is "_A = c_A;" in Web Part's constructor.

Setting "[DefaultValue(c_A)]" is enough to set default value.

share|improve this answer
    
says no object to reference prodvided –  Customized Name May 3 '13 at 12:10
add comment

i use this method to sovle my issue : thanks you all for help

http://msdn.microsoft.com/en-us/library/ff649867.aspx

try
{
    // Loads a user control
    MyUserControl myUserControl = (MyUserControl)Page.LoadControl("~/_controltemplates/MyWebPart/MyUserControl.ascx");
    myUserControl.Web = SPContext.Current.Web;
    myUserControl.TextColor = this.TextColor;

    // Adds it to the controls collection of the Web Part 
    this.Controls.Add(myUserControl);
}
catch ( HttpException ex )
{
    this.Controls.Add(new LiteralControl("<br />An unexpected error occurred loading Web Part. " + ex.Message));
}
share|improve this answer
add comment

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.