- 浏览: 677906 次
文章分类
最新评论
项目经历——解决ScriptManager和UpdatePanel局部刷新以及不弹出对话框问题
世间所有的相遇都是久别重逢,比如说:Asp.Net中的AJAX扩展中的UpdatePanel,原先在.Net视频学习中,只是简单的留意了一下,没想到在项目中,竟重逢了!
问题描述:
在项目中设定定性指标权重的时候,通过Easy-UI将不同的DIV转化成了四个Tab页面,分别是县市辖区,市直单位,开发园区,职工干部,要对各个Tab页面下的指标进行不同的操作,不可避免的要用到异步的局部刷新,于是就有了文章开头那句话:和UpdatePanel相遇了!ScriptManager和UpdatePanel:
在MSDN上,关于UpdatePanel的属性,事件,方法数不胜数,这里不一一列举,仅仅说几个较为重要的属性:
ScriptManager:EnablePartialRendering属性:true-实现页面的异步局部更新;false-实现全页面的刷新。
UpdatePanel:
RenderMode属性:InLine-UpdatePanel控件被解析成HTML的<span>标记;Block-UpdatePanel控件被解析成HTML控件的<DIV>。
UpdateMode属性:Always-UpdatePanel页面上任何一处发生的回发操作都会产生页局部更新;Conditional-只在特定的情况下才产页面的回发,如执行UpdatePanel控件的update()方法或在指定的触发器的操作下。
ChildAsTrigger属性:指示UpdatePanel内部控件引起的回发是否产生当前UpdatePanel控件的局部更新。如果UpdateMode设为Always的话,那ChildAsTrigger局性必须设为True,否则运行出错。
(更多MSDN详解)
UpdatePanel实现:
实例:页面加载的时候,显示一个时间,单击按钮,获取当前时间,页面不刷新从而实现两个时间不一致
HTML前台:
<asp:LabelID="Label2" runat="server"Text="Label"></asp:Label>
<asp:ScriptManagerID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanelID="UpdatePanel1" runat="server"ChildrenAsTriggers="True">
<ContentTemplate>
<asp:LabelID="Label1" runat="server"></asp:Label>
<br />
<asp:ButtonID="Button1" runat="server"OnClick="Button1_Click" Text="Button" />
</ContentTemplate>
</asp:UpdatePanel>
C#后台代码:
protected void Button1_Click(object sender, EventArgs e) { Label1.Text = DateTime.Now.ToString(); ; } protected void Page_Load(object sender, EventArgs e) { Label2.Text = DateTime.Now.ToString(); }
实例效果:每次点击按钮,页面加载下的时间不变,因为页面没有Load,而按钮下的时间在不断变换。
在项目中:
Html代码:
需要引用对应的Easy-UI和JS文件
<div title="县市辖区" style="padding: 10px" id="CityInfo"> <asp:ScriptManager ID="ScriptManager2" runat="server"></asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel2" runat="server"> <ContentTemplate> <div> 定量指标总权重: <asp:Label ID="lblCityWeight" runat="server" Text=' <%#Eval( "ObjectTypeId")%> ' Visible="false"></asp:Label><asp:TextBox ID="txtCityWeight" runat="server" ReadOnly="True"></asp:TextBox>%(单位百分比) </div> <div> <%--动态绑定县市辖区的定性指标权重和名称--%> <asp:Repeater ID="CityTarget" runat="server"> <HeaderTemplate> <table> <tbody> </HeaderTemplate> <ItemTemplate> <tr> <td> <asp:Label ID="Label1" runat="server" Text=' <%#Eval( "id")%> ' Visible="false"></asp:Label> </td> <%-- <td># ((DataRowView )Container.DataItem)["Name"]: <asp:TextBox ID="TextBox1" name="txtcity" OnTextChanged="cmdCity_Click" Class="EditWeight" Text='<%# ((DataRowView )Container.DataItem)["Weight"] %>' runat="server" ReadOnly="True"></asp:TextBox>%(单位百分比)</td> --%> <%-- <td><%# ((DataRowView )Container.DataItem)["Name"]%>:</td>--%> <td> <a title="<%#Eval("Name") %>"><%# StringTruncat( Eval("Name").ToString(),8 , "...") %></a></td> <td> <asp:TextBox ID="TextBox1" name="txtcity" OnTextChanged="cmdCity_Click" Class="EditWeight" Text='<%# ((DataRowView )Container.DataItem)["Weight"] %>' runat="server" ReadOnly="True"></asp:TextBox>%(单位百分比)</td> </tr> </ItemTemplate> <FooterTemplate> </tbody> </table> </FooterTemplate> </asp:Repeater> </div> <div> <%--<asp:Button ID="EditCity" runat="server" Text="编辑权重" OnClick="EditCity_Click" />--%> <input id="btneditCity" type="button" value="编辑权重" onclick="EditcityTxt()" /> <asp:Button ID="cmdCity" runat="server" Text="提交修改" OnClick="cmdCity_Click" /> </div> </ContentTemplate> </asp:UpdatePanel> </div> <div id="UnitTargetInfo" title="市直单位" style="padding: 10px" aria-selected="true"> <asp:UpdatePanel ID="UpdatePanel4" runat="server"> <ContentTemplate> <div> 定量指标总权重:<asp:Label ID="lblUnitWeight" runat="server" Text=' <%#Eval( "ObjectTypeId")%> ' Visible="false"></asp:Label> <asp:TextBox ID="txtUnitWeight" runat="server" ReadOnly="True"></asp:TextBox>%(单位百分比) </div> <div> <%--动态绑定市直单位的定性指标权重和名称--%> <asp:Repeater ID="UnitTarget" runat="server"> <HeaderTemplate> <table> <tbody> </HeaderTemplate> <ItemTemplate> <tr> <td> <asp:Label ID="Label1" runat="server" Text=' <%#Eval( "id")%> ' Visible="false"></asp:Label> </td> <%--<td><%# ((DataRowView )Container.DataItem)["Name"] %>:</td>--%> <td> <a title="<%#Eval("Name") %>"><%# StringTruncat( Eval("Name").ToString(),8 , "...") %></a></td> <td><asp:TextBox ID="TextBox1" Class="EditWeight" Text='<%# ((DataRowView )Container.DataItem)["Weight"] %>' runat="server" ReadOnly="True"></asp:TextBox>%(单位百分比)</td> </tr> </ItemTemplate> <FooterTemplate> </tbody> </table> </FooterTemplate> </asp:Repeater> </div> <div> <%--<asp:Button ID="EditCity" runat="server" Text="编辑权重" OnClick="EditCity_Click" />--%> <input id="btneditUnit" type="button" value="编辑权重" onclick="EditunitTxt()" /> <asp:Button ID="cmdUnit" runat="server" Text="提交修改" Style="height: 20px" OnClick="cmdUnit_Click" /> </div> </ContentTemplate> </asp:UpdatePanel> </div>
C#后台:
#region 单击修改县市区确定修改按钮事件 protected void cmdCity_Click(object sender, EventArgs e) { EvaluationSystem.Model.CharacterizationTargetWeightEntity characterizationTargetWeightEntity = new CharacterizationTargetWeightEntity(); EvaluationSystem.BLL.CharacterizationTargetWeightBLL characterizationTargetWeightBll = new CharacterizationTargetWeightBLL(); foreach (RepeaterItem ri in CityTarget.Items) { //获取修改后的权重 TextBox txtWeight = (TextBox)ri.FindControl("TextBox1"); //String str = txtWeight.UniqueID; string Weight = Request.Form[txtWeight.UniqueID]; Label labelid = (Label)ri.FindControl("Label1"); string TargetID = labelid.Text; //赋值给定性指标实体的各个属性 characterizationTargetWeightEntity.Id = TargetID; characterizationTargetWeightEntity.Weight = Weight; characterizationTargetWeightEntity.YearTime = DateTime.Now.Year.ToString(); //获取修改的定量指标 //获取修改后的定量指标考核对象类型,权重,时间 assignmentweighEntity.ObjectTypeId = lblCityWeight.Text; assignmentweighEntity.Weight = Request.Form[txtCityWeight.UniqueID]; assignmentweighEntity.YearTime = year; if (assignmentweighEntity.ObjectTypeId.Trim() == "") { if (characterizationTargetWeightBll.UpdateCharacterWeight(characterizationTargetWeightEntity)) { //修改文本框为不可编辑 //更新成功进行提示 txtWeight.Text = Weight; txtCityWeight.Text = "尚未设定"; ScriptManager.RegisterClientScriptBlock(UpdatePanel2, this.GetType(), "click", "alert('定量指标权重尚未设定,修改定性指标权重成功')", true); } else { //更新失败 ScriptManager.RegisterClientScriptBlock(UpdatePanel2, this.GetType(), "click", "alert('修改定性指标权重失败,请联系管理员')", true); } } else { if (assignmentweigh.UpdateWeight(assignmentweighEntity) && characterizationTargetWeightBll.UpdateCharacterWeight(characterizationTargetWeightEntity)) { //修改文本框为不可编辑 //更新成功进行提示 txtWeight.Text = Weight; txtCityWeight.Text = assignmentweighEntity.Weight; ScriptManager.RegisterClientScriptBlock(UpdatePanel2, this.GetType(), "click", "alert('修改权重成功')", true); } else { //更新失败 ScriptManager.RegisterClientScriptBlock(UpdatePanel2, this.GetType(), "click", "alert('修改权重失败,请联系管理员')", true); } } } } #endregion
出现问题及小结:
使用了UpdatePanel之后,可能会出现对话框不显示的问题。因为alert 就是界面刷新之后进行提示的,但是Updatepanel却不刷新页面,因此也就不可能弹出提示框了。对于这个问题,那是因为没有找到alert所在的‘宿主’,这个问题使用ScriptManager.RegisterClientScriptBlock(UpdatePanelID, this.GetType(), "click", "alert('消息')", true);既可以解决了。其中UpdatePanelID是html页面的的UpdatePanel的ID;UpdatePanel和ScriptManager作为Asp.net 封装好的用于异步刷新的控件,在很大程度上提升了系统的用户友好性,同时结合使用ScriptManager.RegisterClientScriptBlock,实现了Ajax的效果,也解决了异步刷新时不弹出对话框的问题。
相关推荐
ScriptManager和UpdatePanel控件实现页面异步局部更新效果,VS2008项目文件夹,内附详细的文档说明,文档说明包括空间属性和用法介绍及代码说明。个人实验。
scriptManager和updatepanel.docscriptManager和updatepanel.doc
所以,我们可以将ready事件中执行的代码提取出来,然后通过捕获ScriptManager的EndRequest事件,在每次 UpdatePanel局部刷新之后执行一次jQuery初始化代码: 代码如下: //处理ajax和ScriptManager的冲突 function ...
AJAX弹出框_利用ScriptManager.RegisterStartupScript
普通的asp.net 2.0应用程序中,如果需要弹出一个对话框,可以使用 Response.Write("<script>alert('弹出一个对话框');...在ajax页面中,不能使用上述方法,但是在AJAX中的ScriptManager却提供了弹出对话框的功能
Ajax UpdatePanel ScriptManager UpdateProgress 示例
问题重现: 1.ASP.NET AJAX在页面中添加ScriptManager和UpdatePanel 2.在UpdatePanel中添加元素A 3.用jQuery对元素A添加X效果 4.在UpdatePanel中加一个Button B用作postback 这样问题就出现了。分析1: UpdatePanel...
ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), “click”, “alert(‘删除成功”+queryId+”‘)”, true);解决 asp.net onClientClick 与 验证控件冲突问题[removed] function ...
安装简单 非常明了 并且有txt文字安装说明
vb.net建立普通web网页,添加System.Web.Extensions.dll,然后修改web.config,在程序中添加ScriptManager、UpdatePanel和Timer控件就能实现局部刷新。本程序中分别使用了Timer和Button两种触发方式。
通过将控件放入到updatepanel中,实现局部刷新。前台代码: 代码如下:”ScriptManager1″ runat=”server”> </asp> //必须有且写在updatepanel前面”server” id=”updatepanel1″><contenttemplate> ”server” ...
UpdatePanel控制页面的局部更新,这个更新功能依赖于scriptManger控件的EnablePartialRendering属性,如果这个属性设置为false局部更新会失去作用(scriptManger控件的EnablePartialRendering属性的默认值为true不必...
导读:在UpdatePanel控件异步更新时,如果有错误发生,默认情况下会弹出一个Alert对话框显示出错误信息,这对用户来说是不友好的,本文看一下如何在服务端和客户端脚本中自定义异常处理,翻译自官方文档。...
安装简单 并且里面有txt文字说明 没资源分的留言联系我 发给大家
服务器标记“asp-ScriptManager”不明确解决参考.docx
使用ScriptManager控件实例代码
代码如下:”ScriptManager1″ runat=”server”> 使用VS控件updatePanel实现无刷新 ”UpdatePanel1″ RenderMode=”Block” UpdateMode=”Conditional” runat=”server”> 点击按钮从服务器获取当前...
微软Microsoft Visual Studio下.net3.5自带ajax页面无刷新技术示例程序。 AJAX Extensions的使用ScriptManager、UpdatePanel的使用示例 带有注释说明(简单易懂) 本示例程序由VS2010创建
AJAX中的ScriptManager实战,对AJAX微软框架中scriptmanager功能的介绍..
于是上网查了下。。。 应该使用 代码如下: System.Web.UI.ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), “Button6_Click”, “alert(‘请输入aaa!’)”, true);