Converting value of an Eval from int to string

给你一囗甜甜゛ 提交于 2019-12-01 02:43:53

问题


I have an integer stored in my database that I need to convert string.

This is my attempt at the Eval:

<%# ChangeSalaryType(Eval("SalaryType")) %>

This is my attempt at the function:

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

But I am getting these errors:

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

I have used "SalaryType" in the Eval as that is the parameter that has the information from the database in. I'm not completetly sure what I am doing wrong..


回答1:


Even though you know that the SalaryType field will be an int in your aspx it will be cast as object after the Eval(). Do an explicit cast like so:

<%# ChangeSalaryType(Convert.ToInt32(Eval("SalaryType"))) %>



回答2:


Try to convert to int on your Eval part;

Convert.ToInt32(Eval("SalaryType"))

Eval probably return object as a return type.




回答3:


I prefer to send the DataItem on code behind, and there get the data as:

<%#RenderSalaryType(Container.DataItem)%>

and on code behind

protected string RenderSalaryType(object oItem)
{
    int salaryType = (int)DataBinder.Eval(oItem, "SalaryType");

    // rest of your code
}


来源:https://stackoverflow.com/questions/15864001/converting-value-of-an-eval-from-int-to-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!