How to invoke Jquery function from code behind?

 Posted by Niladri.Biswas on 7/19/2012 | Category: jQuery Interview questions | Views: 2965 | Points: 40
Answer:

Suppose we want to change the background color of a div tag from the code behind upon button click.In code behind write the below

protected void btnChangecolor_Click(object sender, EventArgs e) 

{
string script = "<script type=\"text/JavaScript\" language=\"javascript\">ChangeDivBackgroundcolor();</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "ChangeColor", script);
}


The aspx file will be as under

<html xmlns="http://www.w3.org/1999/xhtml"> 

<head runat="server">
<title></title>
<script type="text/javascript" src="Scripts/jquery-1.7.2.js"></script>
<script type="text/javascript">
function ChangeDivBackgroundcolor()
{
var divObject = $('#divTest');
divObject.css("background-color", "cyan");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:Button ID="btnChangecolor" runat="server" Text="Say Hello" onclick="btnChangecolor_Click" />
</div>
<div id="divTest">Change the color of this div</div>
</form>
</body>
</html>


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response