简单问题关于ViewState(100分)
view.aspx.cs文件中:
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
ItemOperation io=new ItemOperation();//自定义的一个类
//获取总投票数
ViewState["VoteCount"] = io.GetVoteCount();
}
}
public int FormatVoteCount(String itemCount)
{
int VoteCount=Int32.Parse(ViewState["VoteCount"].ToString());
return ((Int32.Parse(itemCount) * 100 /VoteCount);
}
在view.aspx文件中有控件:
<asp:datagrid id="DataGrid1" runat="server" Width="352px"
AutoGenerateColumns="False" Height="80px" DataKeyField="ItemID">
<Columns>
<asp:BoundColumn DataField="ItemName" HeaderText="投票项目">
<HeaderStyle Width="3cm"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="ItemCount" HeaderText=" 投票数">
<HeaderStyle Width="3cm"></HeaderStyle>
</asp:BoundColumn>
<asp:TemplateColumn>
<HeaderStyle Width="5cm"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:Image id=Image2 runat="server" Height="4px"
Width='<%#FormatVoteImage(FormatVoteCount(DataBinder.Eval(
Container.DataItem,"ItemCount").ToString()))%>'
ImageUrl="Images/vote.bmp" BackColor="Navy">
</asp:Image>
<%# FormatVoteCount(DataBinder.Eval(Container.DataItem, "ItemCount").ToString())%>%
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
编译时出现如下错误:
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误:
行 56:public int FormatVoteCount(String itemCount)
行 57:{
行 58: int VoteCount=Int32.Parse(ViewState["VoteCount"].ToString());
行 59: return ((Int32.Parse(itemCount) * 100 /VoteCount);
行 60:}
为什么会有这样的问题呢请高手////////////////////