方法一:
在UpdatePanel里面需要上傳文件。大家應(yīng)該知道UpdatePanel里面是不可以上傳文件的!不過我們可以變換下就可以了!
ASPX代碼如下:(跟正常的代碼是沒有什么區(qū)別的!)
<asp:UpdatePanel ID="UpdatePanel1" unat="server">
<ContentTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="上傳" onclick="Button1_Click" /> </ContentTemplate>
</asp:UpdatePanel>
C#代碼如下:也是最關(guān)鍵的
PostBackTrigger trigger =
new PostBackTrigger();
trigger.ControlID = Button1.UniqueID;
UpdatePanel1.Triggers.Add(trigger);
這里記得一定別放在if(!IsPostBack)里面。
下面說下c#代碼:就是給UpdatePanel增加一個(gè)回發(fā)控件,那為什么要用Button1.UniqueID。是應(yīng)為如果你這個(gè)控件放在一些面板控件里面,UpdatePanel是找不到回發(fā)控件的ID的。
本文出自 “lee” 博客,請(qǐng)務(wù)必保留此出處http://leehai.blog.51cto.com/757045/153736
本文出自 51CTO.COM技術(shù)博客
方法二:
如果將scriptmanager的enablepartialrending設(shè)為false則可以正確上傳,這個(gè)方法最簡(jiǎn)單,但是會(huì)有缺 陷,就是在同一個(gè)頁面上的多個(gè)UpdatePanel不可以獨(dú)自刷新了。另外,當(dāng)你的UpdatePanel中存在Validator的話,會(huì)造成整個(gè)頁 面postback,這個(gè)問題似乎是Altas的一個(gè)bug.http://hi.baidu.com/tree3200/blog/item/b894418b88f7e317c9fc7a16.html
方法三:新建主頁面Default.aspx
1:在適當(dāng)?shù)奈恢?,放置一個(gè)上傳附件的UpdatePanel區(qū)域
<atlas:UpdatePanel ID="up_attachment" Mode="Conditional" runat="server">
<ContentTemplate>
<asp:Image ID="img_photo" runat="server" Height="64" ImageUrl="~/images/anonymous.gif"
Width="64" /><br />
<input type="hidden" runat="server" id="hi_src" name="hi_src" value="~/images/anonymous.gif" />
<iframe id="file" name="file" src="attachment.aspx"></iframe>
</ContentTemplate>
</atlas:UpdatePanel> 2:新建上傳文件的頁面
attachment.aspx,然后放上FileUpload控件
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="OK" OnClick="Button1_Click" />
</div> 3:在
attachment.aspx里面,上傳文件之后調(diào)用主頁面的js:
protected void Button1_Click(object sender, EventArgs e)
{
string fileFullPath = fu_photo.PostedFile.FileName;
string fileName = fileFullPath.Substring(fileFullPath.LastIndexOf('\\') + 1);
string fileSavePath = "../Photos/" + fileName;
fu_photo.PostedFile.SaveAs(Server.MapPath(fileSavePath));
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "photoscript", "window.top.callBack('" + fileSavePath + "');", true);
4:Default.aspx主頁面里面增加這個(gè)函數(shù),處理返回值
用js改變圖片路徑為新上傳的路徑,然后服務(wù)器端獲的隱藏字段的值,即為新上傳圖片路徑
上傳頁面時(shí)不能獲得js更改后的image控件的屬性值,所以添加一個(gè)隱藏字段。。。 <script>
function callBack(fileName)
{
document.getElementById('<%=img_photo.ClientID %>').src=fileName;
document.getElementById('<%=hi_src.ClientID %>').value=fileName;
}
</script> http://hi.baidu.com/jovifiona/blog/item/c4f41dd14487c1d2572c8404.html
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。