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 make a Webpart and want to use the controls from the VisualWebpart1UserControl.ascx in the VisualWebpart1.cs. Is this possible?

I wrote following code in the VisualWebpart1.cs:

VisualWebPart1UserControl control = (VisualWebPart1UserControl)Page.LoadControl(_ascxPath);
        control.WebPart = this;
        Controls.Add(control);

I want to manipulate the controls like Label1 in the VisualWebpart.cs, which are created in the VisualWebpart1UserControl.ascx.

How can I do this?

share|improve this question
    
thanks for your answers. I will check out. –  grekko Dec 5 '12 at 15:03
add comment

3 Answers 3

up vote 1 down vote accepted

You can use control.FindControl("labelId") to access the control...

You will need to type cast it to Label and set properties..

Alternative solution! :)

UPDATE:
Once the label is found, cast it to Label class as below:
Label lblMyLabel = control.FindControl("labelId") as Label;

then lblMyLabel.Text = "your text";

share|improve this answer
    
Hi, I tried your solution. The "FindControl" is helpful. The Label is found, but then how can I modify (set properties)? –  grekko Dec 5 '12 at 15:27
    
Updated the answer! –  Arsalan Adam Khatri Dec 5 '12 at 15:31
    
thank you! It works! –  grekko Dec 5 '12 at 15:38
    
It works comment on my Answer and accepted someone else ANSWER :@ hehe... volla ! –  Arsalan Adam Khatri Dec 5 '12 at 15:43
    
lol iv noticed that ;) hay i cant complain looool , as you are right on the ball iv put you up a point ;) –  ali Sharepoint Dec 5 '12 at 16:05
show 2 more comments

If you expose the control as a member of VisualWebPart1UserControl you can access it via the control's object. Something like this:

In VisualWebPart1UserControl.ascx.cs:

public Label Label1;

In VisualWebPart1.cs:

VisualWebPart1UserControl control = (VisualWebPart1UserControl)Page.LoadControl(_ascxPath);
if (control != null)
{
    control.Label1.Text = "Change the text";
    control.WebPart = this;
}
Controls.Add(control);
share|improve this answer
add comment

Yup this is easy and very explainable ;):

in your createchildcontrol do the following:

    private UserControl control;

    protected override void CreateChildControls()
    {
        try
        {

            base.CreateChildControls();

            //call the ascx and load it into a new usercontrol so you can use it
            control = (UserControl)Page.LoadControl(@"/_controltemplates/yourascxtofindgoeshere.ascx");

            //create a button and get the control from the usercontrol (btnSave is the button id in the ascx)
            Button btnSave = (Button)this.control.FindControl("btnSave");
            //If you have a textbox you can do the same
            TextBox txtName = (TextBox)this.control.FindControl("txtName");
            //now you have created the button that is inherited from the ascx you can give it a click event ;) and do what you want in there
            btnSave.Click += new EventHandler(btnSave_Click);

            //add the user control to the visualwebpart controls list so its visible
            Controls.Add(control);

        }
        catch (HttpException ex)
        {
            this.Controls.Add(new LiteralControl("<br />An unexpected error occurred loading Web Part. " + ex.Message));
        }
    }

    void btnSave_Click(object sender, EventArgs e)
    {
       //do what you want in here ;)
    }

The above code is just an example of how a visual webpart works. I have shown you how to get the ascx into the visualwebpart, then create new controls that inherit the ascx controls. Now you have created these new controls you can do what you like ;)

hope it helps :)

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.