hemtros
10/26/2016 - 9:59 PM

Inject clientside script in server side in asp.net webforms

Inject clientside script in server side in asp.net webforms

Page page = HttpContext.Current.CurrentHandler as Page;

String jscript = "window.open('https://google.com', '_blank')";

ClientScriptManager csm = page.ClientScript;
 
csm.RegisterStartupScript(page.GetType(), "salesforcescript", jscript,true);
Page page = HttpContext.Current.CurrentHandler as Page;

String jscript = "window.open('https://google.com', '_blank')";

Control control = page.FindControl("clientid of control");

ScriptManager.RegisterClientScriptBlock(control, control.GetType(), "whatever", jscript, true);
//we can also use ScriptManager.RegisterStartupScript()