Javascript provides convenience for getting UserControl content for html-javascript tips-js tutorial

Source: Internet
Author: User
Javascript cannot pass the test when obtaining the UserControl content. It originally inherited the Page and used VerifyRenderingInServerForm to verify that I read Lao Zhao's article today, but I failed to debug it.

The Code is as follows:


[AjaxPro. AjaxMethod]
Public string gethtml ()
{

UcViewHelper ViewManager = new UcViewHelper ();
UserControl control = viewManager. LoadViewControl ("~ /Uc/giftoutmodel. ascx ");
String s = viewManager. RenderView (control );

Return s;

}

Public class UcViewHelper Where T: UserControl
{
Private MyPage m_pageHolder;
Public T LoadViewControl (string path)
{
M_pageHolder = new MyPage ();
Return (T) m_pageHolder.LoadControl (path );
}
Public string RenderView (T control)
{
StringWriter output = new StringWriter ();

This. m_pageHolder.Controls.Add (control );
HttpContext. Current. Server. Execute (this. m_pageHolder, output, false );

Return output. ToString ();
}
}

Class MyPage: Page
{
Public override void VerifyRenderingInServerForm (Control control Control)
{
// If (control is GridView | control is UserControl)
//{
// Return;
//}
// Base. VerifyRenderingInServerForm (control );
}
}

Test passed.

If:

The Code is as follows:


[AjaxPro. AjaxMethod]
Public string gethtml ()
{
String s = getString ();
Return s;

}
Public string getString ()
{
UserControl control = LoadControl ("~ /Uc/giftoutmodel. ascx ") as UserControl;
StringWriter tw = new StringWriter ();
HtmlTextWriter writer = new HtmlTextWriter (tw );
Control. RenderControl (writer );
Return writer. InnerWriter. ToString ();
}

Public override void VerifyRenderingInServerForm (Control control Control)
{
// If (control is GridView | control is UserControl)
//{
// Return;
//}
// Base. VerifyRenderingInServerForm (control );
}

The reason is that Lao Zhao's code inherits the Page and uses VerifyRenderingInServerForm for verification. Second, my code does not inherit the Page and uses VerifyRenderingInServerForm directly.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.