博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

今天在写代码中出现当FileUpload控件上传图片的时候无法获得文件名问题!

代码如下

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
<title>Untitled Page</title>
</head>
<body>
    
<form id="form1" runat="server">
        
<asp:ScriptManager ID="ScriptManager1" runat="server" />
        
<asp:updatepanel ID="Updatepanel1" runat="server">
        
<ContentTemplate>
        
<div>
            
<asp:FileUpload ID="FileUpload1" runat="server" />
            
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        
</div>
        
</ContentTemplate>
        
</asp:updatepanel>
        
<asp:Label ID="Label1" runat="server" Height="98px" Text="Label" Width="228px"></asp:Label>
    
</form>
</body>
</html>

 

    protected void Button1_Click(object sender, EventArgs e)
    
{
        
string fileName = FileUpload1.PostedFile.FileName;

        
if (fileName == null)
        
{
            Label1.Text 
= "failed";
        }

        
else
        
{
            Label1.Text 
= "success";
        }
        
    }

运行的时候会在给fileName赋值时发生错误“未将对象引用设置到对象的实例”
当将UpdatePanel的范围缩小到FileUpload控件范围时问题就不会出现!

继续研究中.......