call javascript method from codebehind - javascript

how to call this javascript function from asp.net codebehind pageload..
<script type="text/javascript">
function abc() {
alert("Hello! I am an alert box!");
}
</script>
Is it possible to pass an integer array in to javascript function from asp.net codebehind pageload?

Try below code :
protected void Page_Load(object sender, EventArgs e)
{
System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "abc", "abc();", true);
}
1. Update > Passing string parameter :
protected void Page_Load(object sender, EventArgs e)
{
var message = "hi";
System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "abc", "abc('" + message + "');", true);
}
JavaScript Method with string parameter :
function abc(message) {
alert(message + ", I am an alert box!");
}
2. Update > Passing string parameter and numeric array to JS method:
protected void Page_Load(object sender, EventArgs e)
{
int[] numbers = { 10, 20, 30 };
string serializedNumbers = (new JavaScriptSerializer()).Serialize(numbers);
var message = "hi";
System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "abc", "abc('" + message + "', " + serializedNumbers + ");", true);
}
JavaScript Method with string and numeric array parameters:
function abc(message, numbers) {
alert(message + ", I am an alert box!");
for (var i = 0; i < numbers.length; i++) {
alert(numbers[i]);
}
}

Regular Page
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(GetType(), "abc" + UniqueID, "abc();", true);
}
Ajax Page
You need to use ScriptManager if you use ajax.
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(),
"abc" + UniqueID, "abc();", true);
}

Try
Dim script As String = String.Format("abc()", "")
ScriptManager.RegisterClientScriptBlock(Me, GetType(Page), UniqueID, script, True)
Or simply
ClientScript.RegisterStartupScript(GetType(), "abc", "alert('Hello! I am an alert box!')", true);

You can't call a JavaScript function from the codebehind, but you can return a response that includes JavaScript that invokes the function when the page loads in the browser. Just make sure that your page includes
<script type="text/javascript">
function abc() {
alert("Hello! I am an alert box!");
}
abc();
</script>
That could be either as part of the ASPX page or you could register it as a script block in the codebehind.

Related

Execute command only after executing the javascript function

I have a button inside a repeater, and this is the event:
protected void Delete_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
string id = btn.CommandArgument;
string client = "011";
try
{ //When is done
ScriptManager.RegisterStartupScript(this, GetType(), "del", "Del(" + id + ");", true);
//do it
this.Repeater.DataSource = dal.GetT(client);
this.Repeater.DataBind();
}
catch { }
}
I need to update the repeater, only after executing the javascript function Del(id)
How can this be done in the best way?

Add JavaScript function to a dynamically created control

I tried all possible ways to add javascript code to my dynamically created control a date textbox but it is not working. I need to add multiple scripts like searchable dropdownlist and datepicker for date textbox. I am creating control on Page_Init and then attaching javascript on Page_Load but it is not working. Can somebody help, please? Thanks.
protected void Page_Init(object sender, EventArgs e)
{
CreateControls();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (fieldtype == "date")
{
string javaScriptString = #"function attachJSScript() {";
javaScriptString += #"$('#<%= " + str + ".ClientID %>').datepicker({ dateFormat: 'dd/mm/yy' }).val();";
javaScriptString += #"}";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "attachJSScript", javaScriptString, true);
}
}
}
}
When I view the page source, it shows
<script type="text/javascript">
//<![CDATA[
function attachJSScript() {$('#<%= txt_1_date_yes.ClientID %>').datepicker({ dateFormat: 'dd/mm/yy' }).val();}//]]>
</script>
I added this code for dropdownlist dynamically created control but it is not working either. Same code I used for the controls in aspx pages and that works.

When I use window.location.href ,then my another function not calling .Following is my javascript code

I am using Following code..
When I click on the link, the javascript Hello() function is invoked
I want to use window.location.href
But when I use this the following __doPostBack('Button2_Click'), it does not work.
But when remove window.location.href from the following code then __doPostBack('Button2_Click') does work.
<script type="text/javascript">
function Hello(clicked_id) {
var abc = "http://localhost:2621/OrgChart.aspx?id" + clicked_id;
window.location.href = abc;
__doPostBack('Button2_Click');
return false;
}
</script>
<a id="A1" href="javascript:Hello();">LINK</a>
This is my code behind code...
public partial class WebForm17 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.GetPostBackEventReference(this, string.Empty);//This is important to make the "__doPostBack()" method, works properly
if (Request.Form["__EVENTTARGET"] == "Button2_Click")
{
//call the method
Button2_Click(this, new EventArgs());
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Label1.Text = "Method called!!!";
EmpInfo emp = new EmpInfo();
DA_EmpInfo da_emp = new DA_EmpInfo();
List<EmpInfo> lei = da_emp.GetAllEmployeeInfoByEmpId("MJ-IB-1");
DetailsView1.DataSource = lei;
DetailsView1.DataBind();
}
}
I guess, __doPostBack is making a request to the server and you break it by using window.location.href = abc;.
You should use some callback from this request to redirect to your url.
try to use setTimeOut function
setTimeout(function () {
window.location.href = abc;
}, 1000);
this will wait 1 second for finish of __doPostBack() function.
Or if you don't want to use timeOut, paste window.location.href = abc; line to end of the __doPostBack() function.

Whats wrong in this asp.net code? Textbox value doesnt set

protected void Button1_Click(object sender, EventArgs e)
{
if (count > 100)
{
StringBuilder javascript = new StringBuilder();
javascript.Append(" <script language=\"javascript\" type=\"text/javascript\">");
javascript.Append(" var tmp = confirm(\"No:Of Records exceeds 1000.Please confirm you want to continue\");");
javascript.Append("if (tmp)");
javascript.Append("{document.getElementById(\" <%=TextBox1.ClientID%>\").value=\"1\"; alert(document.getElementById(\"<%=TextBox1.ClientID %>\").value);}");
javascript.Append(" </script>");
ClientScript.RegisterStartupScript(GetType(), "recordscript", javascript.ToString(), false);
return;
}
}
Here I want to set the value of the textbox by clicking the button event and oly that condition is true.So I cant call that function from source.actually that function gets called but the textbox value doesnt set..I really dont understand where is the problem..
protected void Button1_Click(object sender, EventArgs e)
{
if(count>100)
{
StringBuilder javascript = new StringBuilder();
javascript.Append(" <script language=\"javascript\" type=\"text/javascript\">");
javascript.Append(" var tmp = confirm(\"No:Of Records exceeds 1000.Please confirm you want to continue\");");
javascript.Append("if (tmp)");
javascript.Append("{document.getElementById('" + TextBox1.ClientID + "').value=\"1\"; alert(document.getElementById('" + TextBox1.ClientID+ "').value);}");
javascript.Append(" </script>");
ClientScript.RegisterStartupScript(GetType(), "recordscript", javascript.ToString(), false);
return;
}
}
}
You need to concatenate the TextBox1.ClientID with your javascript string. The code you have will get rendered to the page as is, look at the output of your rendered page with view source, you will see the string '<%= TextBox1.ClientID =%>' not the expected ID. Keep in mind that the inline display expression <%= =%> is equivalent to a server Response.Write().

how can I to get controls back on my first page after server.execute("../../abc.aspx"). I have to show an alert after server.execute()

This is my Code, After Server.Execute() my popup does not show.
protected void Generate_cola_letter(object sender, EventArgs e)
{
try
{
Server.Execute("../ABC.aspx");
}
catch (Exception exp)
{
}
finally
{
ScriptManager.RegisterStartupScript();
}
Are you trying to create a Message box?
this is an example of a message box in asp.net
string script = "alert(\"Your Message.\");";
ScriptManager.RegisterStartupScript(this, GetType(),
"ServerControlScript", script, true);

Categories

Resources