从ASP.NET中的GridView中的RadioButtonList中的选定项中获取文本
我正在尝试从gridview行中提取ID,但我不断收到错误:
System.FormatException: Input string was not in a correct format'.
代码片段:
foreach (GridViewRow gvrow in GridView1.Rows) { RadioButtonList rblAnswer = (RadioButtonList)gvrow.FindControl("rblResult"); string strAnswer = rblAnswer.SelectedValue; int intAnswer = Convert.ToInt32(strAnswer); //error with this line Label lblQuestionID = (Label)gvrow.FindControl("lblID"); string strID = lblQuestionID.Text; int intID = Convert.ToInt32(strID); Package.Sql.Sql.saveAnswers(Convert.ToInt32(Session["userID"]), intID, intAnswer); }
ASP.NET代码段:
Strongly Disagree Disagree Tend to Disagree Tend to Agree Agree Strongly Agree
看到你的ASPX代码后,有一种方法可以将文本输出。 我没有对此进行过测试,但这些方面应该有效:
string strAnswer = String.Empty; foreach (ListItem item in rblAnswer.Items) { if (item.Selected) { strAnswer = item.Value; } }
rblAnswer.SelectedValue有什么价值? 如果它不能转换为Int32 ..你得到那个错误
所以它需要是一个整数
你唯一需要确定的是来自这行代码的价值:
strAnswer = rblAnswer.SelectedValue;
这是一个可以转换的有效整数吗?
尝试使用tryparse
int.tryparse(strAnswer,out intAnswer);
如果无法转换字符串,则返回false。
如果指定的控件不存在,FindControl将返回空引用。
foreach (GridViewRow gvrow in GridView1.Rows) { RadioButtonList rblAnswer = (RadioButtonList)gvrow.FindControl("rblResult"); ... }
如果GridView1包含任何不包含名为rblResult的控件的行,则rblAnswer将设置为null,因此您需要在使用它之前检查rblAnswer是否为null。
如果尚未从RadioButtonList中选择值,则SelectedValue将返回空字符串。 因此,找到rblResult控件后,您需要检查是否实际选择了一个值。
上述就是C#学习教程:从ASP.NET中的GridView中的RadioButtonList中的选定项中获取文本分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
if(rblAnswer != null) { if(rblAnswer.SelectedValue != string.Empty) { string strAnswer = rblAnswer.SelectedValue; .... } }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1022467.html