0

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?

0

3 Answers 3

1

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";

4
  • Hi, I tried your solution. The "FindControl" is helpful. The Label is found, but then how can I modify (set properties)?
    – grekko
    Commented Dec 5, 2012 at 15:27
  • Updated the answer! Commented Dec 5, 2012 at 15:31
  • lol iv noticed that ;) hay i cant complain looool , as you are right on the ball iv put you up a point ;)
    – Ali Jafer
    Commented Dec 5, 2012 at 16:05
  • Oh, guys, sorry!!! I thought it is possible to mark 2 of the answers as answered. So I first accepted Arsalan's Answer and then of almostSharepointMaster. The marked answer switched to the other. Sorry. and thank you for your help.
    – grekko
    Commented Dec 5, 2012 at 16:42
1

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

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 :)

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.