I need value to be passing from ASP.NET page to JavaScript and then to HTML textfield.
My following code is able to read values from ASP.NET to JavaScript but unable to pass value form JavaScript to HTML TextField. Can anybody please correct my code.
Default.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript">
function can() {
var myVariable = '<%=ServerSideVariable %>';
document.write(myVariable);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<br />
<br />
<br />
<input id="Text1" type="text" value='can()'/>
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
public string ServerSideVariable { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
string pval = "Passed value;
ServerSideVariable = pval;
}
}
document.getElementById('Text1').value = myVariable;
But you need to move the assignment script block bellow the declaration of element Text1, or do it on document.ready
Anyway, you also need to error check.
It's best to use JQuery anyway, and get familiar with it.
As others have said with javascript OR like this:
(since there is nothing in your example that requires javascript -- KISS-- don't use javascript if you don't have to do so.)
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<br />
<br />
<br />
<input id="Text1" type="text" ><%=ServerSideVariable %></input>
</div>
</form>
</body>
</html>
Do this instead in a document.load.
document.getElementByID('Text1').value = myVariable;
Related
I'm working on trying to implement a simple cache work-around for a project.
In the project I have a public string:
public string noCache = DateTime.Now.Second.ToString();
I'm trying to bring this in at the end of the css reference so it prevents pulling the cached version.
<link rel="stylesheet" href="./css/core.css?version=<%= noCache%>">
This would make the url "./css/core.css?version=<%= noCache%>" literal and not plug in the variable. After some reading I've found out that Inline Expressions aren't supported in .net. The weird thing is, before I figured that out I did this:
<link rel="stylesheet" href="./css/core.css?version=<%= noCache%>" <%= noCache%> />
For whatever reason it works when I use this. Can someone explain to me why and how it'd be best to set something like this up without weird workaround.
AppSite.Master:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="AppSite.master.cs" Inherits="APP_Partnership.AppSite" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title><%# noCache%></title>
<link runat="server" id="CSS1" rel="stylesheet" href="<%= noCache%>"/>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div class="container">
<div class="header">
APP SITE
</div>
<div class="content">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</div>
</form>
</body>
</html>
AppSite.Master.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace APP_Partnership
{
public partial class AppSite : System.Web.UI.MasterPage
{
public string noCache = DateTime.Now.Second.ToString();
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
I would use a UserControl to do that.
Here the code for the usercontrol ascx page:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="CacheControl.ascx.cs" Inherits="Usercontrols_CacheControl" %>
<link rel="stylesheet" href="./css/core.css?version=<%= noCache%>">
The code behind file:
protected string noCache = DateTime.Now.Second.ToString();
protected void Page_Load(object sender, EventArgs e)
{
}
Adding to the Masterpage:
<%# Register TagPrefix="MyTagName" TagName="CacheControl" Src="~/Usercontrols/CacheControl.ascx" %>
and in the head section:
<MyTagName:CacheControl ID="CacheControl" runat="server" />
The output on the page in the header looks like that:
<link rel="stylesheet" href="./css/core.css?version=3">
Hope this helps.
I read some forums and found an easier way to call a C# Method from JavaScript but it's not working. I did it in my live app and it didn't work so I took a fresh project and used the code as below:
ASPX Mark-up
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="sm" runat="server" EnablePageMethods="true"></asp:ScriptManager>
<div>
<asp:Button ID="btnMe" runat="server" OnClientClick="jsfun()" />
</div>
</form>
</body>
</html>
Javascript
<script type="text/javascript">
function jdfun() {
PageMethods.CSFun(onSucess, onError);
}
function onSucess(result) {
alert(result);
}
function onSucess(result) {
alert(result);
}
</script>
C#
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string CSFun()
{
string result = "Hey Yeah";
return result;
}
}
No Error No Exception. The Debugger is not even going into the C# code.
Can anyone help me out.
Thanks.
Edit
I didn't really know about this, but I read a little and fixed your code.
Here is the code that works:
js:
<script type="text/javascript">
function jsfun() {
PageMethods.CSFun(onSuccess, onError);
}
function onSuccess(result) {
alert(result);
}
function onError(result) {
alert(result);
}
</script>
aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form2" runat="server">
<asp:ScriptManager ID="sm" runat="server" EnablePageMethods="true"></asp:ScriptManager>
<div>
<asp:Button ID="Button1" runat="server" OnClientClick="jsfun()" />
</div>
</form>
</body>
</html>
Basic Example to do the same
aspx page
<form runat="server">
<asp:ScriptManager ID="ScriptManager" runat="server"
EnablePageMethods="true" />
<fieldset id="ContactFieldset">
<label>
Your Name
<input type="text" id="NameTextBox" /></label>
<label>
Email Address
<input type="text" id="EmailTextBox" /></label>
<label>
Your Message
<textarea id="MessageTextBox"></textarea></label>
<button onclick="SendForm();">
Send</button>
</fieldset>
</form>
Page Method (.cs)
using System;
using System.Web.Services;
public partial class ContactUs : System.Web.UI.Page
{
[WebMethod]
public static void SendForm(string name, string email, string message)
{
if (string.IsNullOrEmpty(name))
{
throw new Exception("You must supply a name.");
}
if (string.IsNullOrEmpty(email))
{
throw new Exception("You must supply an email address.");
}
if (string.IsNullOrEmpty(message))
{
throw new Exception("Please provide a message to send.");
}
// If we get this far we know that they entered enough data, so
// here is where you would send the email or whatever you wanted
// to do :)
}
}
javascript function
function SendForm() {
var name = $get("NameTextBox").value;
var email = $get("EmailTextBox").value;
var message = $get("MessageTextBox").value;
PageMethods.SendForm(name, email, message,
OnSucceeded, OnFailed);
}
function OnSucceeded() {
// Dispaly "thank you."
$get("ContactFieldset").innerHTML = "<p>Thank you!</p>";
}
function OnFailed(error) {
// Alert user to the error.
alert(error.get_message());
}
I would like to 'transfer' a textbox value into a html code. E.g the textbox value is a website like http://www.google.com in form1.aspx. and i would like the value to be put into the html code in form2.aspx . Might i ask how i should be going around on this?
<span class="logoposition">
<img src="Logo.jpg" height= "120"; width="280"; />
</span>
Like i would like the textbox1 value to replace the current www.google.com
Here's how you can do this:
Webform1 Markup:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SOTest1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Your text: ">
</asp:Label><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="GO" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
Webform1 Code:
using System;
namespace SOTest1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
string myString = Server.HtmlEncode(TextBox1.Text);
Response.Redirect("Webform2.aspx?mystring=" + myString);
}
}
}
Webform2 Markup:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="SOTest1.WebForm2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<span class="logoposition">
<img src="Logo.jpg" height= "120"; width="280"; />
</span>
</div>
</form>
</body>
</html>
Webform2 Code:
using System;
namespace SOTest1
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string mystring = Request.QueryString["mystring"];
myAnchor.HRef = mystring;
myAnchor.InnerText = mystring;
}
}
}
So, I'm not sure what's going on. My boss wasn't pleased with me using MVC and Razor, so I'm being forced to code with this nasty webcontrol/codebehind style. =/
The error is:
Only Content controls are allowed directly in a content page that contains Content controls.
Here is the masterpage:
<%# Master Language="C#"%>
<!DOCTYPE html>
<html>
<head>
<title>Application Form</title>
</head>
<body>
<div id="container">
<asp:contentplaceholder id="contentPlaceHolder" runat="server" />
</div>
</body>
</html>
And here is the Default.aspx page that's throwing the error.
<%# Page Language="C#" Inherits="dumb.Default" MasterPageFile="MasterPage.master" %>
<h2>Application Form</h2>
<asp:Content id="content" ContentPlaceHolderID="contentPlaceHolder" runat="server">
<p>Please complete this application.</p>
<form action="Default.aspx" method="post" runat="server">
<div>
<span class="spaced">Name:</span><asp:TextBox id="name" runat="server" />
</div>
</form>
<p>
<asp:Label id="finalmessage" runat="server" />
</p>
</asp:Content>
And the silly Default.aspx.cs codebehind...
using System;
using System.Web;
using System.Web.UI;
namespace dumb
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
if (IsPostBack) {
finalmessage.Text = "Submission Processed Successfully!";
}
}
}
}
All of your content, including static HTML must be inside Content tags in the content page. You have a <h2> outside:
<h2>Application Form</h2>
<asp:Content id="content" ContentPlaceHolderID="contentPlaceHolder" runat="server">
Should be:
<asp:Content id="content" ContentPlaceHolderID="contentPlaceHolder" runat="server">
<h2>Application Form</h2>
I’m trying to integrate “jquery.mmenu” in a Visual Studio project.
It seems that “asp:Button” does not work inside a “mmenu nav “.
Nor a "asp:LinkButton" seems to fully perform.
Take the specific case of a login:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="test._default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="Scripts/jquery-2.1.4.min.js"></script>
<script src="Scripts/jquery-ui-1.11.4.min.js"></script>
<script src="Scripts/jquery.mmenu.min.all.js"></script>
<link href="Content/themes/base/all.css" rel="stylesheet" />
<link href="Content/themes/base/jquery.mmenu.all.css" rel="stylesheet" />
<title></title>
<script>
$(document).ready(function () {
$("#login").mmenu({
});
});
</script>
</head>
<body>
<div>
<form id="form1" runat="server">
Open login pannel<br />
<asp:LoginStatus ID="LoginStatus1" runat="server" LogoutAction="Redirect" LoginText="login" LogoutText="logout" LogoutPageUrl="~/" /><br />
<nav id="login">
<div>
<asp:Label ID="UserNameLabel" runat="server">User:</asp:Label><br />
<asp:TextBox ID="UserNameTextBox" runat="server"></asp:TextBox><br />
<asp:Label ID="PasswordLabel" runat="server">Password:</asp:Label><br />
<asp:TextBox ID="PasswordTextBox" runat="server" TextMode="Password"></asp:TextBox><br />
<asp:Button ID="LoginButton" runat="server" ClientIDMode="Static" Text="Login Button" OnClick="LoginButton_Click" /><br />
<asp:LinkButton ID="LoginLinkButton" runat="server" ClientIDMode="Static" OnClick="LoginLinkButton_Click">Login LinkButton</asp:LinkButton>
</div>
</nav>
</form>
</div>
</body>
</html>
The code behind looks like:
using System;
using System.Web.Security;
namespace test
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LoginButton_Click(object sender, EventArgs e)
{
if (Membership.ValidateUser(UserNameTextBox.Text, PasswordTextBox.Text))
{
FormsAuthentication.RedirectFromLoginPage(UserNameTextBox.Text, false);
}
}
protected void LoginLinkButton_Click(object sender, EventArgs e)
{
if (Membership.ValidateUser(UserNameTextBox.Text, PasswordTextBox.Text))
{
FormsAuthentication.RedirectFromLoginPage(UserNameTextBox.Text, false);
}
}
}
}
The asp:Button does not post back.
The asp:LinkButton does not login.
Is there a way to have it working?
Thanks.
Web forms most of the time change the name of the element and jquery needs the exact name to find your button. Add this property to your button and test again
ClientIDMode="Static"
The mmenu plugin grabs the NAV#login from the HTML and puts it back directly inside the BODY. Meaning, the input and button are no longer inside the FORM (and thus won't submit the form).
Basically, if you put the FORM inside the NAV#login all should work as expected.