how to call a javascript function from content page which was declared in master page?
this is my master page
<%
@MasterLanguage="VB"CodeFile="MasterPage.master.vb"Inherits="MasterPage" %><
htmlxmlns="http://www.w3.org/1999/xhtml"><
headrunat="server"><title>calendar</title><
metaname="GENERATOR"content="Microsoft Visual Studio .NET 7.1"><
metaname="CODE_LANGUAGE"content="Visual Basic .NET 7.1"><
metaname="vs_defaultClientScript"content="JavaScript"><
metaname="vs_targetSchema"content="http://schemas.microsoft.com/intellisense/ie5"><
scriptlanguage="JavaScript"src="calendar2.js"></script></
head><
body><formid="Form1"method="post"runat="server"><table><tr><tdwidth=700bgcolor="dimgrey"><div><asp:contentplaceholderid="ContentPlaceHolder1"runat="server"></asp:contentplaceholder></div></td></tr></table></form><scriptlanguage="javascript">var
cal1 =new calendar2(document.forms['Form1'].TextBox1);</
script></
body></
html>********* this is my content page************
<%
@PageLanguage="VB"MasterPageFile="~/MasterPage.master"AutoEventWireup="false"CodeFile="Calender.aspx.vb"Inherits="Calender" %><
asp:ContentID="Content1"ContentPlaceHolderID="ContentPlaceHolder1"Runat="Server"><
table><tr><td><
p><
ahref=cal1.popup();">Click here</a></
p><
asp:TextBoxid="TextBox1"runat="server"></asp:TextBox></td></tr><
tr><td><asp:LinkButtonid="lnkHome"runat="server">Home</asp:LinkButton></
td></tr></table></
asp:content>how to call href=cal1.popup();" function from content page. this is a calender control i need to add in most of the pages in my application
please help me friends.
Hi,
Use Scripting Generator methods.
<
asp:Content ID="Content3" ContentPlaceHolderID="Content" Runat="Server" ><input type="text" name="Message" runat="server">
<
input name="TextBox1" type="button" runat="server" value="ClickMe" onclick="DoClick()"></
asp:Content>
protected
void Page_Load(object sender, EventArgs e){
// Define the name and type of the client scripts on the page.
String csname = "ButtonClickScript";
Type cstype = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
// Check to see if the client script is already registered.
if (!cs.IsClientScriptBlockRegistered(cstype, csname)){
StringBuilder cstext2 = new StringBuilder();
cstext2.Append("<script type='text/javascript'> function DoClick() {");
cstext2.Append("document.forms[0].Message.value='Text from client script.'} </");
cstext2.Append("script>");
cs.RegisterClientScriptBlock(cstype, csname, cstext2.ToString(), false);
}
}
Regards,
Omer kamal
I'm new to asp.net, but what about putting this in the Page_Load event of the master page:
Response.Write("<SCRIPT LANGUAGE='JavaScript' src='" & Server.MapPath("~/Scripts/filename.js") & "'></SCRIPT>")
are there any problems to doing this?
also, did I misunderstand the question?
Hi mors,
you can call the javascript within the page_prerender()...... event of your Masterpage. I had overcome with the same problem adding the Calendar to a content page... So i added that javascript within the page_prerender() event and it works fine for me. Try it.. If you need more help with that just let me know it.
C#_Worker
Thanks c# - do I add it the same way that I wrote or do I do it with other vb/c# functions?
Including JavaScript codes,
eg: An ASP textbox control with the ID="targetDate" and onClick or onClientClick we want to call a JavaScript function(Displaying a calendar within a Div),
OnClick="displayCalendar(document.forms[0].targetDate",'mm/dd/yyyy',this)"
We cannot pass the control name like this. it's giving the JavaScript error.
so there is a way we used to resolve that problem(in C#),
protected void Page_PreRender(object sender, EventArgs e)
{
this.targetDate.Attributes["onclick"] = "displayCalendar(" + this.targetDate.ClientID + ",'mm/dd/yyyy',this)";
}
That is what I did exactly ....Hope you got the idea... If you have more questions let me know it.. I will reply to you tonight(Canada).One last thing you have to add the <script> file within the Masterpage(.aspx page) as normal.
NOTE: Even I added same thing for OnFocus of the text box as well.
Thanks again. Here's what I did:
in the master page Page_Load event I have:
Page.ClientScript.RegisterStartupScript(Me.GetType(), "commonjsfile", "<SCRIPT LANGUAGE='JavaScript' src='" & Request.ApplicationPath & "/Scripts/Js/Javascriptfile.js" & "'></script>")
I did have it just saying response.write("<script src...">) but I guess this is the wrong way to do it.
My questions:
1. like I said above, i put that script thing in the master's page_load event - should I put it in the prerender event?
2. In order to add/set attributes to elements(like onClick), I was putting them in the Page_Load event. Is the better place in the Page_PreRender event or the individual elements Init() event or ? - and why?
3. If I want to set something in the Style attribute, can I just say attributes("style") = "display:none;" or do I need to say attributes("style") &= "display:none:" to add to the style? - note: I just tried a couple of things: if I just say = instead of &=, then the other style attributes aren't applied to the element. However, by using &= to add to the style, when this page is postedback, then thst style of the element has multiple entries ex: "display:none;display:none". So I guess the thing to do is check if "display:none" is in the style attribute before adding it. correct?
I'm new to asp (much less asp.net) - i don't even code in vb or c#, but it's not that bad. I code in foxpro of all things lol (and yes i'm using connections to vfp tables instead of sql server or access)
Here is what I do in my page.
TestForm.aspx
<%
@ Page Language="C#" EnableEventValidation="true" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="TestForm.aspx.cs" Inherits="TestForm" Title="Test Form" %><
script language="javascript"><!--
var
cal1 = new CalendarPopup("date1");// --></
script><form>
<
asp:TextBox ID="txtDate" runat="server" MaxLength="10" Width="80px"></asp:TextBox><a id="A1" href="#" onClick="cal1.select(ctl00_ContentPlaceHolder1_txtDate,'imgCal','M/d/yyyy'); return false;" runat="server"><img src="../Calendar/img/cal.gif" id="imgCal" name="imgCal" runat="server" border="0" /></a></form>
Conclusion: if you are using the masterpage, the name/ID of the textbox or other input field will change on the client side from 'txtDate' to 'ctl00_ContentPlaceHolder1_txtDate'. So to check the name/id just launch the page and click to view the source code.
Hope this help
Thanks...This blog was really useful!!!
Hi GoodGuy,I know as you posted in blog "ctl00_ContentPlaceHolder1_txtDate" works fine... But that's not the right way to do it. So try to find something right... Think about a situation like more than 50 controls... are you going to do the same thing?
you may use document.getElementByID
('<%=yourControl.ClientID%>') to get the real ID of the control when it is renderedHai,
Thank you, you solved my problem, Its Working fine.
Thank you