0
public partial class VisualWebPart1UserControl : UserControl
{
    public string SetId{ get; set; }

    protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {
         SetId = ddl.SelectedItem.Value;
    }

}
WebPart Implementation (VisualWebPart1.cs)

public class VisualWebPart1 : WebPart
{
    private const string _ascxPath = @"~/_CONTROLTEMPLATES/mypath/VisualWebPart1/VisualWebPart1UserControl.ascx";

    protected override void CreateChildControls()
    {
        //generated by visual studio
        //Control control = Page.LoadControl(_ascxPath);
        //Controls.Add(control);

        var control = (VisualWebPart1UserControl) Page.LoadControl(_ascxPath);

        if (!string.IsNullOrEmpty(control.SetId)) //<<<NULL 
        {

        }

    }
}

UPDATED:

Below code is what I have in my usercontrol.ascx page and my question is how can I access the dropdownlist or textbox from outside the page. which is: class1.cs

  <tr>
            <td>
                <fieldset title="Configured Tabs">
                    <legend>Configured Tabs</legend>
                    <asp:Panel ID="panelConfiguredTabs" runat="server">
                        <table cellpadding="5px" cellspacing="5px">
                            <tr style="padding-top: 5px; padding-bottom: 5px">
                                <td>
                                    <asp:Label runat="server" ID="labelConfiguredTabs" Text="Tab list:"></asp:Label>
                                </td>
                                <td colspan="2" align="right">
                                    <asp:DropDownList ID="dropDownConfiguredTabs" runat="server" EnableViewState="true"
                                        AutoPostBack="true" OnTextChanged="dropDownConfiguredTabs_OnTextChanged">
                                        <asp:ListItem Text="Select one" Value="Select one"></asp:ListItem>
                                    </asp:DropDownList>
                                </td>
                            </tr>
                            <tr style="padding-top: 5px">
                                <td align="center">
                                    <asp:Button runat="server" ID="buttonAddNewTab" Text="Add New" OnClick="ButtonAddNewTab_OnClick"
                                        CausesValidation="false" />
                                </td>
                                <td align="center">
                                    <asp:Button runat="server" ID="buttonEditTab" Text="Edit" OnClick="ButtonEditTab_OnClick"
                                        CausesValidation="false" />
                                </td>
                                <td align="center">
                                    <asp:Button runat="server" ID="buttonDeleteTab" Text="Delete" OnClick="ButtonDeleteTab_OnClick"
                                        CausesValidation="false" />
                                </td>
                            </tr>
                        </table>
                    </asp:Panel>
                </fieldset>
            </td>
        </tr>

I am working on the custom web part bit advance topic.

I have a created visual web part (creates usercontrol.ascx and .cs file)

So on the root I have a class and I am trying to access the prop/method of usercontrol.ascx, how can I do that?

2
  • Can you provide some code? I'm not sure what you're asking Commented Oct 12, 2011 at 2:31
  • i have updated my question... let me know if you still have question
    – Nick Kahn
    Commented Oct 12, 2011 at 2:47

1 Answer 1

1

From what i understand, you want to access a property/method from your UserControl in your WebPart? If so, just make them public!

UserControl Implementation (VisualWebPartControl1.cs):

public partial class VisualWebPart1UserControl : UserControl
{
    public string Something { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    public void DoThisAndThat()
    {
        //run some code
    }
}

WebPart Implementation (VisualWebPart1.cs)

public class VisualWebPart1 : WebPart
{
    private const string _ascxPath = @"~/_CONTROLTEMPLATES/mypath/VisualWebPart1/VisualWebPart1UserControl.ascx";

    protected override void CreateChildControls()
    {
        //generated by visual studio
        //Control control = Page.LoadControl(_ascxPath);
        //Controls.Add(control);

        var control = (VisualWebPart1UserControl) Page.LoadControl(_ascxPath);
        control.Something = "its working!";
        control.DoThisAndThat();
    }
}

hope this helps

3
  • i have updated my question.. i am getting null value.
    – Nick Kahn
    Commented Oct 12, 2011 at 15:09
  • i think its overriding the value when the usercontrol loads?
    – Nick Kahn
    Commented Oct 12, 2011 at 15:10
  • Are you getting null or empty? You check for IsNullOrEmpty which returns true if a string has no value.
    – James Love
    Commented Dec 11, 2011 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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