无法将 HiddenField 转换为 Int32 [英] Can not convert HiddenField to Int32

查看:35
本文介绍了无法将 HiddenField 转换为 Int32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据类型转换方面遇到了问题,我只能弄清楚

I am having a problem with data type conversion that I just can figure out

 HiddenField hfFriendId = item.FindControl("hfFriendId") as HiddenField;
 Int32 FriendId = Convert.ToInt32(hfFriendId.Value);

我需要在下面使用FriendId as和int32

I need to use FriendId as and int32 in the following

UserInfo TargetUser = UserController.GetUserById(PortalSettings.PortalId, FriendId);

因为两个参数都应该是 int32.当我调试这个TargetUser"时为空.

as both args are expected to be int32. When I debug this 'TargetUser' is null.

可能是什么问题?

推荐答案

如果您不确定隐藏字段中可用的值,则不要使用

If you are not sure of the value available in the hiddenfield then instead of using

Int32 FriendId = Convert.ToInt32(hfFriendId.Value);

试试

int FriendId = int.Parse(hfFriendId.Value); // will throw exception if parse fails

或更好

int FriendId ;
if(int.TryParse(hfFriendId.Value, out FriendId))
     //get userinfo

也可能是您的 UserController.GetUserById 方法存在问题,以防 Convert 获取解析的值并且未返回零.

Also it could be an issue with your UserController.GetUserById method in case the Convert gets the value parsed and is not returning zero.

这篇关于无法将 HiddenField 转换为 Int32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆