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.