开发者

casting problem of star rating?

开发者 https://www.devze.com 2022-12-14 02:24 出处:网络
I have an application with ajax star rating but when i am assigning value to CurrentRating from datatable then it showing error of \"Specified cast is n开发者_如何学JAVAot valid\".

I have an application with ajax star rating but when i am assigning value to CurrentRating from datatable then it showing error of "Specified cast is n开发者_如何学JAVAot valid".

I am using this code.

<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>

Then its showing error CurrentRating='<%# Bind("CustomerRating") %>'.

I am taking refrence from these sites.

asp.net forum Code Project

Same thing working on Code project.


The problem is most likely that the CustomerRating property of your data item is not of the correct data type. Rating expects an int. The Databinder does use reflection and attempts to automatically handle type conversions, but it has limits.

Unfortunatly there isn't enough information in your qustion to know what the actual runtime type of CustomerRating is, so I can't say why it can't be cast. My advise would be to explicitly cast or convert the property like so:

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

If you can't convert it simply, or just need to get a debugger on it so you can figure out what the type is you can call out to a custom method in your code-behind instead (and you can attach a debugger there to so you can see the runtime type of the item:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号