如何在文件上传控件中更改按钮文本?

时间:2013-12-19 09:05:30

标签: c# asp.net button

我尝试更改文件上传控件浏览按钮的文本。我进行了文件上传控件visible=false,然后添加了另一个文本框和按钮:

.aspx文件:

<asp:FileUpload ID="fuUploadPhoto" runat="server" visible="false"/>
<asp:TextBox ID="tbFilePath" runat="server" />
<asp:Button ID="btnChooseFile" runat="server" Text="Choose file from disk" />

接下来,我尝试在.c中以PageLoad为btnChooseFile添加属性。不幸的是它不起作用,我不知道为什么。我犯了什么错误?

.cs文件:

protected void Page_Load(object sender, EventArgs e)
    {
        btnChooseFile.Attributes.Add("onclick", "document.getElementById(" + fuUploadPhoto.ClientID + ").click()");
       MultiViewAddPhoto.SetActiveView(viewAddPhotoStepOne);
    }

protected void btnChooseFile_Click(object sender, EventArgs e)
    {

        if (fuUploadPhoto.HasFile)
        {
            tbFilePath.Text = fuUploadPhoto.PostedFile.FileName;
            string filename = Path.GetFileName(fuUploadPhoto.FileName);
            string ext = Path.GetExtension(filename);
            imageGuid = Guid.NewGuid();

            string contenttype = String.Empty;
            switch (ext)
            {
                case ".jpg":
                    contenttype = "image/jpg";

                    break;
                case ".jpeg":
                    contenttype = "image/jpg";

                    break;
                case ".png":
                    contenttype = "image/png";

                    break;
            }


            if (string.IsNullOrEmpty(contenttype))
            {
                ltrErrorMessage.Text = "Nieprawidłowy format pliku!";
            }
            //prawidłowy format pliku
            else
            {

                if (fuUploadPhoto.PostedFile.ContentLength > MyConsts.DAL.SizeOfPhoto)
                {
                    ltrErrorMessage.Text = "Plik może mieć maksymalnie "+ MyConsts.DAL.SizeOfPhoto/1024 + " Mb! Zmniejsz plik i spróbuj ponownie.";
                }
                //jeśli prawidłowy format i rozmiar zdjęcia
                else
                {
                    try
                    {
                        filePath = ConfigurationManager.AppSettings["FilesPath"] + "\\" + Request.QueryString["konkurs"] + "\\" + imageGuid + ext;
                        path = "\\" + Request.QueryString["konkurs"] + "\\" + imageGuid + ext;

                        //zapisujemy plik na dysk
                        fuUploadPhoto.SaveAs(filePath);

                        if (File.Exists(filePath))
                        {
                            imgInspirationPhoto.ImageUrl = filePath;
                            imgInspirationPhoto.Visible = true;
                        }
                        else
                        {
                            imgInspirationPhoto.Visible = false;
                        }

                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex.Message, LogSource, ex); 
                    }

                }
            }
        }

    }

3 个答案:

答案 0 :(得分:1)

当你使fileupload可见时,它将不会在页面上呈现,即它不是隐藏但不存在。因此,它显示无,而不是可见的虚假。

试试这个

protected void Page_Load(object sender, EventArgs e)
{
    btnChooseFile.Attributes.Add("onclick", "jQuery('#" + fuUploadPhoto.ClientID + "').click();return false;");
   //MultiViewAddPhoto.SetActiveView(viewAddPhotoStepOne);
}

在aspx文件中:

<div style="display:none;">
<asp:FileUpload ID="fuUploadPhoto" runat="server"/>
</div>

记得在aspx页面中添加对jQuery库的引用;

更新:在完全回发This solution might help

之前,该文件在后面的代码中不可用

答案 1 :(得分:1)

使用两个js文件http://the-echoplex.net/demos/upload-file/file-upload.jshttp://the-echoplex.net/demos/upload-file/jelly/min.js。并添加file-upload.css文件。您的样本 aspx文件是,

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <script src="script/jelly.js" type="text/javascript"></script>  
   <style type="text/css">  
 /****************** Start page styles ********************************************/  
 body {  
      background: #DFA01B;  
      font-family: arial, sans-serif;  
      font-size: 11px;  
      }  
 #wrap {  
      max-width: 600px;  
      margin: 30px auto;  
      background: #fff;  
      border: 4px solid #FFD16F;  
      -moz-border-radius: 15px;  
      -webkit-border-radius: 15px;  
      border-radius: 15px;  
      padding: 20px;  
   }  
 .field {   
      padding: 0 0 1em;   
      }  
 </style>  
 <head runat="server">  
   <title></title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div id="wrap">  
 <form enctype="multipart/form-data" action="#" method="post">  
      <div class="field">  
           <label class="file-upload">  
                <span><strong>Put YOUR TEXT</strong></span>  
                <%--<input type="file" name="uploadfile" onclintclick="test_load()" />--%>  
       <asp:FileUpload  
         ID="FileUpload1" name="uploadfile" runat="server"   
       ondatabinding="FileUpload1_DataBinding" />  
           </label>  
      </div>  
 </form>  
 </div><!--/ wrap -->  
   <script src="script/file-upload.js" type="text/javascript"></script>  
   </form>  
 </body>  
 </html>  

和CSS文件,

body {
}
/* 

    As this stylesheet is lazy loaded these styles only apply if JavaScript is enabled

*/  
.file-upload {
    overflow: hidden;
    display: inline-block;
    position: relative; 
    vertical-align: middle;
    text-align: center;

    /* Cosmetics */
    color: #fff;
    border: 2px solid #2FA2FF;
    background: #6FBEFF;

    /* Nice if your browser can do it */
    -moz-border-radius: 8px;
    -webkit-border-radius: 8px;
    border-radius: 8px;
    text-shadow: #000 1px 1px 4px;
    }

.file-upload:hover { 
    background: #2FA2FF; 
    }

.file-upload.focus { 
    outline: 2px solid yellow;
    }

.file-upload input {
    position: absolute;
    top: 0;
    left: 0;
    margin: 0;
    font-size: 70px;

    /* Loses tab index in webkit if width is set to 0 */
    opacity: 0;
    filter: alpha(opacity=0);
    }

.file-upload strong { 
    font: normal 1.75em arial,sans-serif; 
    }   

.file-upload span {
    position: absolute;
    top: 0;
    left: 0;
    display: inline-block;

    /* Adjust button text vertical alignment */
    padding-top: .45em;
    }

/* Adjust the button size */    
.file-upload { height: 3em; }
.file-upload,
.file-upload span { width: 14em; }  

.file-upload-status {
    margin-left: 10px;
    vertical-align: middle;
    padding: 7px 11px;
    font-weight: bold;  
    font-size: 16px;
    color: #888;
    background: #f8f8f8;
    border: 3px solid #ddd;
    }

您可以在changedfileuploadbutton text

下载示例项目

答案 2 :(得分:0)

您无法使用标准的asp文件上传控件。

您可以创建自己的自定义控件,该控件继承自FileUpload,您可以在其中添加自定义行为:

public class MyFileUpload : FileUpload 
{
     //do stuff
}