Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

.net

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("New 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?

like image 570
Philly Avatar asked May 26 '10 14:05

Philly


2 Answers

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.

like image 99
AxelEckenberger Avatar answered Oct 22 '22 14:10

AxelEckenberger


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");
like image 45
Fredrik Mörk Avatar answered Oct 22 '22 14:10

Fredrik Mörk