简体   繁体   中英

Cannot create an object of type 'System.Int32' from its string representation '3.2' for the 'CurrentRating' property

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

get me error:

Cannot create an object of type 'System.Int32' from its string representation '3.2' for the 'CurrentRating' property.

C# code:

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));
        }

"3.2" isn't an int value, but a double. Try changing the rating_Changed logic to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM