Alex
Alex

Reputation: 9720

from page *.aspx.cs call javascript function

I want to call a javascript function from my codebehind. In my button click event handler I have:

protected void load_data_Click(object sender, EventArgs e)
{
    if (dt.Rows.Count == 1)
        {
            BindDl();                       
        }
        else
        {
            //if dt.rows.count! = 1 I want to call a JavaScript function where be one alert! how to do?
        }
}

Upvotes: 3

Views: 6762

Answers (2)

Ben
Ben

Reputation: 764

This page will be helpful for you

// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
Type cstype = this.GetType();
String csName = "MyAlertFunction";

// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(cstype, csName))
{
  String jsFunction = "yourFunctionHere()";
  cs.RegisterStartupScript(cstype, csName, jsFunction, true);
}

Upvotes: 2

DotNetUser
DotNetUser

Reputation: 6612

user scrip manager

    ScriptManager.RegisterStartupScript(this, typeof(string), "SHOW_ALERT",  "alert('')", true);

where in place of alert you can put your javascript code, next argument true puts in script tags automatically so you dont have to write them.

Upvotes: 1

Related Questions