我試圖從一個GridView行提取ID,但我不斷收到錯誤:充分利用選定的項目在RadioButtonList的文本在GridView的ASP.NET
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代碼片段:
<asp:RadioButtonList ID="rblResult" runat="server" Width="200px">
<asp:ListItem Value="1">Strongly Disagree</asp:ListItem>
<asp:ListItem Value="2">Disagree</asp:ListItem>
<asp:ListItem Value="3">Tend to Disagree</asp:ListItem>
<asp:ListItem Value="4">Tend to Agree</asp:ListItem>
<asp:ListItem Value="5">Agree</asp:ListItem>
<asp:ListItem Value="6">Strongly Agree</asp:ListItem>
</asp:RadioButtonList>
您是否驗證了「rblResult」返回了您認爲它所做的事情?你檢查了strAnswer的內容嗎? – 2009-09-30 11:02:36
標籤部分或RadioButtonList部分是否存在錯誤?另外,「strAnswer」的值是什麼, string strAnswer = rblAnswer.SelectedValue; 它是一個可以解析成「int」的有效整數嗎? – Nasir 2009-09-30 11:10:03
執行後strAnswer =一個整數1,2,3,4,5或6 – Alex 2009-09-30 12:03:17