0

I want to develop a simple web part using VS 2012 and add this code for button click:

SPListItem Item = SPContext.Current.Web.Lists["Tasks"].Items.Add();
Item["Title"] = TextBox1.Text;
Item["Due Date"] = Calendar1.SelectedDate;
Item.Update();
Label1.Text = "Item Added";
TextBox1.Text = "";

ScriptManager.RegisterStartupScript(this, GetType(), 'nima', "alert('this is nima');",true);

but the problem is this alert does not show and this code does not render in my page.

How I can inject javascript to a page in Sharepoint 2010?

thanks

  • Where did you put the code above? In which event? – MdMazzotti Jan 14 '14 at 18:14
  • in button click – Arian Jan 14 '14 at 19:14
2

try once using ClientScript.RegisterStartupScript as below :

Page.ClientScript.RegisterStartupScript(this.GetType(), "nima", "alert('this is nima');", true);
  • I test it but does not work – Arian Jan 14 '14 at 16:45
  • could u verify if the script is getting on the page via view source. – Pushpendra Jan 14 '14 at 17:16
  • No the code isn't on my source – Arian Jan 14 '14 at 17:26
  • Your problem seems weird. Then I would suggest u to go for a new webpart to test just RegisterStartupScript method and add it to a new page in SP. It might be a case of conflict somewhere. I have used these methods and it works fine for me. – Pushpendra Jan 14 '14 at 17:56
0

I would lean towards throwing the JS into a literal object on the page, as I have found this is 1) easier and 2) give you a wee bit more control on what is really going on. So instead of using the ScriptManager, throw your script

<script type="text/javascript">
$(document).ready(function() {
   alert("this is nima");
});
</script>

in a ASP:Literal control.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.