在UpdatePanel里使用FileUpload控件

曾在开发ATLAS时候,想用UpdatePanel (UP)来上传文件,但是没有想到FileUpload (FU)控件不能在UP里使用,这里有个小技巧,可以让你的FU控件在UP里面起做用. 来看代码:

HTML:

    <div>
        
<atlas:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true">
        
</atlas:ScriptManager>
        
<atlas:UpdatePanel ID="UpdatePanel1" runat="server">
            
<ContentTemplate>
                
<asp:FileUpload ID="FileUpload1" runat="server" /><asp:Button ID="cmdButton1" runat="server"
                    Text
="Upload" /><asp:Label ID="Label1" runat="server" Text=""></asp:Label>
            
</ContentTemplate>
        
</atlas:UpdatePanel>
   
        
<asp:Button ID="cmdButton2" OnClick="cmdButton2_click"  runat="server" Text="Full post back" /> 
        
</div>

CODE BEHIND:
    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load

        
Me.cmdButton1.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(Me.cmdButton2, ""))

    
End Sub


    
Protected Sub cmdButton2_Click(ByVal sender As ObjectByVal e As System.EventArgs)
        
If Me.FileUpload1.HasFile Then
            System.Threading.Thread.Sleep(
1000)
            
Me.Label1.Text = Me.FileUpload1.FileName
        
End If
    
End Sub


这样的话,当你在点击UP里面的UPLOAD按钮时,整个页面就会回传,当然你的可以找到你FU控件里面的文件,所以上传文件是没问题. OK~

后来考虑到界面问题,想把button2按钮给隐藏掉,然后设置button2的visible的属性为false.运行是却出现了如下的错误:
回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。
把button2的visible的属性重新该为true,问题就不在重新出现,现在对此不能理解
麻烦知道的朋友告诉一声.谢谢!!!
posted on 2007-03-22 11:37  Edwin dong  阅读(1869)  评论(3)    收藏  举报

<% Function googleColor(value, random) Dim colorArray colorArray = Split(value, ",") googleColor = colorArray(random Mod (UBound(colorArray) + 1)) End Function Function googleScreenRes() Dim screenRes, delimiter, resArray screenRes = Request.ServerVariables("HTTP_UA_PIXELS") delimiter = "x" If IsEmpty(screenRes) Then screenRes = Request.ServerVariables("HTTP_X_UP_DEVCAP_SCREENPIXELS") delimiter = "," End If resArray = Split(screenRes, delimiter, 2) If (UBound(resArray) + 1) = 2 Then googleScreenRes = "&u_w=" & resArray(0) & "&u_h=" & resArray(1) End If End Function Function googleDcmguid() Dim dcmguid dcmguid = Request.ServerVariables("HTTP_X_DCMGUID") If Not IsEmpty(dcmguid) Then googleDcmguid = "&dcmguid=" & dcmguid End If End Function Dim googleTime, googleDt, googleScheme, googleHost googleTime = DateDiff("s", "01/01/1970 00:00:00", Now()) googleDt = (1000 * googleTime) + Round(1000 * (Timer - Int(Timer))) googleScheme = "http://" If StrComp(Request.ServerVariables("HTTPS"), "on") = 0 Then googleScheme = "https://" googleHost = Server.URLEncode(googleScheme & Request.ServerVariables("HTTP_HOST")) Dim googleAdUrl, googleAdOutput googleAdUrl = "http://pagead2.googlesyndication.com/pagead/ads?" &_ "ad_type=text_image" &_ "&channel=" &_ "&client=ca-mb-pub-6539345765131754" &_ "&dt=" & googleDt &_ "&format=mobile_single" &_ "&host=" & googleHost &_ "&ip=" & Server.URLEncode(Request.ServerVariables("REMOTE_ADDR")) &_ "&markup=xhtml" &_ "&oe=utf8" &_ "&output=xhtml" &_ "&ref=" & Server.URLEncode(Request.ServerVariables("HTTP_REFERER")) &_ "&url=" & googleHost & Server.URLEncode(Request.ServerVariables("URL")) &_ "&useragent=" & Server.URLEncode(Request.ServerVariables("HTTP_USER_AGENT")) &_ googleScreenRes() &_ googleDcmguid() Set googleAdOutput = Server.CreateObject("MSXML2.ServerXMLHTTP") googleAdOutput.Open "GET", googleAdUrl, false googleAdOutput.Send Response.Write(googleAdOutput.responseText) %>