如何更改FileUpload控件中的按钮文本并将值设置为文本框 [英] How to change Text of button in FileUpload control and set value to textbox

查看:300
本文介绍了如何更改FileUpload控件中的按钮文本并将值设置为文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想使用c#



上传文件1.我想将按钮文本从默认的浏览更改为上传。



2.当用户返回此页面时,我想显示他/她之前在文本框中上传的图像名称。



经过一些搜索,我发现它无法编辑按钮名称和文本框的设置值。



所以我开发了以下编码。



.ASPX



Hi I want to upload file using c#

1. I want to change the text of button from default "Browse" to "Upload".

2.When User return to this page I want to show image name he/she previously upload in textbox.

After some search I found its not possible to edit button name and set value for textbox.

So I developed below coding.

.ASPX

<input runat="server" id="File1" type="file" style=" visibility:hidden;" />
                     <input id="Text1" type="text" runat="server" />
                     <asp:Button ID="Button1" OnClientClick="fireFileClick()" runat="server" Text="Upload" />

Js



Js

function fireFileClick() {
            var objfile = document.getElementById("<%= File1.ClientID %>");
            objfile.click();


            var objTextBox = document.getElementById("<%= Text1.ClientID %>");
            objTextBox.value = objfile.value;
        }



.cs




.cs

private void SetPicture()
{
string strFileName = System.IO.Path.GetFileName(File1.PostedFile.FileName);
            string strExtnt = strFileName.Substring(strFileName.LastIndexOf('.') + 1).ToUpper();
            System.Drawing.Image UploadedImage = System.Drawing.Image.FromStream(File1.PostedFile.InputStream);
            float UploadedImageWidth = UploadedImage.PhysicalDimension.Width;
            float UploadedImageHeight = UploadedImage.PhysicalDimension.Height;
            if (UploadedImageWidth < 185 && UploadedImageHeight < 51)
            {
}
}



问题在于.CS我没有获取文件名和路径。变量strFileName,strExtnt返回空。一些身体指导我。


The problem is in .CS im not getting file name and path. the variable strFileName , strExtnt is returning empty. some body guide me.

推荐答案

嘿那里,



问题是,你的按钮正在打开文件对话框获得点击回发。



所以你需要进行以下更改:

首先更改你的按钮:
Hey there,

The problem is, your button that is opening the file dialog gets a postback on click.

So you need to make the following changes:
First change your button with:
<asp:button id="Button1" onclientclick="return fireFileClick();" runat="server" text="Upload" />



并添加return false;在你的JavaScript函数中。

其次,你试图在文本框中显示所选的值,这不会像你那样工作。



在你的fileupload控件中添加onchange事件,如下所示:


and add return false; in your JavaScript function.
Secondly, you are trying to show the selected value in the textbox, which won't work the way you are doing it.

Add onchange event in your fileupload control like this:

<input  runat="server" id="File1" type="file" style=" visibility:hidden;"  önchange="myFunction(this);" />



您的JavaScript函数最终应该包含以下格式。




and your JavaScript functions should have the following form in the end.

function fireFileClick() {
           var objfile = document.getElementById("<%= File1.ClientID %>");
           objfile.click();

           return false;
       }




function myFunction(objfile) {
            var objTextBox = document.getElementById("<%= Text1.ClientID %>");
            objTextBox.value = objfile.value;
        }





希望有所帮助。



祝你好运。



Azee ......



Hope it helps.

Good luck.

Azee...


这篇关于如何更改FileUpload控件中的按钮文本并将值设置为文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆