1

On my aspx page I have two buttons. One calls some javascript and the other calls a C# function on the code-page. When I call the javascript file in the head of the document, the JavaScript function works well but the C# does not. Clicking on the button does not do anything. If I remove the javascript call then the C# function works normally.

How can I overcome this? It seems as if it is expecting to find the C# function within the JavaScript file.

ASP:

<head>
<script src="MyFunctions.js" language="javascript" type="text/javascript"></script>
</head>
<body>
<asp:Button id="btn1" Text="Submit" runat="server" OnClick="buttonSumbit_Click" />
<asp:Button id="btn2" Text="Show" runat="server" OnClientClick="buttonShow_Click()" />
</body>

C#:

protected void buttonSumbit_Click(object sender, EventArgs e)
{
//SOME CODE HERE
}

JavaScript:

function buttonShow_Click()
{
//SOME CODE HERE
}
5
  • If one calls javascript and other calls c# do you click both the buttons at the same time !? Commented Feb 20, 2013 at 11:18
  • 1
    The language attribute of script elements has been deprecated long ago. Commented Feb 20, 2013 at 11:24
  • are you missing some code here?? or this is the total code present in the .aspx page? Commented Feb 20, 2013 at 11:27
  • I don't click both buttons at the same time. The javascript function sets a div's display attribute from 'hidden' to 'block', and the C# function writes data to database. Commented Feb 20, 2013 at 12:08
  • Yes there is some code missing, such as <form runat="server"> Commented Feb 20, 2013 at 12:08

3 Answers 3

2

You need to put all Asp.Net server controls inside a <form runat="server"></form> tag

Sign up to request clarification or add additional context in comments.

1 Comment

all controls are within the <form> tags. The C# functions work perfectly when I don't have the JavaScript.js file linked to the page.
1

You need to modify your javascript function call to be like this

<asp:Button id="btn2" Text="Show" runat="server" OnClientClick="return buttonShow_Click()" />

and make sure that your javascript function returns true so postback (execute C# function) happens. If your javascript function return false, postback to server won't happen.

2 Comments

My C# functions are still not available. I've tried removing the button with the javascript on-click event. The other buttons still do not work. They only work when I remove the line: <script type="text/javascript" src="Javascript.js"></script>
If so can you please share your Javascript.js file?
0

I Suggest you to use this for calling javascript functions:

OnClientClick="if(!hidepopup())return false;"

you can also call javascript functions from codebehind:

Page.ClientScript.RegisterStartupScript(this.GetType(), "Call my function", "jsfunction();", true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.