1

In a user control I have an unordered list that contains items showing editable file data.

I create these items in my javascript using a JSON string set in a hidden field when the page is loaded or when the controlling element (AJAX async file upload) performs it's OnClient_UploadComplete function.

Everything I have is solid so far: the items are created on the client side, and I can get any values changed using an existing hidden field and transfer the data back to a BL object when I need it.

My problem is that during the server-side On_UploadComplete function, my server code cannot find any of the dinamically created items created by my javascript to add essential data to the new item.

I have to be missing something. The value I get from the control is "/r/n"..

My best guess is that my c# code is set up wrong. On the page, to find the ul I have:

Control m_ulFileItems = m_fuPhotoUpload.FindControl("m_ulFileItems");

I'll do some more loking, but this is an important aspect of getting this control to work.

4

2 回答 2

1

在页面中添加一个隐藏字段,然后在发回服务器之前,扫描列表中的所有项目,可能会创建一个 JSON 对象,其中包含所创建项目中所有需要的信息,并将隐藏字段值设置为此 JSON。现在您应该能够获取该值并在 C# 中解析 JSON 并从那里处理它。

于 2012-09-24T20:25:58.957 回答
1

我不知道您如何能够在服务器端访问这些控件。但是,我建议您将它们的值从客户端传递到服务器,可能像这样: $.ajax({ url: ..., type: ..., data: ids and values from your controls in json format } );

以下是更多细节:如果这是 HTML:

<div>
    <div id="placeHolder"></div>
    <input type="button" value="Submit" onclick="submit();" />
</div>

我通过以下方式添加了一些文本框:

    $(document).ready(function () {
        for (var idx = 0; idx < 10; idx++) {
            $('<input class="myInput" type="text" />').appendTo($('#placeHolder'));
        }
    });

那么这应该工作:

    function submit() {
        var jsonInput = '{';
        var inputList = $(".myInput");

        for (var idx = 0; idx < inputList.length; idx++) {
            jsonInput = jsonInput + 'txt_' + idx + ': ' + $(inputList[idx]).val() + ',';
        }

        jsonInput = jsonInput.substring(0, jsonInput.length - 1);
        jsonInput = jsonInput + '}';
        alert(jsonInput);

        $.ajax({
            url: "WebForm1.aspx/HandleSubmitClick",
            data: jsonInput

        });
    }

希望能帮助到你。

如果您不喜欢对服务器的 Ajax 调用,我可以建议使用隐藏控件,并在访问服务器之前将该​​字符串放入隐藏控件中。然后您将能够在服务器端获取该隐藏控件的值。

无论如何,这是基本思想 - 在客户端收集和准备数据,然后以某种方式将其发送到服务器端。

于 2012-09-24T19:11:46.473 回答