Search code examples
c#javascriptjqueryasp.netigoogle

How to force the script to be executed in specific place?


I have the following script :

<script type="text/javascript"src = "//www.gmodules.com/ig/ifr?url=http://hosting.gmodules.com/ig/gadgets/file/100080069921643878012/facebook.xml&amp;up_useNewFB_p=1&amp;up_showPopUp2_p=true&amp;synd=open&amp;w=320&amp;h=500&amp;title=Facebook&amp;border=%23ffffff%7C3px%2C1px+solid+%23999999&amp;output=js"></script>

to use the igoogle gadget .


I execute this script in run time through the following code :

HtmlGenericControl div_general_ac = new HtmlGenericControl("div");
StringBuilder str = new StringBuilder();
str.Append("<script type=\"text/javascript\"");
str.Append("src = '" + dt_list.ElementAtOrDefault(0).Field<string>("process_url") + "'" + "></");
str.Append("script>");
ClientScript.RegisterStartupScript(this.GetType(), "rowTest", str.ToString());
return div_general_ac;

Everything goes okay. The gadget is created but not in div_general_ac. I want to execute the script in the div.


From MSDN:

The main difference is that the RegisterStartupScript method places the JavaScript at the bottom of the ASP.NET page right before the closing element. The RegisterClientScriptBlock method places the JavaScript directly after the opening element in the page.

I want the script in specific place not in the top or the bottom.


Solution

  • You don't need to use ClientScript. If all you are after is putting the script into the <div> element, then just put it there. Example:

        protected void Page_Load(object sender, EventArgs e)
        {
            form1.Controls.Add(CreateGadget());
        }
    
    
        private HtmlGenericControl CreateGadget()
        {
            HtmlGenericControl div_general_ac = new HtmlGenericControl("div");
            StringBuilder str = new StringBuilder();
            str.Append("<script type=\"text/javascript\"");
            str.Append(" src = '" + dt_list.ElementAtOrDefault(0).Field<string>("process_url")+ "'></");
            str.Append("script>");
            div_general_ac.InnerHtml = str.ToString();
            return div_general_ac;
        }