当前位置: 移动技术网 > IT编程>开发语言>.net > [转载].NET ASP.NET 中web窗体(.aspx)利用ajax实现局部刷新

[转载].NET ASP.NET 中web窗体(.aspx)利用ajax实现局部刷新

2019年10月15日  | 移动技术网IT编程  | 我要评论

 

c#所有runat="server"的控件都会造成整个界面的刷新,如果想实现局部刷新,可以利用ajax。需要加入的控件有scriptmanager和updatepanel,可以实现只刷新updatepanel内的内容。
c#中已经将ajax封装到了一个控件中,可以很简单的实现,使用方法有点类似panel:
例:在前台aspx文件中:

<body>
<form id="form1" runat="server">
<asp:scriptmanager id="scriptmanager1" runat="server"></asp:scriptmanager><br />
<div style="text-align: center; color: blue; font-size: larger">
不需要刷新的内容
</div>
<div style="text-align: center">
<asp:updatepanel id="updatepanel1" runat="server">
<contenttemplate>
<asp:datalist id="datalist1" runat="server"></asp:datalist>
</contenttemplate>
</asp:updatepanel>
<br />
<br />
<div id="divbutton" style="text-align: center"></div>
</div>
</form>
</body>

 

其中 <asp:scriptmanager> 就是实现ajax的控件,需添加在所有页面内容的最上面,扔在最上面就行,标签之间不用添加内容。<asp:updatepanel> 以</asp:updatepanel>结尾,在该区域内的刷新内容时,不会刷新整个页面,标签之间为局部刷新的内容。

后台页面,只需要按照原来的需要对datalist等进行相应的操作,不需要对<asp:scriptmanager >, <asp:updatepanel>进行操作,另外,<asp:scriptmanager >,<asp:updatepanel >在vs2008之后,可以在工具箱(toolbox)中的ajax extensions 下找到,在vs2005中,需要安装ajax插件才能使用。
注意事项:
(1)scriptmanager要放在updatepanel的前面。
(2)updatepanel内的控件只能影响updatepanel内的控件,不能影响外面的控件,而外面的控件则可以影响updatepanel内的。
常见错误
(1)类型“system.web.ui.updatepanel”不具有名为“dropdownlist”的公共属性
解决方法:其实原因很简单。就是少了一个<contenttemplate></contenttemplate>
例:

<asp:scriptmanager id="scriptmanager1" runat="server"></asp:scriptmanager>
<asp:updatepanel id="updatepanel1" runat="server">
<asp:dropdownlist id="ddlused" runat="server">
<asp:listitem text="百度" value="-1"></asp:listitem>
<asp:listitem text="谷歌" value="0"></asp:listitem>
<asp:listitem text="新浪" value="1"></asp:listitem>
</asp:dropdownlist>
</asp:updatepanel>

 

正确的如下:

<asp:scriptmanager id="scriptmanager1" runat="server"></asp:scriptmanager>
<asp:updatepanel id="updatepanel1" runat="server">
<contenttemplate>
<asp:dropdownlist id="ddlused" runat="server">
<asp:listitem text="百度" value="-1"></asp:listitem>
<asp:listitem text="谷歌" value="0"></asp:listitem>
<asp:listitem text="新浪" value="1"></asp:listitem>
</asp:dropdownlist> 
</contenttemplate> 
</asp:updatepanel>

 

(2)类型“scriptmanager”的控件“scriptmanager1”必须放在具有 runat=server 的窗体标记内

错误原因:<asp:scriptmanager id=“scriptmanager1” runat=“server”></asp:scriptmanager>这个内容必须放在<form>标记内。
————————————————
版权声明:本文为csdn博主「用得到就留个赞gh」的原创文章,遵循 cc 4.0 by-sa 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_43434300/article/details/100092123

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网