below is my code
Can you tell me how to send label1.text on this page to product.aspx and update label1.text ???
product.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="styles/modal-window.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" language="javascript" src="scripts/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript" src="scripts/modal-window.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="0"></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="icrement" />
show popup
</div>
</form>
</body>
</html>
product.aspx.cs code behind
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
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = (int.Parse(Label1.Text) + 1).ToString();
}
}
viewcart.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="decrement"
onclick="Button1_Click" />
<br />
current value:
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
viewcart.aspx.cs codebehind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class viewcart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Label1.Text = Request["fn"];
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = (int.Parse(Label1.Text) - 1).ToString();
}
}
You can add an asp:HiddenField control to the page, set its value through javascript and it will be posted back to your codebehind.
Related
i have this little proyect that brings a little window to take the data from the "modal" window to the main but i have problems putting in to work.
The index:
%# Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="WebTest.Index" %>
<!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>
<script type="text/javascript" src="Ventana.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:HiddenField ID="valor1" runat="server" />
<asp:HiddenField ID="valor2" runat="server" />
<asp:TextBox ID="txtIdRutPersona" runat="server"></asp:TextBox>
<asp:Button ID="btnAbrirPopup" runat="server" Text="Abrir" />
</div>
</form>
</body>
</html>
the .cs part of index:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebTest
{
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
btnAbrirPopup.Attributes.Add("onclick", "javascript:Asistencia('txtIdRutPersona');");
}
}
}
}
the "persona" window that is the page that bind the data to the index:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Persona.aspx.cs" Inherits="WebTest.Persona" %>
<!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>
<script type="text/javascript" src="Ventana.js"></script>
<script type="text/javascript">
function cerrar()
{
self.close();
}
</script>
</head>
<body>
<form id="Buscar" method="post" runat="server">
<div>
<asp:TextBox ID="txtRut" runat="server"></asp:TextBox>
<asp:Button ID="btnOk" runat="server" Text="Aplicar" />
<asp:Button ID="btnCerrar" runat="server" Text="Cerrar" />
</div>
</form>
</body>
</html>
the .cs part of "persona":
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebTest
{
public partial class Persona : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["form"] = Request.QueryString["formname"];
ViewState["txtRut"] = Request.QueryString["txtRut"];
ViewState["postBack"] = Request.QueryString["postBack"];
btnOk.Attributes.Add("onClick", $"window.opener.SetCodigo({ViewState["form"]},{ViewState["txtRut"]},{ViewState["postBack"]})");
btnCerrar.Attributes.Add("onClick", "cerrar()");
}
}
}
}
and the js:
var VentanaOrigen;
function Asistencia(txtRut) {
popUp = window.open('Persona.aspx?formname=' + document.forms[0].name + '&txtRut=' + txtRut, '', 'width=430,height=300,left=200,top=150,resizable=yes,status=yes,scrollbars=yes');
}
function SetCodigo(formulario, txtIdRutPersona, IdRutPersona, IPostback) {
eval('var theform = document.' + formulario + ';');
VentanaOrigen.close();
theform.elements[txtIdRutPersona].value = IdRutPersona;
if (IPostback)
__doPostBack(txtIdRutPersona, '');
}
sorry to bother with the large text but im kinda frustated right now, thanks!
It's difficult what you're trying to achieve. I am guessing:
index.aspx has a button to load a popup (which is not modal)
The button passes in the ID of textbox txtIdRutPersona
Index.aspx
<input type="button" value="Abrir" onclick="Asistencia('<%= txtIdRutPersona.ClientID %>')" />
Use client ID in case ASP.NET generates a different ID. Also you should get errors if you change ID (on a later date).
<script type="text/javascript">
function Asistencia(txtIdRutPersonaID) {
popUp = window.open('Persona.aspx?txtIdRutPersonaID=' + txtIdRutPersonaID, '', 'width=430,height=300,left=200,top=150,resizable=yes,status=yes,scrollbars=yes');
}
</script>
persona.aspx
<asp:TextBox ID="txtRut" runat="server"></asp:TextBox>
<input type="button" value="Aplicar" onclick="SetCodigo('<%= TxtIdRutPersonaID %>', '<%= txtRut.ClientID %>')" />
<input type="button" value="Cerrar" onclick="cerrar()" />
You can send the data to index.aspx using window.opener and the ID from index.aspx (txtIdRutPersonaID)
<script type="text/javascript">
function SetCodigo(txtIdRutPersonaID, txtRutID) {
if (window.opener != null && !window.opener.closed) {
var txtIdRutPersona = window.opener.document.getElementById(txtIdRutPersonaID);
// txtIdRutPersona is the textbox from index.aspx
txtIdRutPersona.value = document.getElementById(txtRutID).value;
}
window.close();
}
function cerrar()
{
self.close();
}
</script>
persona.aspx.cs
public string TxtIdRutPersonaID
{
get
{
return Request.QueryString["txtIdRutPersonaID"];
}
}
Alternatively you could use ASP.NET code behind to generate onclick event (but theres no good reason to). E.g:
btnOk.Attributes.Add("onclick", $"SetCodigo('{txtIdRutPersonaID}','{txtRut.ClientID}')");
Likewise, theres no reason to use ViewState["form"], ViewState["txtRut"] & ViewState["postBack"]
When adding the JQuery JS include the InnetHTML is not working:
My HTML code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="aaa.aspx.cs" Inherits="aaa" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile.structure-1.4.5.min.css"
/>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="ppdiv" runat="server">
</div>
</div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" style="height: 26px" Text="Button" />
</form>
</body>
</html>
My C# code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class aaa : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ppdiv.InnerHtml = "aaa";
}
}
when include JQUery the InnetHTML is not working. Where is the error and how to fix it.
Thanks in advance
i believe it is because you are using runat="server" in head within which you add the jquery. jquery and javascript for client side, and runat="server" posts the data to server. remove runat="server" from head and check.
I have just started teaching myself dotNet with C# and have hit a problem. Basically I have a variable that I want initialised when the page is first loaded. I have tried to use Session_Start to do this. However when I put a breakpoint at that point, it never seems to get there.
What I am not understanding?
C#
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
{
int numTries;
protected void Session_start(object sender, EventArgs e)
{
numTries = 3;
}
protected void CheckNum_ServerClick(object sender, EventArgs e)
{
if (numTries>0)
{
numTries--;
}
}
protected void Page_Load(object sender, EventArgs e)
{
Answer.InnerText = "You have "+numTries.ToString()+" changes left.";
}
}
HTML
<%# 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">
<div>
Guess a number between 1 & 100
<input type="text" id="Guess" runat="server" />
<br /><br />
<input type="submit" value="OK" id="CheckNum" runat="server" onserverclick="CheckNum_ServerClick" />
<br /><br />
<p id="Answer" runat="server"></p>
</div>
</form>
</body>
</html>
I am using Visual Studio Community 2013
the aspx code:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="MyWebForm.aspx.cs" Inherits="MyWebApplication.MyWebForm" %>
<!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>
</head>
<body style="height: 334px">
<form id="form1" runat="server">
<div style="height: 338px">
<asp:TextBox ID="MyTextBox" runat="server" AutoPostBack="True"></asp:TextBox>
<br />
<asp:CustomValidator ID="MyCustomValidator" runat="server"
ControlToValidate="MyTextBox" ErrorMessage="My error message"
onservervalidate="Foo2"></asp:CustomValidator>
</div>
</form>
</body>
</html>
the C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MyWebApplication
{
public partial class MyWebForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
bool Foo1(string bar)
{
if (bar == "bar")
return true;
return false;
}
protected void Foo2(object source, ServerValidateEventArgs args)
{
args.IsValid = this.Foo1(this.MyTextBox.Text);
} // breakpoint
}
}
I set breakpoint in the Foo2 function (comment), but debugger hasn't visited it. I tried writing diffrent text, not writting anything and writting "bar" and pushing enter and tab after that, but it still doesn't work. I mean text changing action. Is it possible?
There's no submit button. You need to add a button to submit the form, which will then trigger page validation.
<asp:Button runat="server" id="myButton" Text="Submit" />
I have an ASP button that lookts like this:
<asp:Button
ID="btnReset"
runat="server"
OnClientClick = "hideOverlay('<%=pnlOverlay.ClientID %>', '<%=pnlAddComment.ClientID %>');"
CssClass ="btnCancel PopUpButton"
/>
The problem are the asp tags in de hideOverlay part.I don't get it working. Why isn't working? And how do i fix it?
Try below examples
First Example
In aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnReset" runat="server" CssClass="btnCancel PopUpButton" />
<asp:Panel ID="pnlOverlay" runat="server">
</asp:Panel>
<asp:Panel ID="pnlAddComment" runat="server">
</asp:Panel>
</div>
</form>
</body>
</html>
In Codebehind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default10 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
btnReset.Attributes.Add("onclick", string.Format("hideOverlay('{0}','{1}')", pnlOverlay.ClientID, pnlAddComment.ClientID));
}
}
It will generate the below source for the button
<input type="submit" name="btnReset" value="" onclick="hideOverlay('pnlOverlay','pnlAddComment');" id="btnReset" class="btnCancel PopUpButton" />
Second Example
In Aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnReset" runat="server" CssClass="btnCancel PopUpButton" OnClientClick=<%# "hideOverlay('" + pnlOverlay.ClientID + "', '" + pnlAddComment.ClientID +"');" %> />
<asp:Panel ID="pnlOverlay" runat="server">
</asp:Panel>
<asp:Panel ID="pnlAddComment" runat="server">
</asp:Panel>
</div>
</form>
</body>
</html>
In CodeBehind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default10 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
btnReset.DataBind();
}
}
It will generate the below source for the button
<input type="submit" name="btnReset" value="" onclick="hideOverlay('pnlOverlay', 'pnlAddComment');" id="btnReset" class="btnCancel PopUpButton" />
Third Example
In aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnReset" runat="server" CssClass="btnCancel PopUpButton" OnClientClick="hideOverlay();" />
<asp:Panel ID="pnlOverlay" runat="server">
</asp:Panel>
<asp:Panel ID="pnlAddComment" runat="server">
</asp:Panel>
</div>
</form>
</body>
<script type="text/javascript" >
function hideOverlay()
{
var pnlOverlayID = '<%= pnlOverlay.ClientID %>';
var pnlAddCommentID = '<%= pnlAddComment.ClientID %>';
//Do your stuff
}
</script>
</html>
It will generate the follwing source for the script portion
<script type="text/javascript" >
function hideOverlay()
{
var pnlOverlayID = 'pnlOverlay';
var pnlAddCommentID = 'pnlAddComment';
//Do your stuff
}
</script>
Try to replace "=" with "#" in you inline code. e.g. <%=pnlOverlay.ClientID %> => <%#pnlOverlay.ClientID %>, so that the ClientId is instantiated in compile time.
OnClientClick is only used to call client-sidee script such as javascript code. If you are trying to call a method in code behind, you should use OnClick event.
Change your code to:
<asp:Button
ID="btnReset"
runat="server"
OnClientClick=<%# "hideOverlay('" + pnlOverlay.ClientID + "', '" + pnlAddComment.ClientID +"');" %>
CssClass ="btnCancel PopUpButton"
/>
or even nicer if you use string.Format()
OnClientClick=<%# string.Format("hideOverlay('{0}', '{1}');", pnlOverlay.ClientID,pnlAddComment.ClientID) %>
And don't forget to databind the link, and yes the onclientclick doesn't have " , since those are used inside the <%# %> tags
You can try this.
i. In the code behind
btnReset.OnClientClick = "hideOverlay'"+pnlOverlay.ClientID+"','"+pnlAddComment.ClientID+"')";
ii. The second approach is to use an inline javascript.
<asp:Button ID="btnReset" runat="server" OnClientClick = "newhideOverlay()" CssClass
="btnCancel PopUpButton"/>
<script type="text/javascript">
function newhideOverlay()
{
hideOverlay('<%=pnlOverlay.ClientID %>', '<%=pnlAddComment.ClientID %>');
}
</script>
Also ensure that both pnlOverlay and pnlAddComment load before the button.