我的数据库中存储了一个整数,我需要将其转换为字符串。
这是我对 Eval 的尝试:
<%# ChangeSalaryType(Eval("SalaryType")) %>
这是我对函数的尝试:
public static string ChangeSalaryType(int salaryType)
{
string salaryTime = string.Empty;
if (salaryType == 1)
{
salaryTime = "per hour";
}
else if (salaryType == 2)
{
salaryTime = "per week";
}
else if (salaryType == 3)
{
salaryTime = "per annum";
}
return salaryTime;
}
但是我收到了这些错误:
Argument 1: cannot convert from 'object' to 'int'
Error 2 The best overloaded method match for 'controls_allPlacements.ChangeSalaryType(int)' has some invalid arguments
Error 4 The best overloaded method match for 'controls_allPlacements.ChangeSalaryType(int)' has some invalid arguments
我在 Eval 中使用了“SalaryType”,因为它是包含数据库信息的参数。我不完全确定我做错了什么。
最佳答案
即使您知道 SalaryType
字段在您的 aspx 中将是一个 int
,它也会在 Eval() 之后被转换为 object
.像这样进行显式转换:
<%# ChangeSalaryType(Convert.ToInt32(Eval("SalaryType"))) %>
关于c# - 将 Eval 的值从 int 转换为 string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15864001/