所以我正在创建一个简单的问题/答案格式并在提交答案时遇到问题。我通过“RenderQuestions()”函数从数据库动态创建 div/按钮/文本框。这将创建一个问题列表和答案文本框/按钮。尝试回答问题时,我输入答案并单击提交,但没有任何反应。我再做一次,它显示了我的第一个答案。这是一个“落后”..如果我刷新它然后显示所有答案。我整晚都在为此苦苦挣扎。这是一些代码:
-----My page load----- (Relevant parts)
protected void Page_Load(object sender, EventArgs e)
{
//If authenticated hide login & show welcome bloc
if (User.Identity.IsAuthenticated)
{
//Show question & render
questionsBloc.Visible = true;
//if(Page.IsPostBack)
RenderQuestions();
}
-----RenderQuestions() function---- (The relevant parts)
//Initialize & get answers
List<Answer> answers = new List<Answer>();
answers = um.GetAnswers(q.QuestionID);
//Initialize html render
HtmlGenericControl questionDiv = new HtmlGenericControl("div");
TextBox txtAnswer = new TextBox();
Button btnAnswer = new Button();
//Set Answer Button
btnAnswer.Text = "Answer";
btnAnswer.Click += new EventHandler(btnAnswer_Click);
//Set ID's
btnAnswer.ID = "btnAnswer" + q.QuestionID.ToString();
questionDiv.ID = "questionDiv" + q.QuestionID.ToString();
//Set classes
questionDiv.Attributes.Add("class", "questionBloc");
btnAnswer.CausesValidation = false;
btnAnswer.EnableViewState = false;
//btnAnswer.UseSubmitBehavior = true;
//Fill inner text with question
questionDiv.InnerText = q.QuestionContent; //Insert question..
//actionDiv.InnerText = "Like/Dislike/Comment/Flag"; //Insert answer..
//Add answer textbox and button to action div
actionDiv.Controls.Add(btnAnswer);
//Add question div to qaDiv
qaDiv.Controls.Add(questionDiv);
//Add action div to qaDiv
qaDiv.Controls.Add(actionDiv);
//Add all controls to feedbloc
feedBloc.Controls.Add(qaDiv);
-----My btnAnswer event handler -----
private void btnAnswer_Click(object sender, EventArgs e)
{
UserManager um = new UserManager();
um.PostAnswer("My first answer!");
//RenderGlobalFeed();
}
这就是对我的按钮的所有引用。我应该在 page_init 中初始化 btn 单击事件吗?任何帮助深表感谢。
多谢你们