更新或刷新用户控件whitout刷新所有页面 [英] update or refresh user control whitout refresh all pages
本文介绍了更新或刷新用户控件whitout刷新所有页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我在usercontrol中的代码:
hi this is my code in usercontrol :
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Captcha.ascx.cs" Inherits="Controls_Captcha" %>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
width: 203px;
}
</style>
<asp:UpdatePanel runat="server" ID="UpdatePanel1" >
<ContentTemplate>
<table class="auto-style1">
<tr>
<td class="auto-style2">
<asp:Image ID="Image1" runat="server" Height="100px" Width="200px" />
</td>
<td>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GivImage();
}
}
private void GivImage()
{
try
{
bitmap = new Bitmap(200, 100, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Point img = new Point(200, 100);
creatImage(bitmap);
string virtualFolder = "~/GigPics/";
string physicalFolder = Server.MapPath(virtualFolder);
bitmap.Save(physicalFolder + "7514bd.gif", ImageFormat.Gif);
Image1.ImageUrl = virtualFolder + "7514bd.gif";
Graphic.Dispose();
bitmap.Dispose();
}
catch (Exception) { throw; }
}
现在我在测试页面中使用此控件:
now i use this control in test page :
<div>
<asp:UpdatePanel runat="server" ID="up1">
<ContentTemplate> <NezamControl:Captcha runat="server" ID="Captcha" /> </ContentTemplate>
</asp:UpdatePanel>
</div>
现在我想要当我点击Button conrol变为刷新更改图片whiteout刷新所有页面测试我该怎么做
now i want when i click on Button conrol become refresh for change picture whiteout refresh all pages test how can i do it
推荐答案
Use this and no require of update panel
(1) Remove Update panel and set
-------------------------------
<table class="auto-style1">
<tr>
<td class="auto-style2">
<asp:Image ID="Image1" runat="server" Height="100px" Width="200px" />
</td>
<td>
<asp:Button ID="Button1" runat="server" OnClientClick="javascript:GivImage(); return false;" Text="Button" />
</td>
</tr>
</table>
(2) Add jquery refrence and some javascript code
---------------------------------------------------
<script src="../Script/jquery-1.6.4.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
function GivImage()
{
.ajax({
type:POST,
url:'demo.aspx / GivImage',
contentType:application / json; charset = utf-8,
dataType:json,
success:function(data,textStatus){
var src = data.d.toString()。replace('〜' ,'');
.ajax({ type: "POST", url: 'demo.aspx/GivImage', contentType: "application/json; charset=utf-8", dataType: "json", success: function(data, textStatus) { var src= data.d.toString().replace('~','');
('#<% = Image1.ClientID %> ')。attr('src',src);
},
错误:函数(数据,状态,错误){
alert( 抱歉生成新的aptcha时出现问题。;
}
});
}
< / script >
(3)在代码方面添加名称空间
-------------------------------------
使用System.Web.Services;
使用System.Drawing;
使用System.Drawing.Imaging;
(4)为web服务创建常规方法
---------- -----------------------------------
protected void Page_Load(object sender,EventArgs e)
{
if(!IsPostBack)
{
Image1.ImageUrl = GivImage();
}
}
[WebMethod]
私人字符串GivImage()
{
尝试
{
位图位图=新位图(200,100,System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Point img = new Point(200,100);
creatImage(bitmap);
string virtualFolder =〜/ GigPics /;
string physicalFolder = Server.MapPath(virtualFolder);
bitmap.Save(physicalFolder +7514bd.gif,ImageFormat.Gif);
返回virtualFolder +7514bd.gif;
}
catch(Exception){throw; }
}
('#<%=Image1.ClientID%>').attr('src',src); }, error: function(data, status, error) { alert('Sorry problem in generate new aptcha.'); } }); } </script> (3) In Code side add namespace ------------------------------------- using System.Web.Services; using System.Drawing; using System.Drawing.Imaging; (4) Create your normal method to web service --------------------------------------------- protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Image1.ImageUrl = GivImage(); } } [WebMethod] private string GivImage() { try { Bitmap bitmap = new Bitmap(200, 100, System.Drawing.Imaging.PixelFormat.Format32bppArgb); Point img = new Point(200, 100); creatImage(bitmap); string virtualFolder = "~/GigPics/"; string physicalFolder = Server.MapPath(virtualFolder); bitmap.Save(physicalFolder + "7514bd.gif", ImageFormat.Gif); return virtualFolder + "7514bd.gif"; } catch (Exception) { throw; } }
这篇关于更新或刷新用户控件whitout刷新所有页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文