十进制到字符串转换 [英] Decimal to String Conversion
问题描述
标签lblTotal =(标签)e.Row.FindControl(lbltotal);
total = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem,AchievedPer));
string stotal = Convert.ToDecimal(total).ToString();
lblTotal.Text = stotal.ToString(); //< ---出现错误这个声明
如何以最佳方式输入演员?
< blockquote>试试这个.. :)
标签lblTotal =(标签)e.Row.FindControl( lbltotal);
total = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, AchievedPer));
decimal stotal = Convert.ToDecimal(total);
lblTotal.Text = Convert.ToString(stotal);
简单:它在行中找不到名为lbltotal的控件,因此返回null
value。
当您尝试设置null值的Text属性时,它会抛出需要对象引用异常。
所以看看你的行,找出它为什么没有lbltotal。有很多可能的原因,但我们看不到你的代码,所以我们无法告诉你。
首先使用调试器并在行后立即查看e.Row :
标签lblTotal =(标签)e.Row.FindControl( lbltotal);
您需要知道它包含的内容,以便您可以查看它包含的内容!
Label lblTotal=(Label)e.Row.FindControl("lbltotal");
total = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "AchievedPer"));
string stotal = Convert.ToDecimal(total).ToString() ;
lblTotal.Text =stotal.ToString();//<---Error occurs in this statement
How to type cast in the best way?
try this.. :)
Label lblTotal = (Label)e.Row.FindControl("lbltotal"); total = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "AchievedPer")); decimal stotal = Convert.ToDecimal(total); lblTotal.Text = Convert.ToString(stotal);
Simple: it can't find a control called "lbltotal" in the row, so it returns anull
value.
When you try to set the Text property of a null value it throws a "Object reference is required" exception.
So look at your row, and work out why it doesn't have "lbltotal". There are so many possible reasons why, but we can't see your code, so we can't tell you.
Start by using the debugger and looking at e.Row in detail immediately after the line:
Label lblTotal=(Label)e.Row.FindControl("lbltotal");You need to know what it does contain, so you can look at what you thought it contained!
这篇关于十进制到字符串转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!