1

I have a jQuery function to show a validation message in an asp.net login page. I put the script in a separate file named login.js (I've called the js file from the html) The name of the function is loginMessage();

I need to know how to call it from aspx.cs in a button click event. I've tried to call the function directly in a button click event, eg :

protected void btnLogin_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{ 
  ...........
  The login validation process goes here
  ...........

  if (ds.Tables.Count == 0)
  { 
       loginMessage();
  } 
}

but I got this following error message when i tried to debug the page :

The name 'loginMessage' does not exist in the class or namespace 'School.Login'

Kindly help me about this query. Regards, Andha

Andha
  • 907
  • 2
  • 11
  • 22
  • possible duplicate of [Proper way to include scripts in an asp.net page](http://stackoverflow.com/questions/3198123/proper-way-to-include-scripts-in-an-asp-net-page) – Sani Huttunen Jul 28 '11 at 02:41
  • sorry for that, but i've edited the example to detail my problem which is quite different from the thread you are referring. – Andha Jul 28 '11 at 02:50

4 Answers4

2

This should do the trick:

btnLogin.Attributes.Add("onclick", "return loginMessage();");

Other examples here: http://www.devcurry.com/2009/01/execute-javascript-function-from-aspnet.html

Ben
  • 1,049
  • 1
  • 7
  • 7
1

You can't call a JS function within the code behind of an aspx page.

Instead use OnClientClick attribute of the btnLogin in the aspx/ascx page to trigger a JS function.

something like ..

<asp:Button id="btnLogin" runat="server" OnClientClick="return loginMessage()" .../>

EDIT: Updating the answer based on OP's edit to the question.

To call the js method on page load try this:

protected void btnLogin_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{ 
  ...........
  The login validation process goes here
  ...........

  if (ds.Tables.Count == 0)
  { 
    if (!IsClientScriptBlockRegistered("loginMessage"))
    {
        String loginMessage= "<script type=\"text/javascript\">loginMessage();</script>";
        RegisterStartupScript("loginMessage", loginMessage);
    }
  } 
}
Chandu
  • 81,493
  • 19
  • 133
  • 134
  • sorry i didn't explain it clearly before. but i've edited my post to detail the problem. the process needs to do some login validation process before the js is called. could it be achieved by using the code you posted above ? thx so much. – Andha Jul 28 '11 at 02:53
0

The first result after a seach on Google with keywords "asp.net button client" points to this site http://msdn.microsoft.com/en-us/library/7ytf5t7k.aspx#Y570 It will be helpful for you.

hirikarate
  • 3,055
  • 4
  • 21
  • 27
0

You can use ScriptManager and ClientScriptManager for that.

An example:

protected void btnLogin_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{ 
  ...........
  The login validation process goes here
  ...........

  if (ds.Tables.Count == 0)
  { 
       Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ValdationMessage", "loginMessage();", true);
  } 
}
Sani Huttunen
  • 23,620
  • 6
  • 72
  • 79