How to invoke Jquery function from code behind?

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

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=""> 

<head runat="server">
<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");
<form id="form1" runat="server">

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

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response