ASP.NET,使用 .Controls.Add() 添加的控件在页面回发和发布时会丢失。如果我使用 .InnerHtml 添加它们,我无法引用它们

发布于 2024-10-19 12:06:10 字数 419 浏览 0 评论 0原文

我正在尝试创建一个评论页面。我从数据库中获取之前发表的评论,并且有一个用于提交新评论的文本框。因此,为了显示数据库中的旧评论,必须动态添加它们,对吧?起初我使用的是类似:TheContainerControl.Controls.Add(TheComment),但事实证明,当页面回发时(提交新评论或其他内容),我动态添加的所有内容都消失了! 为了解决这个问题,我使用 TheContainerControl.InnerHtml("[html]") 代替。这样,当页面回发时,我动态添加的控件就不会丢失。然而,我现在已经搜索了近九或十个小时,寻找一种将事件处理程序添加到我通过 .InnerHtml 添加的控件的方法,但我没有找到任何东西:(我根本无法引用它们,.FindControl( )返回一个空引用。我还尝试手动插入会触发该事件的代码/脚本,但惨败(我是 jQuery 新手,我对 javascript 知之甚少)。

I'm trying to make a comments page. I get previously made comments from a database, and there's a textbox for the new comment to be submitted. So in order to display the old comments from the database they have to be added dynamically, right? At first I was using something like: TheContainerControl.Controls.Add(TheComment), but it turns out that when the page is posted back (new comment submitted or something), everything I've dynamically added is gone!
In order to work around this, I used TheContainerControl.InnerHtml("[html]") instead. That way, when the page is posted back the controls I've dynamically added aren't lost. However, I've been searching for nearly nine or ten hours now for a way to add event handlers to the controls I add via .InnerHtml and I haven't found anything :( I can't reference them at all, .FindControl() returns a null reference. I also tried to manually insert the code/script that would trigger the event, and miserably failed (I'm new to jQuery, and I know very little javascript). Please help if you can.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撑一把青伞 2024-10-26 12:06:10

使用 Repeater、GridView 或 ListView 等控件会更容易,这些控件是为显示动态数量的评论而构建的。

每次回发时都需要将动态控件添加回页面。

我不确定您的意思:“我已经搜索了近九或十个小时,寻找一种将事件处理程序添加到我通过 .InnerHtml 添加的控件的方法,但我没有找到任何东西:(我可以'根本不引用它们,.FindControl() 返回一个空引用,我还尝试手动插入会触发该事件的代码/脚本,但惨败了” - JQuery 如何适应这一点,您是否尝试这样做?客户端还是服务器

It would be much easier to use a control like the Repeater, GridView, or ListView instead, which is built to display a dynamic number of comments.

Dynamic controls need to be added back to the page on every postback.

I'm not sure what you mean by: "I've been searching for nearly nine or ten hours now for a way to add event handlers to the controls I add via .InnerHtml and I haven't found anything :( I can't reference them at all, .FindControl() returns a null reference. I also tried to manually insert the code/script that would trigger the event, and miserably failed" - how does JQuery fit into this, are you trying to do this on client or server?

HTH.

只是在用心讲痛 2024-10-26 12:06:10

您在页面上添加回的这些旧注释,这些 UI 元素是响应 DOM 事件(如单击等)还是仅仅是用于显示的标签?如果它们只是用于显示,那么正如 Brian 上面所说,您最好使用 Repeater r GridView 或 ListView 而不是动态控件。

实际上,即使它们是控件元素,您仍然可以将它们放入 GridView 中的模板列中,并让它处理分页等。您可以使用 jQuery 实时事件来处理这些控件。

或者,使用 ASP.NET Ajax 和更新面板仅刷新页面中绝对需要在回发时刷新的部分,而使其他部分保持不变。

如果您可以发布一些代码,将会很有帮助。

These old comments that you are adding back on the page, are these UI elements which respond to DOM events like click etc or are they merely lables which are there just for display? If they are merely there for display then as Brian said above you are better of using a Repeater r GridView or ListView instead of dynamic controls.

Actually even if they are control elements, you can still drop them in a template column in a GridView and let it handle the pagination etc. You can use jQuery live events to work with these controls.

Alternatively use ASP.NET Ajax and Update Panels to only refresh the part of the page that absolutely needs to be refreshed on a postback leaving the other parts intact.

It would be helpful if you can post some code.

翻身的咸鱼 2024-10-26 12:06:10

在 Asp.net 中动态添加控件到页面时必须记住的一点是 页面生命周期。当您可以添加控件并在下一次回发后仍然保留它们时,这是非常特殊的。或者多晚您可以添加控件并仍然让它们呈现。

人们动态构建页面的常见趋势是开始将代码放入 Page_Load() 中,但这在生命周期中为时已晚。您可能想尝试 Init (或 Page_Init)。

正如其他人所建议的,您也可以考虑根本不动态添加控件,而是使用一些内置的 Asp.net 内容控件来管理页面的动态状态。当您使用其他可以动态构建页面的 Web 框架/技术时,Asp.net 可能看起来有点复杂。 请记住 - 一旦您学会利用页面生命周期和 Asp.net 控件,您就不必自己手动管理页面上所有元素的状态。

The thing you have to keep in mind in Asp.net when dynamically adding controls to a page is the page life cycle. It is very particular about when you can add controls and still have them be persisted after the next postback. Or how late you can add controls and still have them render.

The common tendency for people building pages dynamically is to just start placing their code in the Page_Load() but this is too late in the life cycle. You might want to try the Init (or Page_Init).

As others have suggested, you may also consider not dynamically adding controls at all but instead sing some built in Asp.net content controls to manage the dynamic state of your page. When coming from other web frameworks/technologies where you can just build a page on the fly, Asp.net can seem a bit complicated. Just remember - once you learn to take advantage of the page life cycle and Asp.net controls you will not have to manually manage the state of all the elements on the page yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文