PrepAway - Latest Free Exam Questions & Answers

Which code segment should you use?

You create a Web page named TestPage.aspx and a user control named contained in a file named TestUserControl.ascx.
You need to dynamically add TestUserControl.ascx to TestPage.aspx.
Which code segment should you use?

PrepAway - Latest Free Exam Questions & Answers

A.
protected void Page_Load(object sender, EventArgs e)
{
Control userControl = Page.LoadControl(“TestUserControl.ascx”);
Page.Form.Controls.Add(userControl);
}

B.
protected void Page_Load(object sender, EventArgs e)
{
Control userControl = Page.FindControl(“TestUserControl.ascx”);
Page.Form.Controls.Load(userControl);
}

C.
protected void Page_PreInit(object sender, EventArgs e)
{
Control userControl = Page.LoadControl(“TestUserControl.ascx”);
Page.Form.Controls.Add(userControl);
}

D.
protected void Page_PreInit(object sender, EventArgs e)
{
Control userControl = Page.FindControl(“TestUserControl.ascx”);
Page.Form.Controls.Load(userControl);
}

Explanation:
Dynamic controls should be added in preinit event for them to participate in view state. Check description of preinit and init events on page-105 of Microsoft Press book MCTS 70-515.

One Comment on “Which code segment should you use?

  1. pDDDD says:

    Ans:A

    protected void Page_PreInit(object sender, EventArgs e)
    {
    Control userControl = Page.LoadControl(“TestUserControl.ascx”);
    Page.Form.Controls.Add(userControl);
    }
    It throw the Null Referance exception




    0



    0

Leave a Reply