2009-12-09 118 views
0

我有阿賈克斯星級的應用程序,但是當我從數據表中指定的值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

同樣的事情上的代碼項目。

回答

0

問題是最有可能是您的數據項的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; 
}