简体   繁体   English

动态创建的jQuery控件未添加到我的asp.net页面

[英]Dynamically created jQuery controls are not being added to my asp.net page

I am not quite sure what I am doing wrong here, I have a page in my webforms project which uses jQuery accordion. 我不太确定自己在做什么错,我的Webforms项目中有一个页面使用jQuery手风琴。 When the page is loaded for the first time, the server side code fetches some data to populate a user control which is then added to my page as shown below. 首次加载页面时,服务器端代码会获取一些数据以填充用户控件,然后将其添加到我的页面中,如下所示。 This works fine and I have no problem with this. 这工作正常,我对此没有问题。

The problem is, I am trying to generate a similar thing on my client, using jQuery by simply creating the controls in jQuery ajax response function and then add to dvProjects 问题是,我试图通过简单地在jQuery ajax响应函数中创建控件,然后将其添加到dvProjects ,使用jQuery在客户端上生成类似的dvProjects

ASPX markup: ASPX标记:

<asp:Content ID="Content2" ContentPlaceHolderID="Content" runat="server">

<div runat="server" id="dvProjects" class="dvAccordion">
</div>
</asp:Content>

jQuery AJAX code: jQuery AJAX代码:

function AddUserProject() {
    var projectDialog = $('.dvEditExhibition');
    var projectName = projectDialog.find('.txtProjectName').val();
    var projectDescription = projectDialog.find('.txtProjectDescription').val();

    var project= JSON.stringify({
        'projectId': "00000000-0000-0000-0000-000000000000",
        'projectName': projectName,
        'projectDescription': projectDescription
    });

    $.ajax({
        type: "POST",
        url: "PresentationService.asmx/AddUserProject",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: project,
        success: function(response) {
            var data = response.d;
            console.log(data);

            var h3Title = $('<h3/>', {
                id: "h3ProjectTitle",
                text:data.Name
            });
            var divWrapper = $('<div/>', {

            });
            var dvProjectImages = $('<div/>', {
                class: "dvProjectImages"
            });

            var imgProjectImage = $('<img/>', {
                id: "imgProjectImage",
                class: "imgProjectImage"
            });

            var dvProjectDetails = $('<div/>', {
                id: "dvProjectDetails",
            });

            var pProjectDescription = $('<p/>', {
                id: "pProjectDescription",
                class: "pProjectDescription",
                text: data.Description
            });

            dvProjectImages.append(imgProjectImage);

            dvProjectDetails.append(pProjectDescription);

            divWrapper.append(dvProjectImages, dvProjectDetails);
            var dvProjects = $('#dvProjects');

            dvProjects.append(h3Title);
        },
        error: function(response) {

        }
    });
    }

My problem is these controls are not added to dvProjects . 我的问题是这些控件未添加到dvProjects

Update: you had a bug in your javscript too, refer to my comments. 更新:您的javscript中也有一个错误,请参阅我的评论。

That's because when you set a div to runat="server" every html element in it, becomes a Literal Control server side and they are persisted to view state. 那是因为当您将div设置为runat =“ server”时,其中的每个html元素都将变为Literal Control服务器端,并且它们将持久保存以查看状态。

When you post the page, the server side control rebuilds the runat="server" div with information in the postdata (view state). 发布页面时,服务器端控件将使用postdata(视图状态)中的信息重建runat =“ server” div。

When you add controls with jQuery they are not in the postData/ViewState, so when the server side code revuilds the div, it doesn't see the ones you added with jQuery. 当您使用jQuery添加控件时,它们不在postData / ViewState中,因此,当服务器端代码撤消div时,不会看到您使用jQuery添加的控件。

To fix that, you would need to create a custom WebControl and implement IPostBackEventHandler. 要解决此问题,您需要创建一个自定义WebControl并实现IPostBackEventHandler。 So instead of... 所以代替...

<div runat="server" id="dvProjects" class="dvAccordion">

You would have something like this, 你会有这样的事情,

<xyz:ProjectsControl runat="server" cssclass="dvAccordion" id="theProjects" />

Then to add a control to it, you would do something like 然后向其添加控件,您将执行以下操作

__doPostBack('<%= theProjects.UniqueID %>, 'someJsonStringHere');

Then in your IPostBackEventHandler implementation, you serialize the json string coming in and use that to determine what server side controls to add to the ProjectsControl. 然后在您的IPostBackEventHandler实现中,序列化传入的json字符串,并使用该字符串确定要添加到ProjectsControl的服务器端控件。

WebControl has methods you can override, WriteBeginTag, WriteEndTag, and there you could write an opening div and closing div, so your web control would render as a div. WebControl具有可以重写的方法WriteBeginTag,WriteEndTag,在那里可以编写一个开始div和结束div,因此您的Web控件将呈现为div。

By adding a string property calledd "CssClass" to your webcontrol, you can use that to set the class. 通过将一个名为“ CssClass”的字符串属性添加到您的Web控件,您可以使用它来设置类。 So in WriteBeginTag you would also write out the class attribute to the div using CssClass as the value. 因此,在WriteBeginTag中,您还将使用CssClass作为值将class属性写出div。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM