从按钮调用FileUpload -都在CodeBehind中创建

本文关键字:CodeBehind 创建 都在 按钮 调用 FileUpload | 更新日期: 2023-09-27 18:11:14

我已经创建了一个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-Function:
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永远不会被调用。

有人能告诉我我做错了什么吗?——谢谢。

从按钮调用FileUpload -都在CodeBehind中创建

如果你用this.button.OnClientClick = "InvokeFileUpload";代替this.button.OnClientClick = "InvokeFileUploadScript";,它不是工作得更好吗?