This is a migrated thread and some comments may be shown as answers.
Dynamically add client function
2 Answers 7 Views
This is a migrated thread and some comments may be shown as answers.
Angie
Top achievements
Rank 1
Angie asked on 11 Dec 2017, 09:34 PM

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 Answers, 1 is accepted

Sort by
0
Accepted
Rumen
Telerik team
answered on 12 Dec 2017, 01:02 PM
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.
0
Angie
Top achievements
Rank 1
answered on 13 Dec 2017, 11:43 PM
Thanks Rumen!  That worked!
Asked by
Angie
Top achievements
Rank 1
Answers by
Rumen
Telerik team
Angie
Top achievements
Rank 1
Share this question
or