无法从“CurrentRating”属性的字符串表示形式“3.2”创建“System.Int32”类型的对象

时间:2011-02-15 20:46:22

标签: c# ajaxcontroltoolkit

<ajax:Rating ID="rating" runat="server" MaxRating="5" CurrentRating="3.2" 
CssClass="rstar" StarCssClass="ritem" WaitingStarCssClass="svd" 
FilledStarCssClass="fld" EmptyStarCssClass="empt"  AutoPostBack="True" 

误会我:

  

无法创建类型的对象   来自其字符串的'System.Int32'   代表'3.2'为   'CurrentRating'属性。

C#代码:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Rating(rating.CurrentRating);
            }
          }

 private void Rating(double value)
        {
            Label1.Text = "Selected value  " + EvalRating(value, rating.MaxRating, rt_min, rt_max);
        }

        private static string EvalRating(double value, int maxvalue, int minrange, int maxrange)
        {
            int stepDelta = (minrange == 0) ? 1 : 0;
            double delta = (double)(maxrange - minrange) / (maxvalue - 1);
            double result = delta * value - delta * stepDelta;
            return FormatRes(result);
        }

        private static string FormatRes(double value)
        {
            return String.Format("{0:g}", value);
        }

        protected void rating_Changed(object sender, AjaxControlToolkit.RatingEventArgs e)
        {
            Rating(int.Parse(e.Value));
        }

1 个答案:

答案 0 :(得分:4)

“3.2”不是int值,而是double。尝试将rating_Changed逻辑更改为:

Rating(double.Parse(e.Value));