回发后如何获取动态控件的值?
我有一个在 ItemDataBound 事件中添加控件的列表视图。当回发发生时,我找不到新的控件。经过一番研究,我发现 ASP .NET 每次都需要创建这些控件,即使在回发之后也是如此。从那里,我将函数移至 if (!Page.IsPostBack)
条件之外绑定 ListView。现在我得到了动态控件值,但我拥有的静态控件被设置为默认值。以下是我想要完成的任务的示例:
为了简洁起见,我在这个示例中省略了一些明显的事情。
<asp:ListView runat="server" ID="MyList" OnItemDataBound="MyList_ItemDataBound">
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="itemPlaceholder" />
</LayoutTemplate>
<ItemTemplate>
<asp:PlaceHolder runat="server" ID="ProductPlaceHolder">
<asp:TextBox runat="server" ID="StaticField" Text="DefaultText" />
<asp:PlaceHolder ID="DynamicItems" runat="server" />
</asp:PlaceHolder>
</ItemTemplate>
</asp:ListView>
这是隐藏的代码:
protected void MyList_ItemDataBound(object sender, System.Web.UI.WebControls.ListViewItemEventArgs e) {
PlaceHolder DynamicItems = (PlaceHolder)e.Item.FindControl("DynamicItems");
DynamicItems.Controls.Add(textbox);
}
所以,就像我说的,如果我只在 Page != PostBack
时进行数据绑定,那么我无法在回发时找到动态控件。如果我每次加载页面时都进行绑定,那么我的静态字段就会设置为其默认文本。
I have a listview that adds controls in the ItemDataBound Event. When the postback occurs, I cannot find the new controls. After a bit of research, I found that ASP .NET needs these controls created every time, even after postback. From there I moved the function to bind the ListView outside of the if (!Page.IsPostBack)
conditional. Now I get the dynamic controls values but the static controls I have are set to their defaults. Here is a sample of what I am trying to accomplish:
For brevity, I left some obvious things out of this example.
<asp:ListView runat="server" ID="MyList" OnItemDataBound="MyList_ItemDataBound">
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="itemPlaceholder" />
</LayoutTemplate>
<ItemTemplate>
<asp:PlaceHolder runat="server" ID="ProductPlaceHolder">
<asp:TextBox runat="server" ID="StaticField" Text="DefaultText" />
<asp:PlaceHolder ID="DynamicItems" runat="server" />
</asp:PlaceHolder>
</ItemTemplate>
</asp:ListView>
and here is the codebehind:
protected void MyList_ItemDataBound(object sender, System.Web.UI.WebControls.ListViewItemEventArgs e) {
PlaceHolder DynamicItems = (PlaceHolder)e.Item.FindControl("DynamicItems");
DynamicItems.Controls.Add(textbox);
}
So, like I said, if I only databind when Page != PostBack
then I cant find my dynamic controls on postback. If I bind every time the page loads then my static fields get set to their default text.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将 ListView 的数据绑定移至 OnInit() 事件中。
Try moving the data binding of the ListView into the OnInit() event.
非常相似的问题(而不是填充 ListView 时,这家伙正在生成一组按钮)。简而言之,您会发现必须将列表中的项目存储在 Viestate 中 - 而不是在 Postback 上将其取出并重新填充列表。
请注意,此解决方案意味着删除数据绑定(您可能出于其他原因不想这样做)。
希望有帮助。
Very similar question (instead of populating a ListView the guy is generating a set of buttons). Briefly, you'll find that you have to store the items in the list in your Viestate - than fish it out on Postback and re-populate the list.
Note that this solutions implies dropping data-binding (which you might not wanna do for others reasons).
Hope it helps.