铸造星级评定问题?

时间:2009-12-09 21:07:54

标签: asp.net ajax

我有一个具有ajax星级的应用程序,但是当我从datatable为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") %>'

我正在接受这些网站的评价。

asp.net forum Code Project

同样适用于Code项目。

1 个答案:

答案 0 :(得分: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;
}
相关问题