将控件转换为html

时间:2013-04-26 09:20:55

标签: c# asp.net-mvc devexpress

我需要将devexpress控件(例如复选框)“转换”为控制器中的html字符串。就像在页面上看起来一样。请告知如何做到这一点。

页:

<input name="myCheckBox" id="chB_S" style="width: 0px; height: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; position: relative; background-color: transparent;" readonly="readonly" value="I"/>

控制器:

CheckBoxSettings s = new CheckBoxSettings();
s.Name = "myCheckBox";
s.Properties.ClientSideEvents.ValueChanged = "function(s,e){alert('Selection changed');}";
CheckBoxExtension chBut = new CheckBoxExtension(s);        
string zzz = chBut.GetHtml().ToHtmlString(); //Get empty string instead of html

1 个答案:

答案 0 :(得分:0)

public static string RenderView(string path, object data)
{
    Page pageHolder = new Page();
    UserControl viewControl = (UserControl) pageHolder.LoadControl(path);

    if (data != null)
    {
        Type viewControlType = viewControl.GetType();            
        FieldInfo field = viewControlType.GetField("Data");

        if (field != null)
        {
            field.SetValue(viewControl, data);
        }
        else
        {
            throw new Exception("View file: " + path + " does not have a public Data property");
        }
    }

    pageHolder.Controls.Add(viewControl);

    StringWriter output = new StringWriter();
    HttpContext.Current.Server.Execute(pageHolder, output, false);

    return output.ToString();
}

上述方法可以与ASP.NET应用程序一起使用,将UserControl转换为HTML。您可以将其引用为创建自定义RenderView以将控件转换为Html的指南。

此外(不确定MVC)但在asp.net中,所有控件都是从WebControl派生的,所以我认为你也可以这样做。现在使用MVC控件将在以后成功更新。