2013-04-26 54 views
0

我需要將devexpress控件(例如複選框)「轉換」爲控制器中的html字符串。喜歡它看起來在頁面上。請告知如何去做。將控制轉換爲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 

回答

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應用程序使用以用戶控件轉換爲HTML。您可以將其引用爲創建自定義RenderView以將控件轉換爲Html的指南。

此外(不確定關於MVC),但在asp.net中,所有控件都來自WebControls,所以我認爲你也可以做同樣的事情。現在使用MVC控件會在成功的情況下更新。