用户控件的css,UpdatePanel中的用户控件 - css样式在更新时消失(IE8)(User controls inside an UpdatePanel - css styles are g...

UpdatePanel中的用户控件 - css样式在更新时消失(IE8)(User controls inside an UpdatePanel - css styles are gone when updating (IE8))

我在UpdatePanel中有一个用户控件。 一旦事件被触发并更新用户控件 - 它似乎失去了它的CSS样式。 这只发生在我的IE8中,而在Chrome和FF中它很好。

用户控件例如:

div.test

{

width: 500px;

height: 400px;

background-color: #0000BB;

color: #FFFFFF;

border: 2px solid #11CC00;

}

public void SetText(string text)

{

div123.InnerText = text;

}

在UpdatePanel内使用用户控件并更新它的页面:

protected void Button1_OnClick(object sender, EventArgs e)

{

test1.SetText(DateTime.Now.ToString());

}

在Chrome和FF中它似乎按预期工作(按钮点击导致当前时间显示在用户控件中,没有其他事情发生),但在IE8中,用户控件内的div丢失其样式(背景颜色,边框)。

可能导致这个问题的原因是什么,可以做些什么来防止它呢?

I have an user control inside an UpdatePanel. Once an event is triggered and updates the user control - it seems to lose its css styles. This happened to me in IE8 only, while in Chrome and FF it was fine.

A user control for example:

div.test

{

width: 500px;

height: 400px;

background-color: #0000BB;

color: #FFFFFF;

border: 2px solid #11CC00;

}

public void SetText(string text)

{

div123.InnerText = text;

}

A page using the user control inside an UpdatePanel and updating it:

protected void Button1_OnClick(object sender, EventArgs e)

{

test1.SetText(DateTime.Now.ToString());

}

In Chrome and FF it seems to be working as expected (button click causes current time to display in the user control, nothing else happens), but in IE8 the div inside the user control loses its styles (background color, borders).

What could be causing this problem, and what can be done to prevent it?

原文:https://stackoverflow.com/questions/4842368

更新时间:2020-03-01 19:04

最满意答案

这里提到了这个问题。

我尝试了这个建议 - 在OnInit中注册css链接 - 它似乎工作。

protected override void OnInit(EventArgs e)

{

base.OnInit(e);

ScriptManager sm = ScriptManager.GetCurrent(Page);

if (!sm.IsInAsyncPostBack)

{

string css = string.Format("", ResolveUrl(CssClassFile));

ScriptManager.RegisterClientScriptBlock(this, typeof(MyBlahControl), "MyBlahId", css, false);

}

}

This issue is mentioned here.

I tried the suggestion - registering the css link in the OnInit - and it seems to work.

protected override void OnInit(EventArgs e)

{

base.OnInit(e);

ScriptManager sm = ScriptManager.GetCurrent(Page);

if (!sm.IsInAsyncPostBack)

{

string css = string.Format("", ResolveUrl(CssClassFile));

ScriptManager.RegisterClientScriptBlock(this, typeof(MyBlahControl), "MyBlahId", css, false);

}

}

相关问答

该按钮永远不会点击btnClick因为该按钮不存在。 您正在按钮单击事件上创建按钮,但它在PostBack上丢失。 必须始终为Page_Load事件处理程序中的每个PostBack操作再次添加动态添加的控件,否则它们将从页面中消失。 但是因为你使用的是UpdatePanel,它看起来仍然存在。 试试下面这个,你会看到按钮点击确实有效。 protected void Page_Load(object sender, EventArgs e)

{

var contain

...

您可以在用户控件中定义要添加的功能。 我在过去做了同样的事情,在我的用户控件中添加了以下内容。 override protected void OnInit(EventArgs e) {

// your code here

}

You could define the functionality you want to add inside your user controls. I have done the same in the past, adding the following, i

...

我相信您缺少用户控件中公开公开UpdateMode属性和内部UpdatePanel控件的Update方法的代码。 您需要这样,以便外部源可以显式刷新用户控件的内容。 您需要以下内容: public UpdatePanelUpdateMode UpdateMode

{

get { return this.UpdatePanel1.UpdateMode; }

set { this.UpdatePanel1.UpdateMode = value; }

}

public void Upd

...

您应该了解asp.net Page Cycle http://msdn.microsoft.com/en-us/library/ms178472%28v=vs.100%29.aspx 要获得已经动态创建的值,您必须从中获取它们 protected void Page_PreRender(object sender, EventArgs e) {

// get values from your dynamically created elements

}

You should learn

...

仅刷新更新面板内的控件而不进行完整页面回发 Only controls inside an update panel are refreshed without a full page postback

要仅在单击其中一个按钮(即“搜索”)时进行更新,您需要将UpdateMode =“Conditional”并设置指向该按钮的“触发器”,例如:

ChildrenAsTriggers="False" runat="server">

...

这里提到了这个问题。 我尝试了这个建议 - 在OnInit中注册css链接 - 它似乎工作。 protected override void OnInit(EventArgs e)

{

base.OnInit(e);

ScriptManager sm = ScriptManager.GetCurrent(Page);

if (!sm.IsInAsyncPostBack)

{

string css = string.Format("

...

你有没有给过iframe用于回发? 就像是:

src="uploaderSender.aspx?AllowedExtension=&StoringPath=&StoringFileName=&OldFileName=&MaximumSize=

...

在UpdatePanel没有PostBack的情况下,您可以包装要更新的所有内容

...

我做了我在更新2上提到的,它工作了! 显然,UpdateProgress控件生成的javascript代码尝试更新受异步回发影响的页面上的所有UpdatePanel,并且找不到Web用户控件内的UpdatePanel(用于查找面板的ID)不匹配面板ClientID)。 解决方案:为Web用户控件内的UpdatePanel建立属性UpdateMode =“Conditional” ,并仅在需要时进行更新。 这样,在按钮生成的异步回发期间不会更新Web用户控件,并且UpdateProgress的jav

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值