使用 AJAX 在中继器内动态添加编辑器控件

发布于 2024-08-14 03:20:40 字数 1152 浏览 3 评论 0原文

我正在寻找一种将编辑器控件(来自 ASP.NET AJAX 控件工具包)添加到转发器的每个元素的方法。如果我只是将它包含在转发器的 ItemTemplate 中,它就可以正常工作,但我的问题是它生成的标记很大,这会大大减慢页面速度(即使打开压缩)。

我没有使用更新面板在中继器项目内添加控件 - 我认为这可能是首选方法,但在中继器项目内的更新面板内动态添加控件并不是我所拥有的任何成功的做法,但我似乎找不到任何好的例子。

我考虑的另一种选择是使用 PageMethods 呈现控件并将 HTML 发送回页面,让 javascript 将其放在适当的位置,然后处理它,但它不会让我呈现控件 - 我得到一个InvalidOperationException“页面不能为空。请确保此操作是在 ASP.NET 请求的上下文中执行的。”。我的猜测是,生成的所有 JavaScript 都使我无法即时渲染编辑器控件。

您能为我指明实现这一目标的正确方向吗?

谢谢

编辑:如果可能的话,另一种选择是将普通的编辑器控件放在页面的标记中,然后根据需要使用 javascript 将其在转发器内移动。我可以使用普通控件执行此操作,但是当我使用编辑器执行此操作时,它的表现不佳 - 文本框出现,但不允许我在其中单击。如果您对此有任何想法,我也将不胜感激。这是这个的代码:

<span id="spanHiddenEditor" style="display: block;">
    <cc1:Editor ID="ed1" runat="server" Height="200" Width="400" />
</span>
<script type="text/javascript">
    function createTextBox(idx) {
        var span = $get("span1_" + idx); // this gets me the target location
        var hiddenEditorSpan = $get("spanHiddenEditor")
        var editorHtml = hiddenEditorSpan.innerHTML;
        hiddenEditorSpan.innerHTML = "";
        span.innerHTML = editorHtml;
    }
</script>

I'm looking for a way to add an Editor control (from the ASP.NET AJAX Control Toolkit) to each element of a repeater. It's working fine if I just include it in the ItemTemplate of the repeater, but my problem is that the markup it produces is massive, which slows down the page considerably (even with compression on).

I haven't had any luck adding the control inside the repeater item using an Update Panel - I think this is probably the preferred method, but dynamically adding a control inside an Update Panel inside of a Repeater Item isn't something I've had any success doing, and there don't seem to be any good examples of this that I can find.

The other alternative I considered was using PageMethods to render the control and send the HTML back to the page to let the javascript put it in the appropriate place, then deal with it, but it won't let me render the control - I get an InvalidOperationException of "Page cannot be null. Please ensure that this operation is being performed in the context of an ASP.NET request.". My guess is that all the javascript that's generated makes it so that I can't just render an Editor control on the fly.

Can you point me in the right direction for accomplishing this?

Thanks

EDIT: Another alternative, if it is possible, would be to put a normal Editor control in the markup of the page, then move it around inside of the repeater as needed, using javascript. I can do this with normal controls, but when I do it with an editor, it is not behaving nicely - the textbox appears, but won't let me click inside it. If you have any ideas on this one, I'd appreciate that as well. Here's the code for this:

<span id="spanHiddenEditor" style="display: block;">
    <cc1:Editor ID="ed1" runat="server" Height="200" Width="400" />
</span>
<script type="text/javascript">
    function createTextBox(idx) {
        var span = $get("span1_" + idx); // this gets me the target location
        var hiddenEditorSpan = $get("spanHiddenEditor")
        var editorHtml = hiddenEditorSpan.innerHTML;
        hiddenEditorSpan.innerHTML = "";
        span.innerHTML = editorHtml;
    }
</script>

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

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

发布评论

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

评论(1

第七度阳光i 2024-08-21 03:20:40

使用类识别中继器内的位置,然后使用 jquery 将 html 插入到准备好的页面上

$(function() {  
        $(".myclass").Append("<htmlz>");
}); 

然后,如果您需要识别每个文本框,您可以使用父容器 ID 来完成,例如。

$("#myID div.class input").value

我是一个完全的 jquery 新手,所以它可能都不起作用,但我相信这个想法是一个好主意!

identify the location within the repeater with a class then use jquery to inser the html on page ready

$(function() {  
        $(".myclass").Append("<htmlz>");
}); 

Then if you need to identify each text box you can do it using the parent container ID eg.

$("#myID div.class input").value

I'm a total jquery newb so none its likely none of it will work, but i believe the idea is a good one!

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