开发者

Cannot implicitly convert type 'string' to 'System.Web.UI.WebControls.Unit'

I ma getting error like Cannot implicitly convert type 'string' to 'System.Web.UI.WebControls.Unit' in the following code. How to fix this.

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        RadTab tab = new RadTab();
        tab.Text = string.Format("N开发者_StackOverflow社区ew Page {0}", 1);
        RadTabStrip1.Tabs.Add(tab);

        RadPageView pageView = new RadPageView();
        pageView.Height = "100px";
        RadMultiPage1.PageViews.Add(pageView);

        BuildPageViewContents(pageView, RadTabStrip1.Tabs.Count);
        RadTabStrip1.SelectedIndex = 0;
        RadTabStrip1.DataBind();

    }
}

Here I am getting error. pageView.Height = "100px";

How to fix this?


Because Height is not of type string but of type UnitSystem.Web.UI.WebControls.Unitenter code here.

You can use the following static methods to convert to Unit:

  • Unit.Pixel(100); // 100 px
  • Unit.Percent(10); // 10 %
  • Unit.Point(100); // 100 pt
  • Unit.Parse("100px"); // 100 px

The Unit structure does not have an explicit or implicit conversion from string, therefore, the error you are observing occurs.


The error message says it all. You need to convert the value to a System.Web.UI.WebControls.Unit in a more specific manner. Luckliy, the Unit type has a constructor with this ability:

pageView.Height = new System.Web.UI.WebControls.Unit("100px");


Change

pageView.Height = "100px";

to

pageView.Height = new Unit(100);

Height is of type Unit, so you need to assign a value to it that is also of type Unit. To make an object of type Unit you need to call Unit's constructor with new; one of the constructors accepts as a parameter the number of pixels that the Unit is to represent.


The Height on the control is of type Unit. You want to use

pageView.Height = Unit.Pixel(100);


This this MSDN doc on how to use Units. In your case:

pageView.Height = new Unit("100px");


Replace "100px"; with

new System.Web.UI.WebControls.Unit("100px");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜