世间所有的相遇都是久别重逢,比如说: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="编辑权重" οnclick="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="编辑权重" οnclick="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的效果,也解决了异步刷新时不弹出对话框的问题。