我正在完成页面编辑您的个人资料和设置。为了保存动态输入中的数据,我使用Ajax将数据传输到服务器,服务器然后将数据存储在字典中,存储到Session中,以及要保存的菜单名称。 之后,我用JavaScript调用了一个帖子。 在页面加载的c#中,如果它是回发文件,它将调用SaveData函数,该函数检查Session是否要保存的菜单名称,并相应地调用正确的函数(SaveProfile,SaveNotifications,SavePrivacy等) ..)。
我的问题是它只能保存动态输入数据的一半。 有6个不同的列表有动态输入,3个只是纯文本输入,另外3个有文本输入选择。 得到保存的是完全随机的,有时它只会保存社交链接,有时只会保存技能,你就会明白。
我认为它的发生是因为Ajax函数是异步的,当触发回发时,并非所有数据都保存到会话中。
发布所有内容的代码太多,所以如果你想看到其他内容,请告诉我你想看到的内容。
创建onclick保存事件。
var dynamicListIds = ["skills", "hobbies", "games"];
var dynamicSelectListIds = ["socialLinks", "contactInfo", "systemSpecs"];
$(document).ready(function () {
$("a[save=all]").on('click', function () { MultipleDataPassToServer(dynamicListIds, dynamicSelectListIds, 'all'); });
$("a[save=profile]").on('click', function () { MultipleDataPassToServer(dynamicListIds, dynamicSelectListIds, $(this).attr('save')); });
$("a[save][save!=all][save!=profile]").on('click', function () { CallDataPassedEvent($(this).attr('save')); });
});

收集和传输数据。
//Begins the procedure of collection and passing data to the server.
function MultipleDataPassToServer(listIDs, selectListIDs, save) {
if (listIDs != null) {
for (var i = 0; i < listIDs.length; i++) {
TransferInputDataToHiddenField(listIDs[i]);
}
}
if (selectListIDs != null) {
for (var i = 0; i < selectListIDs.length; i++) {
TransferSelectInputDataToHiddenField(selectListIDs[i]);
}
}
CallDataPassedEvent(save);
}
//Gets all values from input elements,
//combines them into one string
//and passes the data to the server
function TransferInputDataToHiddenField(id)
{
var inputElements = document.getElementsByName("input" + id);
//var inputElements = $('#input' + id);
var inputValues = "";
for (var i = 0; i < inputElements.length; i++)
{
inputValues += inputElements[i].value;
if (i != inputElements.length - 1)
{
inputValues += "|";
}
}
CallPassDataToServer(id, inputValues);
}
//Gets all values from select and input elements,
//combines them into one string
//and passes the data to the server
function TransferSelectInputDataToHiddenField(id) {
var inputElements = document.getElementsByName("input" + id);
var selectElements = document.getElementsByName("select" + id);
//var inputElements = $('#input' + id);
var inputValues = "";
for (var i = 0; i < inputElements.length; i++) {
if ($(selectElements[i]).val() == "Custom") {
inputValues += $('#' + id + "Custom" + i).val() + "=";
}
else
{
inputValues += selectElements[i].value + "=";
}
inputValues += inputElements[i].value;
if (i != inputElements.length - 1) {
inputValues += "|";
}
}
CallPassDataToServer(id, inputValues);
}
//Passes data asynchronously to the server
function CallPassDataToServer(id, clientData) {
PageMethods.PassDataToServer(id, clientData, onSucceeded, onFailed);
}
//Calls event to tell server that either the data has been passed,
//or no data needs to be passed.
//save is a string which tells the server which forms's data will be saved.
//pass 'all' into save for the server to save all forms.
function CallDataPassedEvent(save)
{
PageMethods.CallEventDataPassed(save, onDataPassed, onFailed);
}
function onDataPassed(result, userContext, methodName) {
__doPostBack();
}
&#13;
C#服务器代码
[System.Web.Services.WebMethod()]
public static string PassDataToServer(String id, String clientData)
{
if (!dynamicInputs.ContainsKey(id))
dynamicInputs.Add(id, clientData);
else
dynamicInputs[id] = clientData;
return "Debug - Data Passed : " + clientData;
}
[System.Web.Services.WebMethod()]
public static void CallEventDataPassed(String menu)
{
System.Web.HttpContext.Current.Session["AccountSave"] = menu;
System.Web.HttpContext.Current.Session["ProfileDynamicInputs"] = dynamicInputs;
//I tried calling the postback from here but it just gives me an error client side. (OnFailed)
//System.Web.HttpContext.Current.Response.Redirect(System.Web.HttpContext.Current.Request.RawUrl);
//EventDataPassed(menu);
}
C#OnLoad
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
SaveData((string)System.Web.HttpContext.Current.Session["AccountSave"]);
}
}
[UserAction.Authorize.Account]
[UserAction.Authorize.Restrictions(UserAction.Authorize.AuthorizationRule.RequireAll, new int[] { 2, 11 }, true)]
protected void SaveData(string menu)
{
switch (menu)
{
case "all":
SaveProfileData();
break;
case "profile":
SaveProfileData();
break;
case "billing":
break;
case "privacy":
break;
case "notifications":
break;
case "account":
break;
}
}
Here is an image of the site,知道什么样的东西有助于我思考。 在我保存之后,只有一些选项仍然存在,看似随机的,如果我再次保存,更多的消失,如果不是全部的话。
每次页面加载时,如果您需要查看,只需询问即可使用c#重新创建动态输入。
由于