I have a function on masterpage and i want to call it from content page from codebehind.
this is my trying :
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert__", string.Format("setStatusBarMessage('{0}',{1});", barMessage, type, ""), true);
"setStatusBarMessage" function is declare in masterpage , so this code doesnt working.
setStatusBarMessage is a client side function.
MasterPage:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Content.master.cs"
Inherits="F8.CRM.Pages.Content" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" />
<div>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
<script type="text/javascript">
function hello() {
alert('hi mennan');
}
</script>
ContentPage :
<%# Page Title="" Language="C#" MasterPageFile="~/Pages/Content.Master" AutoEventWireup="true"
CodeBehind="Departman.aspx.cs" Inherits="F8.CRM.Departman" %>
<%# Register Src="~/Controls/Objects/StudioSideBox/StudioSideBox.ascx" TagName="StudioSideBox"
TagPrefix="uc1" %>
<%# Register Src="~/Controls/Objects/Baslik/Baslik.ascx" TagName="Baslik" TagPrefix="uc2" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
my html...
<script type="text/javascript">
my script codes...
</script>
</asp:Content>
This masterpage and content page is under a iframe object.
ok try the following code
I have a function in Master Page that is
<script>
function hello() {
alert('hi');
}
</script>
Now on content page' page load
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "ntmtch", "hello();", true);
}
It working. i haven't added any thing to content page.
Update
Master page's Code
<%# Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script>
function hello() {
alert('hi');
}
</script>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
First Content Page's code
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<iframe src="Default2.aspx"></iframe>
</asp:Content>
Code behind Of first Content Page:
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "ntmtch", "hello();", true);
}
Try this for calling master page server side function
MasterPagename ms = Master as MasterPagename ;
ms.FuctionOnMasterPage();
If u r trying to call client side function on master page, the i guess u can call it directly since ur master page and content page function will get rendered on the same html page.
Here We can Make a Object of Master Page Class and then we can Call MasterPage Function
MasterPageClassName MyMasterPage = (MasterPageClassName)Page.Master;
MyMasterPage.Functionname();
It will definitely Help you. Try It
Usually I do it so: In the markup:
<asp:Literal ID="ScriptLit" runat="server" />
In the code behind:
ScriptLit.Text = "<script>functionName();</script>"
Label lbl = (Label)this.Master.FindControl("lblBalance");
lbl.Text = "Hi";
Related
I have a class named Alerts which i inherit from BootstrapPage. I am using this class as a MessageBox to the users after doing some operation. Here is the code
public class BootstrapPage : Page
{
public enum WarningType
{
Success,
Info,
Warning,
Danger
}
public void ShowNotification(string message, WarningType type)
{
var mainContentHolder = Master.FindControl("MainContentPlaceHolder") as ContentPlaceHolder;
Panel notificationPanel = new Panel();
{
LiteralControl closeButton = new LiteralControl();
closeButton.Text = "×";
Label notificationMessage = new Label() { Text = message };
notificationPanel.Controls.Add(closeButton);
notificationPanel.Controls.Add(notificationMessage);
}
notificationPanel.CssClass = string.Format("alert alert-{0} alert-dismissable", type.ToString().ToLower());
notificationPanel.Attributes.Add("role", "alert");
mainContentHolder.Controls.AddAt(0, notificationPanel);
}
}
I have a MasterPage named Site.Master which is here (simplified code for better understanding)
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="DeceasedSystem.Site" %>
and here is the HTML
<!DOCTYPE html>
<html>
<head runat="server">
<link href="css/bootstrap.min.css" rel="stylesheet" />
<link href="css/Style.css" rel="stylesheet" />
<link href="css/select2.css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div class="col-md-9">
<asp:ContentPlaceHolder ID="MainContentPlaceHolder" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
and here is the contentPage
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="UserProfile.aspx.cs" Inherits="DeceasedSystem.WebForm1" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
</asp:Content>
Now when i logged in, it throws NullReferenceException on this line
var mainContentHolder = Master.FindControl("MainContentPlaceHolder") as ContentPlaceHolder;
Here is the picture.
I have no clue why it throws NullReferenceException, when i have provided the MainContentPlacholder and it is present in my MasterPage. Please help me out because i don't understand the Exception and also what is the reason that it throws this Exception.
Note: I am Inheriting web pages from BootstrapPage. Does this have anything to do with the Exception? Help me folks. PLEASE
The 'this' I'm talking about is the first argument of createDiv function call below. So it's basically the LinkButton that is clicked.
<asp:LinkButton ID="btn_ReportReply1" runat="server" class="btn btn-danger btn-xs" Text="ReportReply" OnClientClick='<%# String.Format("createDiv(this," + Eval("UniqueNo") +","+ Eval("CommentSeqNo") + "," + Eval("UserNo") + ", \"{0}\");", Eval("LoginID")) %>' ></asp:LinkButton>
Now what I want to do with 'this' button is that I want to call Bootstrap .popover() to it. So below I'm trying to get the btn's ClientID and do .popover() but red underline shows up under btn.ClientID. And I cannot use the button's class because there are several buttons that have the same class.
function createDiv(btn, ReplyNo, CommentSeqNo,ReportedUserNo, LoginID) {
$('#' + '<%= btn.ClientID %>').popover({
trigger: 'manual',
placement: 'left',
content: vType + vContent
});
$('#' + '<%= btn.ClientID %>').popover("show");
}
OnClientClick is rendered as onclick on the DOM object. The this in that scope is the DOM object itself, so you can use btn.id to get the ClientID or simply use $(btn) in your function.
Here's a sample:
Markup
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="Scripts/jquery-1.7.1.js"></script>
<script type="text/javascript">
function createDiv(btn) {
$(btn).text($(btn).attr('id'));
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater runat="server" ID="List">
<ItemTemplate>
<div>
<asp:LinkButton ID="thisIsMyId" Text="<%# Container.DataItem %>" runat="server" OnClientClick="createDiv(this); return false;" />
</div>
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
Code behind
using System;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List.DataSource = new string[] { "TEST1", "TEST2" };
this.DataBind();
}
}
}
I'm having trouble getting the window.opener value to transfer between child popup windows. This code works in IE 8 and order, but quit working in IE 9 and newer. Any input is appreciated.
Explained a different way. My sister knows that I'm her brother but doesn't trust me to tell her who our parents are.
Process Flow:
Parent window (Main form) -> Opens popup window (PopUp 1)
User clicks button on PopUp 1 to open PopUp 2.
PopUp 2 opens:
It pulls data from PopUp 1 then closes PopUp 1
User clicks button(Save) on PopUp 2.
PopUp 2 refreshes the data on the original parent "Main form". Then closes itself.
The last step is the step that is not happing IE 9. I created the sample below to show the functionality works using pure JavaScript but fails when posting back and using RegisterStartupScript to call the JavaScript function. We are using the RegisterStartupScript because the save action of the form has many different outcomes based on the user's input.
All pages are in the same domain.
MainForm.aspx
<%# Page Language="VB" AutoEventWireup="false" CodeFile="MainForm.aspx.vb" Inherits="MainForm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function OpenPopUp() {
document.getElementById('lblStatus').innerHTML = '';
window.open('PopUp1.aspx', '',
'width=750,left=' + ((screen.width / 2) - 375) + ',top=' + ((screen.height / 2) - 250) +
',height=500,location=no,menubar=no,status=yes,scrollbars=yes,toolbar=no,resizable=yes');
}
function Refresh() {
document.getElementById('lblStatus').innerHTML = 'It Worked';
}
</script>
</head>
<body>
<form id="form1" runat="server">
<input type="button" id="btnOpen" value="Open Popup" style="width: 110px" onclick="OpenPopUp()" />
<br /><br />
<asp:Label ID="lblStatus" runat="server"></asp:Label>
</form>
</body>
</html>
PopUp1.aspx
<%# Page Language="VB" AutoEventWireup="false" CodeFile="PopUp1.aspx.vb" Inherits="PopUp1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript">
function OpenPopUp2() {
window.open('PopUp2.aspx', '', 'width=700,left=' + ((screen.width / 2) - 350) + ',top=' +
((screen.height / 2) - 275) + ',height=550,location=no,status=yes,toolbar=no,resizable=no,scrollbars=yes');
}
</script>
</head>
<body>
<h1>This is pop up 1</h1>
<form id="form1" runat="server">
<asp:TextBox ID="txtPop1" runat="server" Text="test 1" ></asp:TextBox>
<input type="button" id="btnOpen" value="Open Popup 2" style="width: 110px" onclick="OpenPopUp2()" />
</form>
</body>
</html>
PopUp2.aspx
<%# Page Language="VB" AutoEventWireup="false" CodeFile="PopUp2.aspx.vb" Inherits="PopUp2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function GrabData() {
var oParentDoc = window.opener.document;
if (oParentDoc != null) {
try { document.getElementById('txtPop2').value = oParentDoc.getElementById('txtPop1').value; } catch (ex1) { }
}
var oParentOpener = window.opener.opener;
window.opener.close();
window.opener = oParentOpener;
}
function MyClose() {
window.opener.Refresh();
window.close();
}
</script>
</head>
<body>
<h1>This is pop up 2</h1>
<form id="form1" runat="server">
<asp:TextBox ID="txtPop2" runat="server" ></asp:TextBox>
<input type="button" id="btnSave1" value="Save Working" style="width: 110px" onclick="MyClose()" />
<asp:Button ID="btnSave" runat="server" Text="Save Not Working" />
</form>
</body>
</html>
PopUp2.aspx.vb
Partial Class PopUp2
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack() Then
ClientScript.RegisterStartupScript(Type.GetType("System.String"), "Refresh", _
"<script type=""text/javascript"">GrabData();</script>")
End If
End Sub
Protected Sub btnSave_Click(sender As Object, e As System.EventArgs) Handles btnSave.Click
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "RestoreFilter", _
"<script type=""text/javascript"">MyClose();</script>", False)
End Sub
End Class
Shouldn't this:
Protected Sub btnSave_Click(sender As Object, e As System.EventArgs) Handles btnSave.Click
ScriptManager.RegisterStartupScript(Me, Me.form1.GetType(), "RestoreFilter", _
"<script type=""text/javascript"">MyClose();</script>", False)
End Sub
Be (see the GetType part):
Protected Sub btnSave_Click(sender As Object, e As System.EventArgs) Handles btnSave.Click
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "RestoreFilter", _
"<script type=""text/javascript"">MyClose();</script>", False)
End Sub
I am using CKEditor in my asp.net page. For this i have added ckeditor.dll to my project.
And i am using this reference in .aspx page.
My problem is that i can not able to read ckeditor content in javascipt.
Can any one please tell me how to achive this.
Here is my code
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CKEditorVamshi._Default" %>
<%# Register Assembly="CKEditor.NET" Namespace="CKEditor.NET" TagPrefix="CKEditor" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
function validate() {
var text = $('#cke_<%= CKEditorGettingStarted.ClientID %> iframe').contents().find('body').html();
alert(text);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<CKEditor:CKEditorControl AutoPostBack="True" ID="CKEditorGettingStarted" runat="server"
Height="300px" BasePath="~/CKEditor" MaxLength="10" Width="100%" CausesValidation="true">
</CKEditor:CKEditorControl>
<br />
<input type="button" id="btnPreview" value="Preview" onclick="validate();" />
</form>
</body>
</html>
Thank you in Advance
try
CKEDITOR.instances.Your_Editor_Client_ID.getData();
I'm trying to retrieve a server control in JavaScript. For testing purposes I am calling the JavaScript function from the page load event.
protected void Page_Load(object sender, EventArgs e){
ClientScript.RegisterClientScriptBlock(GetType(), "js", "confirmCallBack();", true);
}
And my JavaScript function is
function confirmCallBack() {
var a = document.getElementById('<%= Page.Master.FindControl("PlaceHolderContent").FindControl("Button1").ClientID %>');
var b = document.getElementById('<%=Button1.ClientID%>');
}
my problem is that both a and b return null. Even when I view the page source the correct ClientID is returned.
I should add that I'm using master page.
Any ideas.
Gotcha!
You have to use RegisterStartupScript instead of RegisterClientScriptBlock
Here My Example.
MasterPage:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs"
Inherits="prueba.MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function confirmCallBack() {
var a = document.getElementById('<%= Page.Master.FindControl("ContentPlaceHolder1").FindControl("Button1").ClientID %>');
alert(a.value);
}
</script>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
WebForm1.aspx
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true"
CodeBehind="WebForm1.aspx.cs" Inherits="prueba.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" />
</asp:Content>
WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace prueba
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(this.GetType(), "js", "confirmCallBack();", true);
}
}
}
Is Button1 visible? I mean, from the server side. Make sure Button1.Visible is true.
Controls that aren't Visible won't be rendered in HTML, so although they are assigned a ClientID, they don't actually exist on the client side.