C# 获取asp.net repeater中HiddenField的值
当我尝试在中继器内部使用隐藏字段时C# 获取asp.net repeater中HiddenField的值,c#,asp.net,repeater,C#,Asp.net,Repeater,当我尝试在中继器内部使用隐藏字段时 <repeater> <repeater> <hiddenfield> 为什么会出现错误?看起来hf为空,因为您试图使用temp.FindControl而不是temp.Items[count].FindControl来查找HiddenField 更改以下内容 DropDownList ds = (DropDownList)temp.FindControl("userdropdown"); HiddenFie
<repeater>
<repeater>
<hiddenfield>
为什么会出现错误?看起来
hf
为空,因为您试图使用temp.FindControl
而不是temp.Items[count].FindControl来查找HiddenField
更改以下内容
DropDownList ds = (DropDownList)temp.FindControl("userdropdown");
HiddenField hf = (HiddenField)temp.FindControl("hiddenid");
对此
DropDownList ds = (DropDownList)temp.Items[count].FindControl("userdropdown");
HiddenField hf = (HiddenField)temp.Items[count].FindControl("hiddenid");
更新
如果ds.SelectedValue
为空字符串或包含非数字字符,此行将引发错误
user.user_id = int.Parse(ds.SelectedValue);
你需要把它改成这个
int userID = 0;
if (int.TryParse(ds.SelectedValue, out userID))
{
user.user_id = userID;
}
else
{
// do something when ds.SelectedValue is non numeric
}
试试这个
foreach (RepeaterItem items in temp.Items)
{
DropDownList ds = (DropDownList)items.FindControl("userdropdown");
HiddenField hf = (HiddenField)items.FindControl("hiddenid");
//your code
}
亲爱的,你们们有嵌套的Repeter,若你们们的意思是向repeater添加一个数据源,当然我有,但并没有提及,因为这是一个很长的查询,但它是有效的。。。但现在我得到了这个错误:System.FormatException:输入字符串的格式不正确。行内:user.user\u id=int.Parse(ds.SelectedValue);但是用户id是int,以确定ds的值是多少。SelectedValue
?如果是空字符串,则int.Parse
将抛出错误。
user.user_id = int.Parse(ds.SelectedValue);
int userID = 0;
if (int.TryParse(ds.SelectedValue, out userID))
{
user.user_id = userID;
}
else
{
// do something when ds.SelectedValue is non numeric
}
foreach (RepeaterItem items in temp.Items)
{
DropDownList ds = (DropDownList)items.FindControl("userdropdown");
HiddenField hf = (HiddenField)items.FindControl("hiddenid");
//your code
}