获取ListView中动态添加的控件的值(Get values of dynamically added controls in ListView)

编程入门 行业动态 更新时间:2024-10-25 06:31:32
获取ListView中动态添加的控件的值(Get values of dynamically added controls in ListView)

我在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); } }

找到了文本框,但没有任何价值。 这就像我刚刚在上一个块中用新的文本框写了一样。 如果我删除了上一段代码,则找不到任何文本框。

有人可以帮我解决一下我在这里缺少的东西吗?

谢谢。

I am having trouble getting the input values of dynamically created controls in a ListView.

Here is my 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" />

In my ItemDataBound Handler I am adding a TextBox or other control to the Placeholder. The control type is dependent on the item, but to keep it simple lets assume it is always a Textbox. The ID of the control is also dynamic.

// 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);

When a user clicks on the button I need to be able to get the values they filled out.

In my research I found that I need to first recreate the dynamically added controls in order to get the values of them due to the Page Lifecycle.

Here is what I have in my button handler to recreate the controls:

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); }

then the next block of code in the same handler I re-iterate through the ListViewDataItems and find the control:

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); } }

The textbox is found, but there is no value. It's like I just wrote over the textboxes with new ones in the previous block. If I remove the previous block of code no textboxes are ever found.

Can someone please help me out with what I am missing here?

Thank you.

最满意答案

正如您已经发现的那样,这是一个生命周期问题。 尝试在ListView.ItemCreated事件中创建动态控件,而不是ListView.ItemDataBound事件。

As you've already discovered, this is a life cycle issue. Try creating your dynamic control in the ListView.ItemCreated event instead of the ListView.ItemDataBound event.

更多推荐

txtbx,ID,电脑培训,计算机培训,IT培训"/> <meta name="description" co

本文发布于:2023-07-08 20:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1080301.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   动态   ListView   values   controls

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!