2013-08-02 91 views
1

我有這樣一個gridview柱:在更新的字段誤差表示整數值

<asp:TemplateField HeaderText="<%$ Resources:UserProfile, C_Updated %>" ItemStyle-Wrap="false" SortExpression="Updated"> 
    <ItemTemplate> 
     <asp:Literal ID="UpdatedLiteral" runat="server" 
     Text='<%# (Eval("Updated").ToString()) == "0" ? string.Format("<span class=greenText>{0}</span>", GetGlobalResourceObject("Vacancies", "VacancyToday")) : ((int)Eval("Updated")) %>' /> 

     <asp:Literal ID="UpdateddaysLiteral" runat="server" Text='<%$ Resources:UserProfile, C_UpdatedDays %>' /> 
    </ItemTemplate> 
</asp:TemplateField> 

值爲數字0或大於0但我收到錯誤:

CS0173: Type of conditional expression cannot be determined because there is no implicit conversion between 'string' and 'int'.

此外,我只想顯示UpdateddaysLiteral更新列的值大於0.請建議如何做到這一點?

+1

你上得到這個錯誤條件的一邊你得到了字符串,你的其他部分被轉換爲int。 – Zaki

回答

0

在你的病情的最後一部分:

: ((int)Eval("Updated")) 

你爲什麼要鑄造「更新」回詮釋?只需Eval("Updated").ToString()就足夠了。你的問題的

第二部分:

<asp:Literal ID="UpdateddaysLiteral" runat="server" Visible='<%# Eval("Updated") > 0 ? "true", "false" %>' ... 
+0

我使用的 Abhitalks

0

解決方法:KISS原則,這意味着保持簡單愚蠢的。 爲什麼你在你的aspx中使事情變得複雜,當你可以完美地從代碼中完成時。

創建一個方法來對您的代碼隱藏進行比較邏輯。像這樣的東西。

/// This might not be you exact logic, but it will help you 
public string comparevalues(string value) 
{ 
    if(Convert.ToInt32(value)>0) 
    { 
     //do something 
    } 
    else 
    { 
    // do something else; 
    } 
    return result_as_string; 
} 

在你的aspx:

<asp:Literal ID="UpdatedLiteral" runat="server" 
Text='<%#comparevalues(Eval("Value_To_Evaluate").ToString() %>' /> 
1

應該有&下,在B之間只有一個隱式轉換:

var value = a?b:c 

這種轉換可以在任何方向。

這意味着,要麼b應該是隱式轉換爲c或者相反。

你的情況,你有b string和c int和有兩者之間沒有隱式轉換。這就是爲什麼這個錯誤出現。 MSDN may help.

例如,這也顯示了同樣的錯誤:

lbldate.Text= (DateTime.Parse(TextBoxActualEndDate.Text)) : null; 

所以校正:(使任何一方轉換爲其他)

lbldate.Text= (DateTime?)(DateTime.Parse(TextBoxActualEndDate.Text)) : null;