星级评分的铸造问题?

问题描述:

我有阿贾克斯星级的应用程序,但是当我从数据表中指定的值CurrentRating那么它显示的错误“指定的转换无效”。星级评分的铸造问题?

我使用这个代码。

<asp:TemplateField HeaderText="Rating" SortExpression="CustomerRating"> 
        <ItemTemplate> 
        <asp:Label ID="Label2" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "CustomerRating")%>'></asp:Label></a> 
         <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
          <ContentTemplate> 
           <cc1:Rating ID="Rating1" runat="server" CurrentRating='<%# Bind("CustomerRating") %>' 
           StarCssClass="ratingStar" 
           WaitingStarCssClass="savedRatingStar" 
           FilledStarCssClass="filledRatingStar" 
           EmptyStarCssClass="emptyRatingStar" 
           > 
         </cc1:Rating> 
          </ContentTemplate> 
         </asp:UpdatePanel> 
        </ItemTemplate> 
       </asp:TemplateField> 

那么它表示错误CurrentRating='<%# Bind("CustomerRating") %>'

我从这些网站采取refrence。

asp.net forum Code Project

同样的事情上的代码项目。

问题是最有可能是您的数据项的CustomerRating属性是正确的数据类型的不行。评级期望值为int。 Databinder确实使用反射并尝试自动处理类型转换,但它有限制。

不幸的是没有足够的信息,在你的qustion知道CustomerRating的实际运行时类型,所以我不能说为什么不能投。我的建议是显式转换或转换属性,像这样:

CurrentRating='<%# (string)Bind("CustomerRating") %>' 
CurrentRating='<%# Bind("CustomerRating").ToString() %>' 
CurrentRating='<%# (int)Bind("CustomerRating") %>' 

如果您不能简单地将其转换,或者只需要得到它的一个调试器,所以你可以找出是什么类型,你可以改为在代码隐藏中调用自定义方法(并且可以在其中附加调试器,以便您可以看到该项目的运行时类型:

CurrentRating='<%# MyCustomMethod(Eval("CustomerRating")) %>' 

in code behind: 

public string MyCustomMethod(object customerRating) 
{ 
    string rValue = ... //do whatever you need to do to customerRating to get a string out of it 
    // good place to set a breakpoint you you can examine what type customerRating actually is so you can figure out how best to convert it to something databinding can use 
    return rValue; 
}