How to call a javascript from master page? - c#

I'm trying to call a javascript alert from a master page, where i got an update panel. Within that a button and a text box.
I need to call the alert on clicking the button in my master page. So far it seems not working. Please help.
Page.ClientScript.RegisterStartupScript
(this.GetType(), "alert", "invokeMeMaster();", true);
This is what i wrote in my button click. invokerMEMaster include just an alert message.I need to reload the page on the ok button click of the alert. How can i do that as well?

Update Panel clears javascript code when Postback so try to put this code on the header.
<script type="text/javascript">
$(document).ready(
function(){
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function EndRequestHandler(sender, args) {//put your code here}
});
</script>

You need a ScriptManagerProxy, with the ScriptManager residing in your content page

If I may guess your problem (psychic debugging): When the update panel updates the javascript is not rendered of fired on your page.
Try to register your javascript like in the update panel:
ScriptManager.RegisterClientScriptBlock(Page,typeof(string),"JavaScriptCall",script.ToString(), false);

If I understand your question correctly you have a JS function in a content page that needs to be called from the master page?
What I would do is add a hidden input control and an alert function to your master page:
<input runat="server" id="hdnAlert" name="Alert" type="hidden" />
<script type="text/javascript" language="javascript">
function AlertMe(){ alert(document.getElementByID("hdnAlert").value); }
</script>
You could then change the value of the input control from a content page:
HtmlInputHidden hdnTemp = new HtmlInputHidden();
hdnTemp = (HtmlInputHidden)Master.FindControl("hdnAlert");
hdnTemp.Value = "Message To Alert";
Then just have the button in your master page call the "AlertMe" function located in the master page.

Related

Iframe doesn't show onload, but it does after manual reload

I'm using an Asp.net/C# environment and Visual Studio 2013 to create a web application.
I have a page (aspx) with only a single iframe in it, and a javascript method that uses jquery to put html into that frame.
<asp:Content runat="server" ID="MainSectionContent" ContentPlaceHolderID="MainSectionContentPlaceHolder">
<section runat="server" id="FrameSection" clientidmode="Static">
<asp:ContentPlaceHolder runat="server" ID="FrameSectionContentPlaceHolder" />
<iframe id="FrameContentFrame" name="FrameContentFrame" src="about:blank" seamless="seamless" onload=" loadFrameContent(); "></iframe>
</section>
<script type="text/javascript">
//<![CDATA[
function loadFrameContent() {
jQuery('#FrameContentFrame').contents().find('html').html('<%= FrameContent %>'); //FrameContent is a c# property which holds a syntactically correct html string
}
//]]>
</script>
</asp:Content>
In my code behind I fetch a long string from a server and store it in the property FrameContent. This long strings is syntactically correct html.
The result should be:
onload, the javascript method runs. The iframe is populated with html and thus the html string is shown to the user.
but what really happens:
nothing is shown. a Javascript error occurs saying "the function loadFrameContent is undefined". But its clearly there if I view the page source in my web browser. If I right click on the blank page where the frame should be, and choose "reload frame" (Google Chrome), the frame does show the Html.
So: the Html string is correct. the frame is correct. the frame can display the html correctly. However, the frame cannot display the Html onload, it needs an additional manual reload to actually show anything! I checked the page source on my browser and the Javascript function IS present, and the FrameContent property is populated with the correct html string.
As I see it, it looks like a timing issue. A blank iframe will be finished loading before your parent page, so it will call loadFrameContent before the parent page has finished parsing its dom (thus the method won't be registered). Get rid of the iframe onload and add this:
<script type="text/javascript">
//<![CDATA[
function loadFrameContent() {
jQuery('#FrameContentFrame').contents().find('html').html('<%= FrameContent %>'); //FrameContent is a c# property which holds a syntactically correct html string
}
$(loadFrameContent);
// Or if you're using an older version of jQuery
//$(document).ready(loadFrameContent);
//]]>
</script>

Web Page Print Preview

I'm creating an online application form.
In front page, There are First Name, Last Name, Email etc... form inputs. What I want is that
if user fills the form and click on the submit, I want to show him the print preview page with values which user filled... Is it possible? I'm using ASP.Net C#
If you can use jquery this code is good
$(document).ready(function () {
window.print();
});
and you can see these
http://www.designplace.org/tutorials.php?page=1&c_id=27
http://www.alistapart.com/articles/goingtoprint/
https://web.archive.org/web/20211029043752/https://www.4guysfromrolla.com/webtech/061103-1.shtml
If you can use javascript, then try this
<script type="text/javascript">
function CallPrint(strid) {
var prtContent = document.getElementById(strid);
var WinPrint = window.open('', '', 'letf=0,top=0,width=850,height=800,toolbar=0,scrollbars=1,status=0');
WinPrint.document.write('<html><head><title>Popup</title>')
WinPrint.document.write('</head><body>');
WinPrint.document.write('</body></html>');
WinPrint.document.write(prtContent.innerHTML);
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
}
</script>
Take a print button on the page
<input id="btnPrint" type="button" value="Print" runat="server"
onclick="javascript:CallPrint('divPrint')"/>
and Place your Controls which are to be printed within a div
<div id="divPrint">
//Your controls
</div>

How to get the div display property in c#.net before i changed div property in javascript

i Have one div in my default web page . The div style display none, that time in page load event i have to get the status none, but the div status changed in javascript function based on dropdown selection that i dosenot get the none status always show "" status, my code is
aspx page
<div id="Div1" runat="server" clientidmode="Static" style="display: none;">
pageload event
string Display1 = "";
Display1 = Div1.Style["display"]; (working fine)( get the display1 value=none)
when i button click show the div like that in c# code
Div1.Style.Add("display", "");(now show the div it is correct)
Dropdown Changed fire javascript function code
document.getElementById('Div1').style.display = 'none'; (after change dropdown Hide the div after pageload i got Display1 value="")
After changed javascript function i doenot get the none status in page load
you can use like this it's below
<script type="text/javascript">
$(function () {
$("#lnkCompany").click(function () {
if ($("#dvClientReg").css('display') == 'block') {
$("#dvClientReg").css('display', 'none');
}
if ($("#dvCompanyReg").css('display') == 'none') {
$("#dvCompanyReg").css('display', 'block');
}
});
});
</script>
i think this will help you
DIV is a HTMLGenericalControl in ASP.Net and any changes you make it in the client to its style or attributes, i dont think it will be posted back to server, instead you can track the changes on a Hidden control which has runat server attribute to true and capture the changes.
let me know if it helps

Send HTML Parameter to button click event in ASCX child control

I'm trying to access some page HTML to use for an email in a Button_Click event.
I cannot set this content easily anywhere else at runtime (Such as in a TAG).
So I'm wondering if I can use JQuery to set a variable to .innerHtml(), and pass that in the button click. How would I go about doing this?
Something like this...
<div id="myDiv">
some content...
</div>
<asp:HiddenField ID="hdnHtml" ClientIDMode="Static" runat="server" />
<script type="text/javascript">
$(function () {
$('#hdnHtml').val($('#myDiv').html());
});
</script>
To add a hidden field to a form: fill its value using javascript/jQuery and submit the form via a Button_Click event.

<script> tag get disappeared from rendered HTML after ajax postback action

I have a webpage with one <asp:updatePanel>. In the end of the webpage, some javascript method is added and called such as :-
The updatePanel code used is as follows:-
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
Some controls like repeater etc..
</ContentTemplate>
</asp:UpdatePanel>
<script type="text/javascript">
alert('hi');
MethodCall();
</script>
After some ajax postback action happened on the page, when i see the webpage html using the "View Source". The script tag code area gets disappeared from the webpage html. And this causes the problem at the place where js method is called.
I don't understand why this is happening? Why it is removing the JS code block after the ajax postback action.
Pls suggest some workaround or any solution to my problem.
Thx
You can also add the following js function, that is called by the ASP.Net AJAX client script library.
function pageLoad(sender, args)
{
if (args.get_isPartialLoad())
{
get_isPartialLoad is true when a partial postback has occurred.
}
}

Categories