简体   繁体   English

如何呈现WebUserControl客户端

[英]How to render a WebUserControl client-side

I created a web user control which has some ASP.NET and third-party controls. 我创建了一个Web用户控件,其中包含一些ASP.NET和第三方控件。

I've already tested my control in the markup of a page and it works well. 我已经在页面标记中测试了控件,并且效果很好。

<APP:MyControl ID="myControl1" InitializeWithValue="Employee" />
<APP:MyControl ID="myControl2" InitializeWithValue="Supplier" />

Now, I want to render this control in the same page but not when the page renders at the first time. 现在,我想在同一页面中呈现此控件,而不是在第一次呈现页面时呈现。 In fact, I will click a button which will call a callback and in that moment I want to render the control. 实际上,我将单击一个按钮,该按钮将调用回调,并且在那一刻我想呈现控件。

I was trying to do something like this. 我正在尝试做这样的事情。

void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
{
   MyControl c1 = new MyControl();
   c1.InitializeWithValue = Person.Enum.Employee; //<--CRASH

   System.Text.StringBuilder sb = new System.Text.StringBuilder();
   System.IO.StringWriter sw = new System.IO.StringWriter(sb);
   HtmlTextWriter hw = new HtmlTextWriter(sw);
   c1.RenderControl(hw);
   return sb.ToString();
}

Then on my client-side receiver 然后在我的客户端接收器上

<a href="javascript:void(0);" onclick="testcallback('', '')">Test</a>
<div id="divControls"></div>

<script>
    function receiver(arguments, context) {
        document.getElementById('divControls').innerHTML = arguments;
    }
</script>

But it crash at the moment of the initialization because all the inner controls of the WebUserControl are null. 但是它在初始化时崩溃,因为WebUserControl的所有内部控件都为空。 They have not been initialized. 它们尚未初始化。

This is the property of the control 这是控件的属性

   [Browsable(true)]
   public Person.Enum InitializeWithValue
   {
      get { return this.enumPersonValue; }
      set
      {
         this.enumPersonValue = value;
         //Literal control
         this.litPersonType.Text = "Employee"; //<-- CRASH because litPersonType is null.
      }
   }

Some can help me to correct this or suggest me another way render a web control but client-side? 有些可以帮助我更正此问题,或者建议我使用另一种方式呈现Web控件(但客户端)?

UserControls usually are tighted to the Page Life Cycle, so you would need to recreate a whole page and render it instead. UserControl通常与页面生命周期紧密相关,因此您需要重新创建整个页面并呈现它。

Second, you should load your usercontrol using its .ascx file and not only its class, this is required because only your class is not enough. 其次,您应该使用.ascx文件而不是仅使用类加载用户控件,这是必需的,因为仅您的类是不够的。

You can still try to do like you was doing only creating but using Page.LoadControl : 您仍然可以尝试像只创建但使用Page.LoadControl

void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
{
   MyControl c1 = (MyControl)page.LoadControl("/Controls/myControl.ascx");
   c1.InitializeWithValue = Person.Enum.Employee; //<--CRASH

   System.Text.StringBuilder sb = new System.Text.StringBuilder();
   System.IO.StringWriter sw = new System.IO.StringWriter(sb);
   HtmlTextWriter hw = new HtmlTextWriter(sw);
   c1.RenderControl(hw);
   return sb.ToString();
}

But if your control has Events related to the Page Life Cycle you might need to create a whole context to it: 但是,如果您的控件具有与页面生命周期相关的事件,则可能需要为其创建一个整体上下文:

You can do it all like this: 您可以这样做:

using (var stringWriter = new StringWriter())
{
       using (var page = new Page())
       {
            MyControl myControl = (MyControl)page.LoadControl("/Controls/myControl.ascx");

            var head = new HtmlHead();
            page.Controls.Add(head);

            var form = new HtmlForm();
            page.Controls.Add(form);

            var div = new HtmlGenericControl("div");
            div.ID = "myControlContent";
            form.Controls.Add(div);

            div.Controls.Add(myControl);

            HttpContext.Current.Server.Execute(page, stringWriter, false);

        }
        return stringWriter.ToString()
}

Then in your JS you can parse the whole html and get only the innerHTML of div#myControlContent , for example: 然后,在您的JS中,您可以解析整个html并仅获取div#myControlContent的innerHTML,例如:

function receiver(arguments, context) {
    var parser = new DOMParser();
    var doc = parser.parseFromString(arguments, "text/html");

    document.getElementById('divControls').innerHTML = doc.getElementById('myControlContent').innerHTML;
}

note that DOMParser , is available only in mordern browsers, but you can find other approachs for older browsers... 请注意, DOMParser仅在现代浏览器中可用,但是您可以找到针对较旧浏览器的其他方法...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM