我在ListView中获取动态创建的控件的输入值时遇到问题.
这是我的ListView:
<asp:ListView ID="lvQuestions" runat="server" DataKeyNames="ProductQuestionId" onitemdatabound="lvQuestions_ItemDataBound"> <LayoutTemplate> <table> <tr runat="server" id="itemPlaceholder"></tr> </table> </LayoutTemplate> <ItemTemplate> <tr> <td><%# Eval("Question") %></td> <td> <asp:PlaceHolder ID="plControl" runat="server" /> <asp:HiddenField ID="hfQuestionId" runat="server" /> </td> </tr> </ItemTemplate> </asp:ListView> <asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
在我的ItemDataBound处理程序中,我将一个TextBox或其他控件添加到占位符.控件类型取决于项目,但为了保持简单,我们假设它始终是一个文本框.控件的ID也是动态的.
// create a textBox control TextBox txtbx = new TextBox(); txtbx.ID = "txtQuestion_" + productQuestionId.ToString(); //productQuestionId is the datakey value of this ListViewItem placeholder.Controls.Add(txtbx);
当用户点击按钮时,我需要能够获得他们填写的值.
在我的研究中,我发现我需要首先重新创建动态添加的控件,以便根据页面生命周期获取它们的值.
以下是我在按钮处理程序中重新创建控件的内容:
foreach (ListViewDataItem item in lvQuestions.Items) { HiddenField hdField = (HiddenField)item.FindControl("hfQuestionId"); PlaceHolder plcHolder = (PlaceHolder)item.FindControl("plControl"); TextBox txtbx = new TextBox(); txtbx.ID = "txtQuestion_" + hdField.Value; plcHolder.Controls.Add(txtbx); }
然后在同一个处理程序中的下一个代码块我重新遍历ListViewDataItems并找到控件:
foreach (ListViewDataItem item in lvQuestions.Items) { HiddenField hdField = (HiddenField)item.FindControl("hfQuestionId"); PlaceHolder plcHolder = (PlaceHolder)item.FindControl("plControl"); TextBox txtbx = (TextBox)plcHolder.FindControl("txtQuestion_" + hdField.Value); if (txtbx != null) { Response.Write("TextBox Found:" + txtbx.Text); } }
找到了文本框,但没有任何价值.这就像我刚刚在上一个块中用新的文本框写了一样.如果我删除前一段代码,则找不到任何文本框.
谢谢.
解决方法
正如您已经发现的那样,这是一个生命周期问题.尝试在
ListView.ItemCreated事件中创建动态控件,而不是ListView.ItemDataBound事件.