updatepanel问题

hdjjun 2009-04-07 02:57:20
前台页面

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function chg() {
document.getElementById("Button2").click();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" >
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button1"
onclick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button2" runat="server" Text="Button2"
onclick="Button2_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>


后台代码

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button2_Click(object sender, EventArgs e)
{
Label1.Text += " chged!";
}
protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "key", "chg();", true);
}
}


有两UpdatePanel,updatePanel 1里的按钮点击后注册一段脚本,这段脚本是调用updatepanel2(UpdateMode="Conditional")中的按钮,并使updatepanel2更新,可是上面为什么无效,请高手指点
...全文
185 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
hdjjun 2009-04-08
  • 打赏
  • 举报
回复
目前的解决办法只能是Button1作为UpdatePanel2的PostBackTrigger
sharpblade 2009-04-07
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 hdjjun 的回复:]
引用 13 楼 sharpblade 的回复:
愣是没看到楼主的想法.
写代码玩?
2个button,一个label,有必要用2个updatepanel吗?


这个例子是我项目中的简化了的,实际项目中updatePanel里还有很多控件,所以我就不把无关的东西贴出来了
[/Quote]

o,这样说比较能理解了.
不过这样会隐藏了很多信息
以前做项目,遇到过靠HiddenField保存信息无法更新的情况.

可以试一下,把button1做为updatepanel的trigger.
sharpblade 2009-04-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hdjjun 的回复:]
没明白2楼的意思,
UpdatePanel2.update() 没起到作用呀,我想在
Button2_Click事件中再更新
[/Quote]

2楼的意思.
用代码让UpdatePanel2更新.
你不是就想UpdatePanel2更新嘛
hdjjun 2009-04-07
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 sharpblade 的回复:]
愣是没看到楼主的想法.
写代码玩?
2个button,一个label,有必要用2个updatepanel吗?
[/Quote]

这个例子是我项目中的简化了的,实际项目中updatePanel里还有很多控件,所以我就不把无关的东西贴出来了
hdjjun 2009-04-07
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 sharpblade 的回复:]
愣是没看到楼主的想法.
写代码玩?
2个button,一个label,有必要用2个updatepanel吗?
[/Quote]

这个例子是我项目中的简化了的,实际项目中updatePanel里还有很多控件,所以我就不把无关的东西贴出来了
hdjjun 2009-04-07
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 ojlovecd 的回复:]
引用 11 楼 hdjjun 的回复:
引用 7 楼 ojlovecd 的回复:
已测试通过:

C# code
ScriptManager.RegisterStartupScript(this, typeof(string), "key", "chg();", true);


是这样吗?
protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, typeof(string), "key", "chg();", true);
}
也没效果呀

你自己的代码应该是没有…
[/Quote]

在IE7下测的,前后台代码都有执行,但是代码执行后updatepanel2没刷新,不明白为啥?Button2不是在UpdatePanel2里的吗?为什么Button2事件触发了,UpdatePanel2没刷新,而且,如果直接点击Button2结果很正常。
sharpblade 2009-04-07
  • 打赏
  • 举报
回复
愣是没看到楼主的想法.
写代码玩?
2个button,一个label,有必要用2个updatepanel吗?
我姓区不姓区 2009-04-07
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 hdjjun 的回复:]
引用 7 楼 ojlovecd 的回复:
已测试通过:

C# code
ScriptManager.RegisterStartupScript(this, typeof(string), "key", "chg();", true);


是这样吗?
protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, typeof(string), "key", "chg();", true);
}
也没效果呀
[/Quote]
你自己的代码应该是没有问题的,你是iE下还是ff下测试的?
hdjjun 2009-04-07
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ojlovecd 的回复:]
已测试通过:

C# code
ScriptManager.RegisterStartupScript(this, typeof(string), "key", "chg();", true);
[/Quote]

是这样吗?
protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, typeof(string), "key", "chg();", true);
}
也没效果呀
阿非 2009-04-07
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 Sandy945 的回复:]
C# code
protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "key", "chg();", true);
}
[/Quote]


protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(Page, GetType(), "key", "chg();", true);
}

你可以换成它所在的容器
BernardSun 2009-04-07
  • 打赏
  • 举报
回复

protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "key", "document.getElementById('Button2').click();", true);


脚本直接写
我姓区不姓区 2009-04-07
  • 打赏
  • 举报
回复
另:用你的例子测试也是可以通过的
我姓区不姓区 2009-04-07
  • 打赏
  • 举报
回复
已测试通过:

ScriptManager.RegisterStartupScript(this, typeof(string), "key", "chg();", true);
hdjjun 2009-04-07
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 Sandy945 的回复:]
C# code
protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "key", "chg();", true);
}
[/Quote]
用这个也没效果,呵呵
阿非 2009-04-07
  • 打赏
  • 举报
回复

protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "key", "chg();", true);
}

hdjjun 2009-04-07
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 llnet 的回复:]
为什么不放在一个updatepanel里?
[/Quote]
因为我现在的项目中 updatepanel1里还有其它按钮或控件,当这些按钮或控件触发事件时又不想让它刷updatepanel2的内容,所以没放在同一个updatepanel中
llnet 2009-04-07
  • 打赏
  • 举报
回复
为什么不放在一个updatepanel里?
hdjjun 2009-04-07
  • 打赏
  • 举报
回复
没明白2楼的意思,
UpdatePanel2.update() 没起到作用呀,我想在
Button2_Click事件中再更新
hecong875 2009-04-07
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button2_Click(object sender, EventArgs e)
{
Label1.Text += " chged!";
}
protected void Button1_Click(object sender, EventArgs e)
{
// ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "key", "chg();", true);
UpdatePanel2.upate();
}
}



这样调用的...
在后台调用的时候 调用1的话就是UpdatePanel1.update();
如果调用2的话就是UpdatePanel2.update();

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧