asyncfileupload 完成后 ajaxcontroltoolkit 设置隐藏值

发布于 2024-09-07 17:08:31 字数 908 浏览 3 评论 0原文

我有一个 asyncfileupload 控件,正在使用 ajaxcontroltoolkit 中的控件。在后面的代码中完成的文件上,我处理该文件并将文件中的信息写入数据库。我从数据库中获取记录的 id,需要将其写入 asp 隐藏字段。我尝试过设置值:

fldImageID.Value = pimg.IdImageGroup.ToString();

我尝试过注册一个脚本,就像我在 网站上的示例

  ScriptManager.RegisterClientScriptBlock(
         ImageFileUploader, 
         ImageFileUploader.GetType(), 
         "script1",
       "alert('hi'); top.document.getElementById('" 
       + fldImageID.ClientID 
         + "').value='" 
       + pimg.IdImageGroup.ToString() 
       + "'; top.document.getElementById('" 
     + lblError.ClientID 
        + "').innerHTML = 'image uploaded'",
   true);

我刚刚尝试在响应中嵌入 javascript。从我设置的方法写入调用来处理上传的内容文件。到目前为止我所做的一切都没有效果。完成所有操作后,隐藏字段仍然不包含所需的值。

I have an asyncfileupload control that I'm using from the ajaxcontroltoolkit. On the file complete in the code behind I process the file and write the information in the file to a database. I get the id of the record from the database, and this needs to be written to an asp hidden field. I've tried just setting the value:

fldImageID.Value = pimg.IdImageGroup.ToString();

I've tried Registering a script like I've seen in an example on a website:

  ScriptManager.RegisterClientScriptBlock(
         ImageFileUploader, 
         ImageFileUploader.GetType(), 
         "script1",
       "alert('hi'); top.document.getElementById('" 
       + fldImageID.ClientID 
         + "').value='" 
       + pimg.IdImageGroup.ToString() 
       + "'; top.document.getElementById('" 
     + lblError.ClientID 
        + "').innerHTML = 'image uploaded'",
   true);

I've just tried embedding javascript in a response.Write call from the method I've set to process the uploaded file. Nothing I've done has worked so far. After I've done everything the hidden field still does not contain the required value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灯下孤影 2024-09-14 17:08:38

更好、更简单的解决方案是在代码后面:

string script = String.Format("top.document.getElementById('hdnFilename').value='{0}';", safeFilename);
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "hdnFilenameFromCodeBehind", script, true);

在我的例子中,safeFilename 是唯一的文件名,在处理重复的文件名后,即第五次上传的sample.png 中的sample_5.png。

请参阅http://forums.asp.net/t/1503989.aspx

The better and more simple solution is in code behind:

string script = String.Format("top.document.getElementById('hdnFilename').value='{0}';", safeFilename);
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "hdnFilenameFromCodeBehind", script, true);

In my case, safeFilename is the unique filename, after handling duplicate filename, i.e. sample_5.png in the 5th upload of sample.png.

See http://forums.asp.net/t/1503989.aspx

同尘 2024-09-14 17:08:36

当我从事这个工作时,我找到了一个可以接受的解决方案。从那时起,我收到了有同样问题的人发来的电子邮件,并询问我是否找到了解决方案。所以我在这里展示它,删除任何无关的代码:

从具有 FileUpload 控件的用户控件中,我首先在 FileUploadComplete 处理程序的背面设置会话变量:

*在 ascx 文件 (upload_chart.ascx )我有AsyncFileUpload,重要的是OnUploadComplete和OnClientUploadComplete:*

<ajaxToolkit:AsyncFileUpload 
         OnUploadedComplete="FileUploadComplete1" 
         OnClientUploadComplete="UploadComplete1" 
         ID="ImageFileUploader" 
         runat="server" />

*在ascx文件(upload_chart.ascx.cs)后面的代码中我处理FileUploadComplete:*

public void FileUploadComplete1(object sender, EventArgs e)
{

    try
    {
        if (ImageFileUploader.FileBytes.Length > 0)
            {

                // File data is in ImageFileUploaded.FileBytes
                    // Save it however you need to
                // I saved it to a database, in a DBImage Object class I created
                    // DBImage is specific to my application
                    ODS.Entity.DBImage pimg = 
                        ODS.Data.DataRepository.SaveImageBytes(ImageFileUploaded.FileBytes);
                    // Set the ImageID1 in the session
                Session["ImageID1"] = pimg.IdImageGroup.ToString();
            }
            else
            {
                //  error handling for an empty file, however you want to handle it

            }
    }
    catch (Exception Ex)
    {
        //  error handling for an unhandled exception, whatever you want to do here

    }
}

Javascript 和脚本方法用于设置页面上的值,这是我的脚本方法的代码隐藏:

 // on the aspx page code behind (chartofthedayadmin.aspx.cs) I have the webmethod:

   [System.Web.Services.WebMethod]
    public static string GetImageID1()
    {
        System.Web.SessionState.HttpSessionState Session = System.Web.HttpContext.Current.Session;
        String retval = Session["ImageID1"].ToString();
        Session["ImageID1"] = null;
        return retval;
    }

这里是 javascript:
// 在 aspx 前端 (chartofthedayadmin.aspx) 我有 javascript
// 调用 Web 方法和 javascript 失败消息:

function UploadComplete1() {
         var str = PageMethods.GetImageID1(uploadSuccess1, uploadFailed);
     }
    function uploadFailed() {

        alert('error occurred or some meaningfull error stuff');
   }

*// 用户控件 (upload_chart.ascx) 上的 javascript 设置隐藏字段的值*

 function uploadSuccess1(result) {

    document.getElementById('<%= fldImageID.ClientID %>').value = result;

}

注意:确保您的脚本管理器具有 EnablePageMethods="true"。

I found an acceptable solution back when I was working on this. And since then I've received emails from people who have had the same problem and have been asking if I found a solution. So I'm presenting it here, stripping out any extraineous code:

From the user control that has the FileUpload control I first set the session variable on the back side in the FileUploadComplete handler:

*in the ascx file (upload_chart.ascx) I have the AsyncFileUpload, what is important is the OnUploadComplete and the OnClientUploadComplete:*

<ajaxToolkit:AsyncFileUpload 
         OnUploadedComplete="FileUploadComplete1" 
         OnClientUploadComplete="UploadComplete1" 
         ID="ImageFileUploader" 
         runat="server" />

*in the code behind of the ascx file (upload_chart.ascx.cs) I handle the FileUploadComplete:*

public void FileUploadComplete1(object sender, EventArgs e)
{

    try
    {
        if (ImageFileUploader.FileBytes.Length > 0)
            {

                // File data is in ImageFileUploaded.FileBytes
                    // Save it however you need to
                // I saved it to a database, in a DBImage Object class I created
                    // DBImage is specific to my application
                    ODS.Entity.DBImage pimg = 
                        ODS.Data.DataRepository.SaveImageBytes(ImageFileUploaded.FileBytes);
                    // Set the ImageID1 in the session
                Session["ImageID1"] = pimg.IdImageGroup.ToString();
            }
            else
            {
                //  error handling for an empty file, however you want to handle it

            }
    }
    catch (Exception Ex)
    {
        //  error handling for an unhandled exception, whatever you want to do here

    }
}

Javascript and script methods are used to set the value on the page, here is my codebehind for the script method:

 // on the aspx page code behind (chartofthedayadmin.aspx.cs) I have the webmethod:

   [System.Web.Services.WebMethod]
    public static string GetImageID1()
    {
        System.Web.SessionState.HttpSessionState Session = System.Web.HttpContext.Current.Session;
        String retval = Session["ImageID1"].ToString();
        Session["ImageID1"] = null;
        return retval;
    }

Here is the javascript:
// on the aspx front end (chartofthedayadmin.aspx) I have the javascript
// to call the Web method and the javascript failed message:

function UploadComplete1() {
         var str = PageMethods.GetImageID1(uploadSuccess1, uploadFailed);
     }
    function uploadFailed() {

        alert('error occurred or some meaningfull error stuff');
   }

*// javascript on the user control (upload_chart.ascx) to set the value of the hidden field*

 function uploadSuccess1(result) {

    document.getElementById('<%= fldImageID.ClientID %>').value = result;

}

note: Make sure your scriptmanager has EnablePageMethods="true".

小糖芽 2024-09-14 17:08:35

使用 jQuery 这非常容易。在您的页面中放置一个 html 隐藏输入控件,而不是 asp:hidden 输入控件。添加一个类,例如 "hiddenPhoto" 到您的 html 隐藏控件。

所以可以说我们的控件 html 是这样的

<input type="hidden" class="hiddenPhoto" runat="server" id="hdPhotoName" />

现在使用 OnClientUploadComplete js 方法中的类选择器访问它并设置其值。声明 runat="server" 以便在服务器端访问它的值。

问候

This is pretty easy with jQuery. Have an html hidden input control placed in your page, not the asp:hidden input control. Add a class lets say "hiddenPhoto" to your html hidden control.

so lets say our control html is like this

<input type="hidden" class="hiddenPhoto" runat="server" id="hdPhotoName" />

Now access it using class selector in your OnClientUploadComplete js method and set its value. Have it declared runat="server" in order to access its value on the server side.

Regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文