ASP.NET AJAX的UpdatePanel和放大器;动态HTML插入发行(Telerik的) [英] ASP.NET AJAX UpdatePanel & Dynamic HTML Insertion Issue (Telerik)

查看:101
本文介绍了ASP.NET AJAX的UpdatePanel和放大器;动态HTML插入发行(Telerik的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我有一个问题。我目前正在使用ASP.NET AJAX的Telerik的框架的一个项目,尽管不应该多大关系,因为我的工作,这部分完全(几乎)绕过Telerik的。

Ok so I have a problem. I am currently working on a project using the Telerik framework for ASP.NET AJAX, although that shouldnt matter much as I am bypassing Telerik completely (almost) for this portion of the work.

我是一个类似Facebook的聊天程序放在一起进入我们公司的CRM系统,同时一切顺利了这一点,我已经打了一个路障。问题是,当我尝试创建一个新的客舱。我使用同一个jQuery $结合的ASP控制的UpdatePanel阿贾克斯调用一个JSON方法名传递给隐藏文件我的code。这里的逻辑JSON:

I'm putting together a facebook-like chat program into our company's CRM system and, while all has gone well up to this point, I've hit a roadblock. The problem is when I try to "create a new chatbox". I'm using the asp control UpdatePanel in conjunction with a jQuery $.ajax call to pass a JSON method name to my code behind file. Here's the JSON logic:

的DoubleClick用户在用户列表:

$telerik.$(".UserListEntry").dblclick(function () {
                var ToName = $telerik.$(this).children(".UserListEntryName").text();
                var FromName = $telerik.$("#lkUserGeneralSettings").text();
                $telerik.$.ajax({
                    type: 'POST',
                    url: 'DefaultHandler.ashx',
                    data: { "ToName": ToName, "FromName": FromName },
                    success: CreateChatBox(),
                    error: function (response) { alert("error: 001"); }
                });
            });

CreateChatBox回调:

function CreateChatBox() {
            $telerik.$.ajax({
                type: 'POST',
                url: 'Default.aspx',
                data: { MethodName: "CreateChatBox" },
                success: ForceAsyncPostback,
                error: function (response) { alert("error: 002"); }
            });
        }

力asyncpostback(不应该是必要的,但这并不甚至擦出火花!):

function ForceAsyncPostback() {
            var UpdatePanel1 = '<%=Panel3.ClientID%>';

            if (UpdatePanel1 != null) {
                __doPostBack(UpdatePanel1, '');
            }
            alert("Success");
        }

在UpdatePanel通过各种文字和一些硬codeD HTML好极了老式的div创建的。问题是不会与动态创建表示元素,这工作就好了。其实我的背后code(我会后下)创建,如果我把它放到我的pageLoad的事件显示一切都完美的罚款。

The UpdatePanel is created through various literals and some hard-coded html good-ole' fashioned divs. The problem is NOT with the dynamic creation of said elements, this works just fine. In fact my code behind (which I will post below) creates and displays everything perfectly fine if I place it into my PageLoad event.

在任何情况下,这里是在.aspx:

<asp:UpdatePanel ID="Panel3" runat="server" OnLoad="Panel3_Load" UpdateMode="Conditional">
                <ContentTemplate>
                    <asp:Literal ID="ChatBoxesLiteralTop" runat="server" />
                    <asp:Literal ID="ChatBoxesLiteralMid" runat="server" />
                    <asp:PlaceHolder ID="ChatBoxesPlaceHolder" runat="server" />
                    <asp:Literal ID="ChatBoxesLiteralBot" runat="server" />
                    <div id="UserListCorner">
                        <img id="UserListBtn" src="images/list.png" />
                    </div>
                    <div id="UserList" class="UserListHidden">
                        <div id="UserListView">
                            <asp:Literal ID="UserListViewLiteral" runat="server" />
                        </div>
                    </div>
                </ContentTemplate>
            </asp:UpdatePanel>

code背后:

protected void Panel3_Load(object sender, EventArgs e)
    {
        #region Ajax methods
        if (Request.Form["MethodName"] == "CreateChatBox")
        {
            CreateChatBox();
        }
        #endregion

        Engine m_engine = new Engine();
        string m_sql = @"SELECT FullName FROM Users WHERE RecordDeleted <> 1";
        DataTable dt = m_engine.GetObjectsAsDataTable(m_sql);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            UserListViewLiteral.Text += "<div class='UserListEntry'><span class='UserListEntryStatus'><img src='images/status-online.png' width='10' /></span>&nbsp;<span class='UserListEntryName'>" + dt.Rows[i]["FullName"].ToString() + "</span></div>";
        }

        RadAjaxManager.GetCurrent(Page).ResponseScripts.Add("ChatAjax()");   
    }

    private void CreateChatBox()
    {
        ChatBoxesLiteralTop.Text = @"<div id='ChatBox' class='ChatBoxHidden'>
                                        <div class='ChatBoxHeader'>
                                            <img id='ChatBoxStatusBtn' src='Images/status-online.png' />
                                            <span id='ChatBoxUserLabel'>John Doe</span>
                                            <img id='closeBtn' src='Images/close.png' />
                                            <img id='toggleTab' src='Images/up-arrow.png' />
                                        </div>
                                        <div id='ChatBoxMessageOutput'></div><div class='ChatBoxFooter'>";

        TextBox txt = new TextBox();
        txt.ID = "ChatBoxMessageInput";
        txt.Height = 16;
        txt.MaxLength = 270;
        txt.Width = 250;
        txt.AutoPostBack = false;
        ChatBoxesPlaceHolder.Controls.Add(txt);

        RadButton btn = new RadButton();
        btn.ID = "ChatBoxSendButton";
        btn.Text = "Send";
        btn.AutoPostBack = true;
        btn.Height = 22;
        btn.Click += ChatBoxSendButton_Click;
        ChatBoxesPlaceHolder.Controls.Add(btn);

        ChatBoxesLiteralBot.Text = "</div></div>";

        Panel3.Update();

        RadAjaxManager.GetCurrent(Page).ResponseScripts.Add("ChatAjax()");
    }

我当然忽视的东西悍然愚蠢的,但是从经验丰富的ASP.Net Ajaxer一套新的眼睛会真的AP preciated!先谢谢了。

I'm certainly overlooking something outrageously stupid, but a fresh set of eyes from a seasoned ASP.Net Ajaxer would be really appreciated! Thanks in advance.

什么是工作:


  • 的code运行正常。

  • 的JSON方法传递到code后面读取。

  • 的CreateChatBox()方法贯穿据称填充文字和控制。

  • 在JSON链中的所有回调成功执行。

什么行不通:


  • 在UpdatePanel,连多余的回传后,不具备
    之后,code这个新的HTML成功执行。

推荐答案

好吧,我碰巧当天发布后解决了这个问题。当然,现在有新的障碍来解决,但我很高兴地说,我的异步聊天模块已接近完成。由于没有人决定把对这个问题,我会寄我做了基于硬件codeD HTML在ASP.NET正常生产动态,异步的对象,因为,谷歌搜索,而此事,这个帖子是唯一的相关导致弹出。

To Whom It May Concern


Well I happened to solve this problem the day after posting it. Of course there are now new hurdles to tackle but I am happy to say that my asynchronous chat module is nearly done. Since nobody decided to take on the problem, I am going to post what I did to properly produce dynamic, asynchronous objects based on hard-coded HTML in ASP.NET since, while Googling the matter, this post was the only relevant result popping up.

我会说我很清楚地知道,这是一个非常非常规的方式开始。话虽这么说,该项目的要求,合理的手段。虽然可能有很多其他的方式来实现自己的目标,这并不工作。我不打算进入无关的细节相对于我的具体项目,但希望这将帮助别人的未来。

I'll start by saying I am well aware that this is a very unconventional approach. That being said, the requirements of the project justified the means. While there may be many other ways to accomplish your goal, this does work. I am not going to go into extraneous details with respect to my particular project, but hopefully this will help somebody in the future.

与我原来的问题主要的问题是我在攻击渲染序列的方式。在我的逻辑的一大缺陷是试图在ASP控件(文字,占位符,按钮等等)从我的HTML太多分开。我(从上面)的初步做法是这样的:

The primary problem with my original question was the way I was attacking the rendering sequence. A major flaw in my logic was attempting to separate the ASP controls (literals, placeholders, buttons and such) from my HTML too much. My initial approach (from above) looked like this:

<asp:UpdatePanel ID="Panel3" runat="server" OnLoad="Panel3_Load" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Literal ID="ChatBoxesLiteralTop" runat="server" />
                <asp:Literal ID="ChatBoxesLiteralMid" runat="server" />
                <asp:PlaceHolder ID="ChatBoxesPlaceHolder" runat="server" />
                <asp:Literal ID="ChatBoxesLiteralBot" runat="server" />
                <div id="UserListCorner">
                    <img id="UserListBtn" src="images/list.png" />
                </div>
                <div id="UserList" class="UserListHidden">
                    <div id="UserListView">
                        <asp:Literal ID="UserListViewLiteral" runat="server" />
                    </div>
                </div>
            </ContentTemplate>
</asp:UpdatePanel>

最后的结果看起来更像是这样的:

The final result looks more like this:

<asp:UpdatePanel ID="Panel3" runat="server" OnLoad="Panel3_Load" UpdateMode="Conditional">
                <ContentTemplate>
                    <asp:PlaceHolder ID="ChatBoxesPlaceHolder" runat="server" />
                    <div id="UserListCorner">
                        <img id="UserListBtn" src="images/list.png" />
                    </div>
                    <div id="UserList" class="UserListHidden">
                        <div id="UserListView">
                            <asp:Literal ID="UserListViewLiteral" runat="server" />
                        </div>
                    </div>
                    <asp:Label ID="Label42" Font-Bold="true" runat="server" />
                    <asp:HiddenField runat="server" ID="LatestDisplayTick" />
                </ContentTemplate>
</asp:UpdatePanel>

通过后面的code(略)与此类似:

With the code behind (abridged) similar to this:

Literal top = new Literal();
// ...
UpdatePanel UpdatePanel2 = new UpdatePanel();
// ...
top.Text = @"<div id='ChatBox' class='ChatBoxShown'>
                                             <div class='ChatBoxHeader'>
                                                 <img id='ChatBoxStatusBtn' src='Images/status-online.png' />
                                                 <span id='ChatBoxUserLabel'>" + UserNames[0] + @"</span>
                                                 <img id='closeBtn' src='Images/close.png' />
                                                 <img id='toggleTab' src='Images/up-arrow.png' />
                                             </div>";

top.ID = "ChatBoxesLiteralTop";
top.Mode = LiteralMode.PassThrough;
// ...
UpdatePanel2.ID = "UpdatePanel2";
UpdatePanel2.UpdateMode = UpdatePanelUpdateMode.Conditional;
UpdatePanel2.ChildrenAsTriggers = false;
UpdatePanel2.ContentTemplateContainer.Controls.Add(top2);
// ...
Panel3.ContentTemplateContainer.Controls.Add(top);
Panel3.ContentTemplateContainer.Controls.Add(UpdatePanel2);               
Panel3.ContentTemplateContainer.Controls.Add(mid);

意味着什么


所有我所做的就是包裹着我客舱元素的整体的一个占位符内,动态地创建包装ASP所有从服务器控件的HTML元素和张贴作为一个动态数据的块。这种回避与ASP的控制布局一些奇怪的行为,否则,并且允许对我的项目的客户端功能的准OO的方法。创建后,我现在可以利用一些JSON和AJAX调用动态地将数据输入到上述动态创建的控件。

What It Means


All I've done is wrapped the entirety of my chatbox elements inside a single placeholder, dynamically creating HTML elements that wrap ASP controls all from the server and posting it as a "chunk" of dynamic data. This sidesteps some strange behaviors with ASP control placement otherwise, and allows for a quasi-OO approach for the client-side functionality of my project. After creation, I can now make use of some JSON and AJAX calls to dynamically enter data into said dynamically created controls.

一个例子是张贴从数据库通知接收到我的聊天窗口在收到时(ChatBoxesLiteralMid控制)消息

An example would be posting a message received from a database notification to my chat window (ChatBoxesLiteralMid control) when it is received.

页/控制负载后

if(ChatBoxesLiteralMid != null)
            ChatBoxesLiteralMid.Text += @"<div class='ChatBoxEntry'><span class='ChatBoxEntryName ChatBoxSelf'>" + AppParameters.Current.AppUser.FirstName + "</span>:&nbsp;<span class='ChatBoxEntryMessage'>" + dy.Rows[dy.Rows.Count - 1]["Message"].ToString() + "</span></div>";

为什么不嫌麻烦?


我已经完成是与需求的特定项目,以远远超出原有的范围,之前由其他开发商几年才开始。这是教老狗一些新花样一种非常规的方式。我现在在客户端即时聊天盒/会话的滥量接近完全控制。这真是pretty刻着'甜蜜的诚实。请随意张贴问题(或关注)我定期检查SO,我很高兴做出回应。

Why Go To The Trouble?


What I have accomplished is for a particular project with needs to go far above and beyond the original scope, started by another developer years prior. This is an unconventional way to teach the old dog some new tricks. I now have near-full control of an indiscriminate amount of real-time chat boxes/sessions on the CLIENT side. It's really pretty freakin' sweet honestly. Please feel free to post questions (or concerns) as I regularly check SO and am happy to respond.

这篇关于ASP.NET AJAX的UpdatePanel和放大器;动态HTML插入发行(Telerik的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆