本文介绍了为什么我的File.HasFile总是返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
用户将输入一个客户号码,然后选择要上传的文件,然后输入描述并按保存按钮。但是,当我检查他们是否选择了一个文件,它总是返回false!我想知道为什么。编辑:我现在使用参数enctype =multipart / form-data的形式,这是包装在更新面板。
下面是错误处理的代码片段,它取得了HasFile()的值:
else if((images.Selected == true || docs.Selected == true)&&
(Upload.HasFile == false || txtInfo.Text == string.Empty))
{
if(Upload.HasFile == false)
lblErrorMessage.Text =您没有选择文件;
else lblErrorMessage.Text =您必须输入描述;
validated = false;
$ b $ p
$ b 这里的方法来自保存按钮$ /
protected void btnSaveNew_Click(object sender,EventArgs e)
{
bool validated = true;
lblErrorMessage.ForeColor = System.Drawing.Color.Red;
if(txtCustomerNumber.Text.Length!= 8)
{
if(txtCustomerNumber.Text.Length == 0)
lblErrorMessage.Text =Hey!客户号码怎么样?
else if(txtCustomerNumber.Text.Length> 8)
lblErrorMessage.Text =无效的客户编号长度(+(txtCustomerNumber.Text.Length - 8)+char(s)too long) ;
else lblErrorMessage.Text =无效的客户编号长度(+(8 - txtCustomerNumber.Text.Length)+char(s)short);
validated = false;
}
else if(links.Selected == true&&(txtLink.Text == string.Empty || txtInfo.Text == string.Empty))
{
if(txtLink.Text == string.Empty)
lblErrorMessage.Text =嘿,你忘了输入链接!
else lblErrorMessage.Text =您必须输入描述;
validated = false;
}
else if((images.Selected == true || docs.Selected == true)&&
(Upload.HasFile == false || txtInfo .Text == string.Empty))
{
if(Upload.HasFile == false)
lblErrorMessage.Text =您没有选择文件。
else lblErrorMessage.Text =您必须输入描述;
validated = false;
else if(txtInfoDesc.Text == string.Empty || txtInfo.Text == string.Empty)
{
if(txtInfoDesc.Text = = string.Empty)
lblErrorMessage.Text =您必须输入信息描述;
else lblErrorMessage.Text =您必须输入信息数据;
validated = false;
if(validated == false)
{
btnModify.Visible = false;
btnCreateNew.Visible = false;
ddlCustomerNumber.Visible = false;
btnSaveNew.Visible = true;
btnCancel.Visible = true;
txtCustomerNumber.Visible = true;
switch(rblSection.SelectedIndex)
{
case 0:txtInfo.Visible = true;
txtLink.Visible = true;
break;
案例1:
案例2:txtInfo.Visible = true;
Upload.Visible = true;
break;
案例3:txtInfo.Visible = true;
txtInfoDesc.Visible = true;
break;
$ b $ if(GetCategoryIDCookie()!= 0)
divData.Attributes.Add(Style,overflow:auto);
}
else
{
addNewCustomerNumber(txtCustomerNumber.Text,txtInfoDesc.Text,txtInfo.Text);
ddlCustomerNumber.Visible = true;
divData.Attributes.Remove(Style);
Response.Redirect(Request.RawUrl);
$ div $解析方案
有一个更新面板包裹在文件上传?
The user will enter a customer number, then select a file to upload, then enter a description and press the save button. However, when I check to see if they've selected a file, it always returns false! I'd like to know why.
Edit: I am now using form with parameter enctype="multipart/form-data" and this is wrapped in an update panel. Both the form and update panel are in a master page
Here's the snippet of code that error handles and is getting the HasFile() value:
else if ( ( images.Selected == true || docs.Selected == true ) &&
( Upload.HasFile == false || txtInfo.Text == string.Empty ) )
{
if ( Upload.HasFile == false )
lblErrorMessage.Text = "You haven't selected a file";
else lblErrorMessage.Text = "You must enter a description";
validated = false;
}
And here's the method that came from, the save button
protected void btnSaveNew_Click( object sender, EventArgs e )
{
bool validated = true;
lblErrorMessage.ForeColor = System.Drawing.Color.Red;
if ( txtCustomerNumber.Text.Length != 8 )
{
if ( txtCustomerNumber.Text.Length == 0 )
lblErrorMessage.Text = "Hey! What about the customer number?";
else if ( txtCustomerNumber.Text.Length > 8 )
lblErrorMessage.Text = "Invalid Customer Number length (" + ( txtCustomerNumber.Text.Length - 8 ) + " char(s) too long)";
else lblErrorMessage.Text = "Invalid Customer Number length (" + ( 8 - txtCustomerNumber.Text.Length ) + " char(s) short)";
validated = false;
}
else if ( links.Selected == true && ( txtLink.Text == string.Empty || txtInfo.Text == string.Empty ) )
{
if ( txtLink.Text == string.Empty )
lblErrorMessage.Text = "Hey! You forgot to enter a link!";
else lblErrorMessage.Text = "You must enter a description";
validated = false;
}
else if ( ( images.Selected == true || docs.Selected == true ) &&
( Upload.HasFile == false || txtInfo.Text == string.Empty ) )
{
if ( Upload.HasFile == false )
lblErrorMessage.Text = "You haven't selected a file";
else lblErrorMessage.Text = "You must enter a description";
validated = false;
}
else if ( txtInfoDesc.Text == string.Empty || txtInfo.Text == string.Empty )
{
if ( txtInfoDesc.Text == string.Empty )
lblErrorMessage.Text = "You must enter an info description";
else lblErrorMessage.Text = "You must enter info Data";
validated = false;
}
if ( validated == false )
{
btnModify.Visible = false;
btnCreateNew.Visible = false;
ddlCustomerNumber.Visible = false;
btnSaveNew.Visible = true;
btnCancel.Visible = true;
txtCustomerNumber.Visible = true;
switch ( rblSection.SelectedIndex )
{
case 0: txtInfo.Visible = true;
txtLink.Visible = true;
break;
case 1:
case 2: txtInfo.Visible = true;
Upload.Visible = true;
break;
case 3: txtInfo.Visible = true;
txtInfoDesc.Visible = true;
break;
}
if ( GetCategoryIDCookie() != 0 )
divData.Attributes.Add( "Style", "overflow:auto" );
}
else
{
addNewCustomerNumber( txtCustomerNumber.Text, txtInfoDesc.Text, txtInfo.Text );
ddlCustomerNumber.Visible = true;
divData.Attributes.Remove( "Style" );
Response.Redirect( Request.RawUrl );
}
}
解决方案
Is there an update panel wrapped around the File Upload?
这篇关于为什么我的File.HasFile总是返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!