2009-09-30 34 views
2

我試圖從一個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> 
+0

您是否驗證了「rblResult」返回了您認爲它所做的事情?你檢查了strAnswer的內容嗎? – 2009-09-30 11:02:36

+0

標籤部分或RadioButtonList部分是否存在錯誤?另外,「strAnswer」的值是什麼, string strAnswer = rblAnswer.SelectedValue; 它是一個可以解析成「int」的有效整數嗎? – Nasir 2009-09-30 11:10:03

+0

執行後strAnswer =一個整數1,2,3,4,5或6 – Alex 2009-09-30 12:03:17

回答

4

看到您的ASPX代碼後,有一種方法來獲取文本出來。我沒有測試過這一點,但這些方針的東西應該工作:

string strAnswer = String.Empty; 
foreach (ListItem item in rblAnswer.Items) 
{ 
    if (item.Selected) 
    { 
     strAnswer = item.Value; 
    } 
} 
+0

不允許我這樣做,甚至。文本 在調試模式中有這麼一句話: \t \t rblAnswer.SelectedValue \t「1」 \t串 我難倒了一個字符串,所以應該讓我把它轉換 – Alex 2009-09-30 11:49:44

+0

道歉我誤讀,只是嘗試:int intAnswer = Convert.ToInt32(rblAnswer.SelectedItem.Value); 但得到:系統。NullReferenceException:未將對象引用設置爲對象的實例。 – Alex 2009-09-30 12:14:59

+0

我最終使用: 的foreach(在rblAnswer.Items列表項項) { 如果(item.Selected) { strAnswer = item.Value; }} 非常感謝你的幫助:) – Alex 2009-09-30 15:05:50

0

rblAnswer.SelectedValue有什麼值?如果它不能被轉換成的Int32 ..你得到這個錯誤

所以它需要一個整數

0

你需要確定的,是價值,從這一行代碼來的唯一:

strAnswer = rblAnswer.SelectedValue; 

這是一個可以轉換的有效整數嗎?

+0

在RadioButtonList設置爲1,2,3,4,5,6值,因此只能返回這些整數: 的 非常不贊同 不同意 傾向不同意 傾向於同意 同意 非常贊同 使用調試它返回值作爲應該工作,但不會因爲原因的字符串 – Alex 2009-09-30 11:41:19

0

嘗試使用的TryParse

int.tryparse(strAnswer,出intAnswer);

如果字符串不能被轉換,這將返回false。

0

如果指定的控件不存在,FindControl將返回空引用。

foreach (GridViewRow gvrow in GridView1.Rows) 
{ 
    RadioButtonList rblAnswer = (RadioButtonList)gvrow.FindControl("rblResult"); 
    ... 
} 

如果GridView1包含不包含名爲rblResult然後rblAnswer將被設置爲空,所以你需要檢查rblAnswer不使用它之前空控制任何行。

如果沒有從RadioButtonList中選擇一個值,則SelectedValue將返回一個空字符串。所以,找到了rblResult控件,你需要檢查一個值是否已經被選中。

if(rblAnswer != null) 
{ 
    if(rblAnswer.SelectedValue != string.Empty) 
    { 
     string strAnswer = rblAnswer.SelectedValue; 
     .... 
    } 
} 
+0

想到它,那會給NullReferenceException不是一個FormatException。有一個值肯定被選中?也許需要添加一個檢查rblAnswer.SelectedValue!= string.Empty – 2009-09-30 14:36:43