2014-02-25 100 views
0

无法将文本从文本框中取出并放入我的List<T>。帮助将不胜感激。为什么不把文本框中的文本保存到我的列表中?

foreach (RepeaterItem ri in rptQuestionnaireQuestions.Items) 
{ 
    TextBox txtResponse = (TextBox)ri.FindControl("txtResponse"); 

    if (txtResponse != null) 
    { 
     responses.Add(new QuestionnaireUserAnswer() 
     { 
      questionId = questionId, 
      answerId = 5, 
      freeText = txtResponse.Text 
     }); 
    } 
} 
+0

会发生什么?你是否输入'if'语句? – Jonesopolis

+0

是的,我输入'if'语句并添加questionId和answerId;没有文字,但! –

+1

您在哪种方法中运行上述语句?动态生成的控件在回发期间/之后不保留其内容;我遇到过类似情况的问题,并没有太多警告。 – Flater

回答

0

问题:你只检查羯羊TextBox控件通过检查空可用,但你是不是cecking空文本

解决方案:可以在TextBox检查EmptyText控制使用String.IsNullOrWhiteSpace()方法

尝试:

if (txtResponse != null && !String.IsNullOrWhiteSpace(txtResponse.Text)) 
{ 
    responses.Add(new QuestionnaireUserAnswer() 
    { 
     questionId = questionId, 
     answerId = 5, 
     freeText = txtResponse.Text 
    }); 
} 
+0

谢谢你的回复。问题实际上是'如果(!Page.IsPostBack)'从'Page_Load'丢失。 –

0

尝试

if(Srting.IsNullorEmpty("Your string text")) 
{ 
//code here 
} 
相关问题