Programmatically adding Javascript File to User Control in .net

后端 未结 8 1915
不知归路
不知归路 2020-12-24 08:08

How do you add Javascript file programmatically to the user control?

I want the user control to be a complete package - ie I don\'t want to have to add javascript th

8条回答
  •  生来不讨喜
    2020-12-24 08:39

    I generally do it when rendering the HTML for the control and depending on whether it's a library injection or an instance injection I use the Items collection to specify whether or not I have already produced the code for a control of this type during the current request using a unique identifier.

    Something to the effect of:

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
    
            //Check if the Object Script has already been rendered during this request.
            if (!Context.Items.Contains("xxx"))
            {
                //Specify that the Object Script has been rendered during this request.
                Context.Items.Add("xxx", string.Empty);
                //Write the script to the page via the control
                writer.Write([libraryinjection]);
            }
            //Write the script that instantiates a new instance for this control
            writer.Write([instanceinjection]);
        }
    

提交回复
热议问题