【问题标题】:How to call manually the onchange event of fileupload control with C#如何使用C#手动调用fileupload控件的onchange事件
【发布时间】:2015-08-19 14:45:23
【问题描述】:

我有一个带有文件上传控制器和图像的网页。

 <img src='#' id='imageId' alt='your image' height='64' width='64' src='placeholder.png' class='placeholder' >

 <input type='file' id='myID' onchange='previewImage(this)' accept='image/*' data-thumbnail='imageId'>

其实上面的HTML代码是我的asp代码衍生出来的。

好的..现在我要做的是使用手动编码生成这两个组件。
所以我就这样开始了。

    System.Web.UI.WebControls.Image preview = new System.Web.UI.WebControls.Image();
    preview.ID = "imageId";
    preview.Height = 64;
    preview.Width = 64;

    FileUpload tt = new FileUpload();
    tt.ID = "myID";

但是原始HTML代码中还保留了一些属性。我不知道如何用C#代码来实现它们。

此外,必须实现文件上传控件的“onchange”事件。

“previewImage(this)”是我用来预览所选图像的 javascript 函数。

sp请帮我用c#代码解决这些问题。

【问题讨论】:

    标签: javascript c# asp.net


    【解决方案1】:

    试试这个,

    tt.Attributes.Add("onchange", "previewImage()");
    

    【讨论】:

    • 用 previewImage() 我需要传递参数“this”..我该怎么做?
    • hmmmm 这很难,但我认为这行不通,在该函数调用 previewImage(this) 中创建一个新函数,如 sampleFunction()。我只是猜测,但可能会奏效。所以在你的服务器端代码中。您只需添加新功能tt.Attributes.Add("onchange", "sampleFunction()");
    • 好的,你知道如何设置 data-thumbnail='imageId'
    • 尝试添加属性tt.Attributes.Add("data-thumbnail", "imageId");
    • 那么实际上“this”参数的值是多少?是选择的文件还是别的什么?
    【解决方案2】:

    希望对您有所帮助:

    tt.Attributes["onchange"] = "previewImage(this)";
    

    【讨论】:

    • 如何设置 data-thumbnail='imageId' ?
    【解决方案3】:

    我想这可能会对你有所帮助...

    javascript

    <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.min.js"></script> 
    
    <script type="text/javascript">
            function previewImage(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader(); 
    
                reader.onload = function (e) {
                $('#imageId').attr('src', e.target.result);
                };
                reader.readAsDataURL(input.files[0]);
            }
          }
     </script>  
    
    
    <img src='#' id='imageId' alt='your image' height='64' width='64' class='placeholder' >
    <input type='file' id='myID' onchange='previewImage(this)' >
    

    在c#中调用这个函数

    ScriptManager.RegisterStartupScript(this, this.GetType(), "Javascript", "javascript:previewImage(input); ", true);
    

    【讨论】:

    • 与我使用的完全相同的javascript。但我想要的是用C#代码调用这个函数。所以我需要用c#代码手动创建Fileupload控件和图像。
    • 表示你想打电话的时候?有什么具体的控制吗?
    • 要在 c# 中调用这个函数,你必须添加 this..ScriptManager.RegisterStartupScript(this, this.GetType(), "Javascript", "javascript:previewImage(input); ", true);
    • 实际上我使用 C# 代码手动创建了文件上传控制器和图像,如上所示。所以现在我想要的是当我选择图像预览时应该显示在图像框中..这就是我想做的手动编码
    猜你喜欢
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 2021-09-16
    • 2017-01-07
    相关资源
    最近更新 更多