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