从按钮调用文件上载 - 均在Code Behind中创建

时间:2014-07-03 13:34:31

标签: c# javascript asp.net file-upload

我创建了一个WebControl。在我的构造函数中:

private readonly Button button;
public DocumentUploadButton() {
    button = new Button();
    var panel = new Panel();
    panel.Controls.Add(button);
    var fileUpload = new FileUpload();
    fileUpload.ID = "fileUploadHidden";
    fileUpload.Attributes["style"] = "display:none";

    panel.Controls.Add(fileUpload);
    Controls.Add(panel);
}
在OnLoad中我附加了一个JavaScript函数:

protected override void OnLoad(EventArgs e) {
    this.button.OnClientClick = "InvokeFileUploadScript";

    const string csName = "InvokeFileUploadScript";
    var csType = GetType();
    var script = new StringBuilder();
    script.Append("<script type=\"text/javascript\"> function InvokeFileUpload(sender, args) {");
    script.Append("document.getElementById('fileUploadHidden').click();");
    script.Append("}");
    script.Append("</script>");
    Page.ClientScript.RegisterClientScriptBlock(csType, csName, script.ToString());

    base.OnLoad(e);
}

但是,当我现在点击按钮时,永远不会调用FileUpload。

有人可以帮我解决我做错的事吗? - 谢谢。

1 个答案:

答案 0 :(得分:0)

如果您将this.button.OnClientClick = "InvokeFileUploadScript";替换为this.button.OnClientClick = "InvokeFileUpload";,是否效果不佳?