Dynamically add client function

3 posts, 1 answers
  1. Angie
    Angie avatar
    113 posts
    Member since:
    Jan 2008

    Posted 11 Dec 2017 Link to this post

    Hello.  I am dynamically adding multiple upload controls - I need to find a way to pass an argument to the OnClientFileUploaded javascript function OR to dynamically create the function itself from the code behind so that I can build the argument directly into the OnClientFileUploaded function.

    Specifically, I need to pass an ID to the function so that when the files are uploaded, I can tell where they should be saved. Please note this is not the controlID but an ID from my database. 

    I've tried adding the function code like this, but the timing does not seem to be correct.    Thank you for any help on this!

    protected void Page_Load(object sender, EventArgs e)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<script> function OnClientFileUploaded(sender) {\n");
                sb.Append("<%= Page.ClientScript.GetPostBackEventReference(new PostBackOptions(this,").Append(tempContentID).Append(")) %>\n");
                sb.Append("}</script>");
     
                this.Page.ClientScript.RegisterClientScriptBlock(typeof(RadEditor), "UploadScript_" + this.ClientID, sb.ToString());
            }
     
      protected void Page_PreRender(object sender, EventArgs e)
            {
                RadAsyncUpload1.OnClientFileUploaded = "OnClientFileUploaded";
            }

     

  2. Answer
    Rumen
    Admin
    Rumen avatar
    13419 posts

    Posted 12 Dec 2017 Link to this post

    Hello,

    My advice is to register the script using the RegisterStartupScript or RegisterClientScriptInclude methods 

    ScriptManager.RegisterStartupScript(Page, (typeof(Page)), "name", sb.ToString(), this.ClientID), true);
    or 

    ScriptManager.RegisterClientScriptInclude(Page, Page.GetType(), "externalFile.js", "Script/externalFile.js");

    Best regards,
    Rumen
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. Angie
    Angie avatar
    113 posts
    Member since:
    Jan 2008

    Posted 13 Dec 2017 in reply to Rumen Link to this post

    Thanks Rumen!  That worked!
Back to Top