我有一个包含TabControl的ASP.Net页面(由Devexpress提供),其中每个Tab都是IFrame。 我的问题是随机(大约每天一次)一个标签更改其内容并显示另一个(显示标签)内容,使用2014年5月发布的谷歌浏览器(此版本是客户基础架构使用的浏览器版本)。 如果我读取IIS日志,我会看到从一个IFrame请求开始(请求由客户端定期发送,大约每分钟一次)disapear和另一个IFrame请求重复。 似乎随机的一个iframe改变了它的来源并获得了另一个iframe的页面来源。
这是我的场景主页代码:
在主页的Page_Load上,我按代码设置每个IFrame源(根据DB使用客户端静态IP作为配置ID的配置)。
protected void Page_Load(object sender, EventArgs e)
{
string ip = Request.UserHostAddress;
UserConfig configuration = (new UserConfig(ip)).Read();
if (!Page.IsPostBack && !Page.IsCallback)
{
try
{
int numberOfPages = configuration.Tabs.Length;
int toClear = pgrTabControl.TabPages.Count - numberOfPages;
while (toClear > 0)
{
pgrTabControl.TabPages.RemoveAt(pgrTabControl.TabPages.Count - 1);
toClear--;
}
foreach ( Tab tab in configuration.Tabs )
{
TabPage currentPage = pgrTabControl.TabPages[i];
currentPage.Text = tab.TabDesc;
currentPage.Name = "Page_" + Tab.tabID;
switch (i)
{
case 0: Iframe0.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
case 1: Iframe1.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
case 2: Iframe2.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
case 3: Iframe3.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
case 4: Iframe4.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
case 5: Iframe5.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
case 6: Iframe6.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
case 7: Iframe7.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
case 8: Iframe8.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
case 9: Iframe9.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
case 10: Iframe10.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
case 11: Iframe11.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
case 12: Iframe12.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
case 13: Iframe13.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
case 14: Iframe14.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
case 15: Iframe15.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
case 16: Iframe16.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
case 17: Iframe17.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
case 18: Iframe18.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
case 19: Iframe19.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
case 20: Iframe20.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
case 21: Iframe21.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
case 22: Iframe22.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
case 23: Iframe23.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
case 24: Iframe24.Src = "~/Container.aspx?tabID=" + tab.TabID + "&ID=" + i + "&NumOfPage=" + numberOfPages; break;
default: throw new Exception( Resources.exc_ControlNotFound );
}
i++;
}
}
catch (Exception err)
{
}
}
}
在我的方案中(该网站仅由公司在本地网络中使用),IP是静态的,每个客户端需要至少看到25个自定义选项卡。
然后在Container.aspx页面中,我使用Request获取tabID(标识要显示的元素,因此用户的tab [0]可以显示另一个tab [0]中的不同内容),并了解我需要显示的内容。 Container.aspx内部有一个UserControl,以正确的方式显示数据。
这是Container.aspx代码:
protected void Page_Load(object sender, EventArgs e)
{
try
{
//this variable "tabId" is private class variable (NOT static)
tabId = Request["tabID"];
try
{
//this variable "id" is private class variable (NOT static)
id = Convert.ToInt32 ( Request["ID"] ) ;
}
catch {}
try
{
//this variable "numberOfPages" is private class variable (NOT static)
numberOfPages = Convert.ToInt32 ( Request["NumOfPage"] ) ;
}
catch {}
if (UCDisplayData != null)
{
UCDisplayData.TabId = tabId ;
UCDisplayData.TabIndex = id ;
UCDisplayData.NumberOfTab = numberOfPages ;
}
}
catch ( Exception err )
{
}
}
然后UCDisplayData使用其属性TabId使用ASPxCallback和一些javascript来获取绑定到的当前TabId并刷新其内容(每分钟一次)。 UCDisplayData包含一些在Page_Load上正确填充的ASPxTextBox。 UpdateCallBack然后向页面发送一些JSON,其中包含要更新的字段名称和字段值。 没有javascript可以在代码的任何部分使用IFrame源。
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (this.TabIndex < this.NumberOfTab)
{
txtData1.Text = data readen form db ;
txtData2.Text = data readen form db ;
.
.
txtDataN.Text = data readen form db ;
}
}
catch (Exception err)
{
LastError = err.Message;
if (!Page.IsCallback)
Response.Redirect("Error.aspx");
else
ASPxWebControl.RedirectOnCallback("Error.aspx");
}
}
更新回调代码如下:
protected void UpdateFieldCallBack_Callback(object source, DevExpress.Web.ASPxCallback.CallbackEventArgs e)
{
dynamic message = new ExpandoObject();
try
{
string fieldToUpdate = string.Empty;
message.Control_txtData1 = txtData1.ClientInstanceName;
message.Control_txtData2 = txtData2.ClientInstanceName;
.
.
message.Control_txtDataN = txtDataN.ClientInstanceName;
message.txtData1 = data readen form db ;
message.txtData2 = data readen form db ;
.
.
message.txtDataN = data readen form db ;
e.Result = Utils.Serialize(message);
}
catch (Exception err)
{
LastError = err.Message;
if (!Page.IsCallback)
Response.Redirect("Error.aspx");
else
ASPxWebControl.RedirectOnCallback("Error.aspx");
}
}
Utils.Serialize()方法:
public static string Serialize ( object toSerialize )
{
try
{
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
return javaScriptSerializer.Serialize(toSerialize);
}
catch ( Exception err )
{
return string.Empty ;
}
}
然后客户端javascrit更新了UserControl:
Update.OnCallbackComplete = function(s, e) {
var message = deserialize(JSON.parse(e.result));
window[message.Control_txtData1].SetText(message.txtData1);
window[message.Control_txtData2].SetText(message.txtData2);
.
.
window[message.Control_txtDataN].SetText(message.txtDataN);
};
function deserialize(_jsonObj) {
if (!_jsonObj)
return {};
var result = {};
for (var i = 0, len = _jsonObj.length ; i < len ; i++) {
var obj = _jsonObj[i];
var key = _jsonObj[i].Key;
var value = _jsonObj[i].Value;
if (key)
result[key] = (Array.isArray(value)) ? deserialize(value) : value;
else {
if (obj && Array.isArray(obj)) {
var array = [];
for (var i = 0, len = _jsonObj.length ; i < len ; i++)
array.push(deserialize(_jsonObj[i]));
return array;
}
}
}
return result;
};
有人知道为什么,随机的iframe获得另一个的相同内容吗? 提前谢谢。