I'm trying to set the value of an ASP TextBox via javascript when I click a button but I can't get the ClientID of my TextBox control using code blocks.
I get this error :
The Controls collection cannot be modified because the control
contains code blocks (i.e. <% ... %>)
Here's my .aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="App.aspx.cs" Inherits="StatsWeb.App" %>
<!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>
<link href="Styles/Site.css" rel="Stylesheet" type="text/css" />
<script>
function testf() {
alert("here");
document.getElementById('<%= TextBox1.ClientID %>').value = "asd";
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Menu ID="Menu1" runat="server" DataSourceID="XmlDataSource1"
CssClass="Menu" StaticDisplayLevels="2" MaximumDynamicDisplayLevels="5" >
<DataBindings>
<asp:MenuItemBinding DataMember="rule" TextField="name" ValueField="value"/>
<asp:MenuItemBinding DataMember="splitByField" TextField="name" ValueField="value" NavigateUrl="javascript:testf()"/>
<asp:MenuItemBinding DataMember="splitByObject" TextField="name" ValueField="value"/>
<asp:MenuItemBinding DataMember="statDefinition" TextField="name" ValueField="value"/>
</DataBindings>
</asp:Menu>
<asp:XmlDataSource ID="XmlDataSource1" runat="server"
DataFile="~/App_Data/stats.xml"></asp:XmlDataSource>
<asp:Literal ID="Message" runat="server" Text="Status:"></asp:Literal>
<asp:TextBox ID="TextBox1" runat="server" EnableTheming="True" Height="137px"
Rows="6" TextMode="MultiLine" Width="842px"></asp:TextBox>
</div>
</form>
</body>
</html>
Replace
<asp:MenuItemBinding DataMember="splitByField" TextField="name" ValueField="value" NavigateUrl="javascript:testf()"/>
with
<asp:MenuItemBinding DataMember="splitByField" TextField="name" ValueField="value" OnClientClick="return testf()"/>
Related
I'm trying to run a JS function that uses JQuery to append information to the HTML of an ASP page and I just can't get it to work. I've made some research about it but maybe it's something a lot simpler.
%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Pruebas.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
const add = (param) => {
alert("hi");
$("div.auto-style1").append("<p>'" + param + "'</p>");
}
</script>
<form id="form1" runat="server">
<div>
</div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" style="height: 26px" Text="Button" />
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<div class="container">
some text
</div>
</form>
</body>
</html>
more specifically this is the function im trying to run
<script type="text/javascript">
const add = (param) => {
alert("hi");
$("div.auto-style1").append("<p>'" + param + "'</p>");
}
</script>
im trying to call this function from the C# code behind the page like this
protected void Button1_Click(object sender, EventArgs e)
{
String p = "this is a message";
ScriptManager.RegisterClientScriptBlock(this, GetType(), "myfunction", "add('" + p + "');", true);
}
If I try to run a JS function that produces a console log or an alert on the page it works fine. Maybe it is related to the Postback property in the ASP button?
If anyone can shed some light on this I will be thankful.
There is no any .auto-style1 class in your code. Please add a class in div to work this code.
<form id="form1" runat="server">
<div class="auto-style1">
</div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" style="height: 26px" Text="Button" />
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<div class="container">
some text
</div>
</form>
Initially I able to preview image after select image. After added the master page, the javascript is not working anymore.
This is my master page
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="Testing.Site1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></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>
This is my content page
<%# Page Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="Testing1.aspx.cs" Inherits="Testing.Testing1" %>
<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
<script type="text/javascript">
function imagepreview(input)
{
if (input.files && input.files[0]) {
var fildr = new FileReader();
fildr.onload = function (e) {
$('#Image1').attr('src', e.target.result);
};
fildr.readAsDataURL(input.files[0]);
}
}
</script>
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<asp:Image ID="Image1" runat="server" height="100" Width="100" />
<br />
<br />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
<br />
<br />
<asp:TextBox ID="tbRetrieve" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnRetrieve" runat="server" Text="Retrieve" OnClick="btnRetrieve_Click" />
<br />
<asp:Image ID="Image2" runat="server" />
<br />
<asp:Label ID="lblMessage" runat="server"></asp:Label>
<br />
<br />
</asp:Content>
And this is my code behind
protected void Page_Load(object sender, EventArgs e)
{
FileUpload1.Attributes["onchange"] = "imagepreview(this)";
if (!IsPostBack)
{
lblMessage.Visible = false;
}
}
So what is the problem?
on adding master page the code of finding the control id will be changed you will have to replace your code like this.
<script type="text/javascript">
function imagepreview(input)
{
if (input.files && input.files[0]) {
var fildr = new FileReader();
fildr.onload = function (e) {
$('#<%=Image1.ClientID%>').attr('src', e.target.result);
};
fildr.readAsDataURL(input.files[0]);
}
}
You need to use code like that in order to call your javascript method on behind.
Page.ClientScript.RegisterStartupScript(this.GetType(),"CallMyFunction","MyFunction()",true);
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 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";