Use the RegisterClientScriptBock method. Example:
--codebehind for somepage.cs, which uses a masterpage (although this doesn't
matter)
Page.aspx:
<input type="button" onclick="something();" value="click me" />
Page.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
Page.RegisterClientScriptBlock("uniqueString", "<script
type=\"text/javascript\">function something(){
alert('something'); }</script>");
}
Doing the Javascript construction in the .cs file can get annoying and
messy, so I will often put the javascript in the aspx (or ascx) file in a
literal control that I never display, but read the contents of, like so:
Page2.aspx:
<input type="button" onclick="somefunction();" value="clickme" />
<asp:Literal ID="litClientScript" runat="server" Visible="false">
<script type="text/javascript">
function somefunction() {
var x = 3;
var y = 9;
alert(x*y);
}
</script>
</asp:Literal>
Page2.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
Page.RegisterClientScriptBlock("clientScript", litClientScript.Text);
}
Ray at work
<bb****@yahoo.comwrote in message
news:11**********************@n76g2000hsh.googlegr oups.com...
Hi,
I have a master page and content page. In content page I have to
validate the text boxes for
name,address, phone number and email address etc. But as content pages
doersn't have html section not head or body section so, where I use
this java script in content page to validate the form.
Thanks