URGENT!!Calling Javascript Function From Code Behind File

Posted by .Netlearner under ASP.NET on 12/18/2012 | Points: 10 | Views : 2486 | Status : [Member] | Replies : 4
Hello All,

I want to pass arguments to the java script function from my code behind file and It returns a value.

How to pass argument to the javascript from codebehind and How to get the return value from java script to a string variable.

Kindly Guide me.

Thanks in Advance




Responses

Posted by: Nerdanalysis on: 12/18/2012 [Member] Starter | Points: 25

Up
0
Down
Asssign the values in hidden field and get those values on page load event, if you need on page load. You can use the Jquery for that. The safest way is to get the values on the like of button click event.

.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 12/18/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
ASPX Design Page

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Testing Page</title>
<script type="text/javascript" language="javascript">
function TestingCodeBehind1() {
document.getElementById('<%= Label1.ClientID %>').innerHTML = '<%= TestingCodeBehind() %>';
return false;
}
function TestingCodeBehind2() {
document.getElementById('<%= Button2.ClientID %>').click();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>
<input id="Button1" type="button" value="Button One" onclick="javascript:TestingCodeBehind1()" />
<br />
<asp:Button ID="Button2" runat="server" Text="" OnClick="ClickedME" /><br />
<input id="Button3" type="button" value="Button two" onclick="javascript:TestingCodeBehind2()" />
<br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</td>
</tr>
</table>
</form>
</body>
</html>

C# Code

protected void Page_Load(object sender, EventArgs e)
{
Button2.Attributes.Add("style", "visibility :hidden");
}

protected string TestingCodeBehind()
{
return "this is a string from C# code behind " + DateTime.Now.ToString();
}
protected void ClickedME(object sender, EventArgs e)
{
Label1.Text = "You clicked this button on " + DateTime.Now.ToString();
}


.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 12/18/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
@Netlearner
Please Mark As Answer if Post helps you.


.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: .Netlearner on: 12/18/2012 [Member] Starter | Points: 25

Up
0
Down
My Java Script Function gets color name as argument and returns the hexadecimal color code for that particular color.

Javascript Function is as follows,

<script type="text/javascript">
function getColorValue(color) {
var t = document.createElement('div');
t.style.display = 'none';
t.style.color = color;
document.body.appendChild(t);

var style = window.getComputedStyle(t, null);
var colorValue = style.getPropertyCSSValue('color').getRGBColorValue();
document.body.removeChild(t);

var hex = function (x) {
return ('0' + parseInt(x, 10).toString(16)).slice(-2);
}

var hexString = '#';
with (colorValue) {
hexString += hex(red.cssText) + hex(green.cssText) + hex(blue.cssText);
}

return hexString;
}
</script>

, how tocall and retrieve the return value from the javascript?

Please Help.

Thanks in Advance

.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response