this code is a excerpt from the book asp.net all in one reference for dummies on pages 18 and 19. The problem with this is that it doesn't compile. The compiler says txtFirst, txtSecond and lblAnswer are not found in the current context. I am new in this field, could someone please help me out ?
<%# Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_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 id="Head1" runat="server">
<title>Simple Calculator</title>
</head>
<body>
<form id="form2" runat="server">
<div>
<h1>The Simple Calculator</h1>
First number:
<asp:TextBox ID="txtFirst" runat="server" />
<br />
<br />
Second number:
<asp:TextBox ID="txtSecond" runat="server" />
<br />
<br />
<asp:Button ID="btnAdd" runat="server"
OnClick="btnAdd_Click" Text="Add" />
<br />
<br />
The answer is:
<asp:Label ID="lblAnswer" runat="server" />
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void btnAdd_Click(object sender,EventArgs e)
{
decimal a = decimal.Parse(txtFirst.Text);
decimal b = decimal.Parse(txtSecond.Text);
decimal c = a + b;
lblAnswer.Text = c.ToString();
}
}
}
Try this version instead - I've tested it and it works:
ASPX:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="Test.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>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>The Simple Calculator</h1>
First number:
<asp:TextBox ID="txtFirst" runat="server" />
<br />
<br />
Second number:
<asp:TextBox ID="txtSecond" runat="server" />
<br />
<br />
<asp:Button ID="btnAdd" runat="server"
OnClick="btnAdd_Click" Text="Add" />
<br />
<br />
The answer is:
<asp:Label ID="lblAnswer" runat="server" />
</div>
</form>
</body>
</html>
CODE-BEHIND:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace Test
{
public partial class Default : System.Web.UI.Page
{
protected void btnAdd_Click(object sender, EventArgs e)
{
decimal a = decimal.Parse(txtFirst.Text);
decimal b = decimal.Parse(txtSecond.Text);
decimal c = a + b;
lblAnswer.Text = c.ToString();
}
}
}
you must put your c# code in code behind page in solution explorer of Visual studio find Default.aspx.cs and copy
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void btnAdd_Click(object sender,EventArgs e)
{
decimal a = decimal.Parse(txtFirst.Text);
decimal b = decimal.Parse(txtSecond.Text);
decimal c = a + b;
lblAnswer.Text = c.ToString();
}
}
}
to the file I hope this help you
if you don't want to use separate .cs file then you have to use script tag in your aspx page.
<%# Page Language="C#">
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0
Transitional//EN" “http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd">
<script runat="server">
protected void btnAdd_Click(object sender,EventArgs e)
{
decimal a = decimal.Parse(txtFirst.Text);
decimal b = decimal.Parse(txtSecond.Text);
decimal c = a + b;
lblAnswer.Text = c.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Simple Calculator</title>
</head>
<body>
<form id="form2" runat="server">
<div>
<h1>The Simple Calculator</h1>
First number:
<asp:TextBox ID="txtFirst" runat="server" />
<br />
<br />
Second number:
<asp:TextBox ID="txtSecond" runat="server" />
<br />
<br />
<asp:Button ID="btnAdd" runat="server"
OnClick="btnAdd_Click" Text="Add" />
<br />
<br />
The answer is:
<asp:Label ID="lblAnswer" runat="server" />
</div>
</form>
</body>
</html>
Make two changes
In your aspx page, change to Inherits="WebApplication2.Default"
In your code behind change the class name to Default instead of _Default.
Then rebuild the solution.
Another reason could be copy-paste problem which sometimes messes up the designer file. Try deleting the controls and re-adding them manually.
Are you making a web site or a web application? If the latter, then try deleting (or removing/renaming, if you prefer to back it up first) the Default.aspx.cs file. Then right-click the Default.aspx file in Solution Explorer and select "Convert to Web Application".
Related
Here is the main form where I have to take user inputs. I have used Design option to create the form in visual studio.
WebForm1.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ArthmeticClient.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
Enter Value 1:
<input id="Text1" type="text" /><br />
<br />
Enter Value 2:
<input id="Text2" type="text" /><br />
<br />
Resul:
<input id="Text3" type="text" /><br />
<br />
<input id="Button1" type="button" value="Addition" />
<input id="Button2" type="button" value="Multiplication" /><br />
<br />
</div>
</form>
</body>
</html>
I have used the same id for the input tag but still it cannot find the 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 ArthmeticClient
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
ServiceReference1.IService1 ob = new ServiceReference1.Service1Client();
int num1 = int.Parse(Text1.Text);
int num2 = int.Parse(Text2.Text);
Text3.Text = Convert.ToString(ob.Sum(num1, num2));
}
protected void Button2_Click(object sender, EventArgs e)
{
ServiceReference1.IService1 ob = new ServiceReference1.Service1Client();
int num1 = int.Parse(Text1.Text);
int num2 = int.Parse(Text2.Text);
Text3.Text = Convert.ToString(ob.Multiply(num1, num2));
}
}
}
I have to just a small option, I have created simple form, I dont think there a problem with the id I have given. but it is still showing this error. Idk what is wrong.
Can anybody help...
My short query is that i need to check the login form data that the value entered in name by user to be checked against all the values under column name in the database using LINQ query expression only and if it exists i want to select that particular row to which the name field matches with and redirect the user to another Dashboard page.
Below is the code which stores and selects the row if the record is found in the database into a variable query.Now it's not able to check whether the variable in query points to any row if selected or not of the database table.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="SessionHandling.aspx.cs" Inherits="WebApplication5.SessionHandling" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>
THIS IS LOGIN FORM
</h1>
Name:
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
<br />
E-mail:
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication5
{
public partial class SessionHandling : System.Web.UI.Page
{
DataClasses1DataContext db = new DataClasses1DataContext();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
tblContact tb = new tblContact();
var query = from x in db.tblContacts where x.name == txtName.Text select x;
}
}
}
Did you try this from your C# code?
if (query.Any())
{
Response.Redirect("http://www.microsoft.com")
}
else
{
Response.Redirect("http://www.google.com")
}
I have the following aspx and cs
<%# Page Language="C#" AutoEventWireup="true" CodeFile="cardPrintingBarcode.aspx.cs" Inherits="Reports_cardPrintingBarcode" %>
<!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>
Impresión de Tarjeta
</title>
<style type="text/css">
.style2
{
font-size: xx-small;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="Card">
<table>
<tr>
<td width="85px" align="center" valign="bottom">
<image ID="imgBarcode" Width="85px" Height="20px" />
<br />
<label ID="lblCardcode" Font-Bold="True" Font-Names="Arial" Font-Size="5pt" >
</label>
</td>
<td width="30px" />
<td width="40px" align="center" valign="bottom">
<label ID="lblCN" Font-Bold="True" Font-Names="Arial" Font-Size="7pt">
</label>
<br />
<image ID="imgQR" Width="37px" Height="37px" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ClubCard.Core;
public partial class Reports_cardPrintingBarcode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (ViewState["CHECKED_CARDS_ITEMS"] != null)
{
if (((List<int>)ViewState["CHECKED_CARDS_ITEMS"]).Count > 0)
{
LoadCards();
}
else
{
this.Response.Redirect("~/default.aspx");
//GET ME OUT OF HERE
}
}
}
private void LoadCards()
{
List<int> lstIdCards = (List<int>)ViewState["CHECKED_CARDS_ITEMS"];
foreach (int intIdCard in lstIdCards)
{
//Process Where I Want To Assign Data To 2 Labels and 2 images
ClubCard.Core.Card card = new ClubCard.Core.Card(intIdCard);
ClubCard.Core.Client client = new ClubCard.Core.Client(card.idClient);
Byte[] bcQR = QR.GetQRByteArray(card.cardCode);
string strBase64 = Convert.ToBase64String(bcQR, 0, bcQR.Length);
//lblCN.Text = client.number;
//lblCardCode.Text = card.number;
//imgBarcode.ImageUrl = "SOME URL";
//imgQR.ImageUrl = "SOME URL";
//PROBABLY HERE'S WHERE I HAVE TO CREATE ALL WEBCONTROLS...
//THEN WHEN THE FOREACH STARTS AGAIN, CREATE ANOTHER ONE...
}
}
}
The point here is to create the table seen in the aspx as many times as the (List)ViewState["CHECKED_CARDS_ITEMS"]).Count.
For instance... if (List)ViewState["CHECKED_CARDS_ITEMS"]).Count = 5, then I want to see the table and all of it's content 5 times (obviously, each will be different because server side I'm assigning different data to each, thus, the use of the foreach)
How can I clone those WebControls?
I've heard that I must not use runat="server" on those controls (however I'm not sure if in this example will work), some other sources claim that it should be inside a div, and clone the div.
You probably needs a ListControl e.g. <asp:DataList />, <asp:GridView etc.
Create a list of your cards
List<ClubCard.Core.Card> cards = new List<ClubCard.Core.Card>();
//cards.Add(...some cards...);
//cards.Add(...some cards...);
//cards.Add(...some cards...);
Bind your list to a ListControl
cardListControl.DataSource = cards;
cardListControl.DataBind();
Implementing this with a DataList:
<asp:DataList runat="server" ID="cardListControl">
<ItemTemplate>
<!-- Layout to display card here -->
<img src='<%# Eval("CardImageUrl")>' />
<span><%# Eval("CardName") %></span>
<!-- other card information -->
</ItemTemplate>
</asp:DataList>
DataList has a RepeatDirection property that let you display your cards Horizontally or Vertically
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;
First I have to let you know that i am a newbie in this area, learning from tutorials.
With that said I'm looking for a way to load sourcecode from the codebehind file into a textbox, when clicking a button. Same goes for the aspx file.
Im making this website, where i am going to show code examples from what im doing. So if I navigate to myweb.com/tutorial1done.aspx this page would show me the final result from the tutorial done. When i click the show source button it should make 2 textboxes visible, and add the codebehind to the first box, and the aspx source to the second box.
I don't know if it is possible, but I am hoping so.
This far i have this:
ASPX:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="DateTimeOutput.aspx.cs" Inherits="WebApplication1.DateTimeOutput" %>
<!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 rel="stylesheet" href="../styles/codeformatter.css" />
</head>
<body>
<form id="form1" runat="server">
<customControls:Header runat="server" heading="Date and Time Output" />
<div>
<asp:Panel ID="Panel1" runat="server">
<asp:TextBox ID="outputText" runat="server" Height="175px" TextMode="MultiLine"
Width="400px"></asp:TextBox>
</asp:Panel>
</div>
<asp:Panel ID="Panel2" runat="server">
<asp:Button ID="runButton" runat="server" Text="Run Code"
onclick="runButton_Click" Width="95px" />
<asp:Button ID="clearButton" runat="server" Text="Clear Console"
onclick="clearButton_Click" Width="95px" />
<br />
<br />
<asp:Button ID="dt_showSource_btn" runat="server"
onclick="dt_showSource_btn_Click" Text="Show Source" />
</asp:Panel>
<asp:Label ID="dtLabel1" runat="server" Text="Code Behind:" Visible="False"></asp:Label>
<br />
<asp:TextBox ID="dtcb_output" runat="server" Height="175px"
TextMode="MultiLine" Visible="False" Width="400px"></asp:TextBox>
<br />
<br />
<asp:Label ID="dtLabel2" runat="server" Text="ASPX:" Visible="False"></asp:Label>
<br />
<asp:TextBox ID="dtaspx_output" runat="server" Height="175px"
TextMode="MultiLine" Visible="False" Width="400px"></asp:TextBox>
</form>
</body>
</html>
And the Codebehind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class DateTimeOutput : System.Web.UI.Page
{
protected void output(String value)
{
outputText.Text += value + Environment.NewLine;
}
protected void runButton_Click(object sender, EventArgs e)
{
DateTime dt = new DateTime();
output(dt.ToString());
DateTime nowDt = DateTime.Now;
output(nowDt.ToString());
}
protected void clearButton_Click(object sender, EventArgs e)
{
outputText.Text = "";
}
protected void dt_showSource_btn_Click(object sender, EventArgs e)
{
if (dtcb_output.Visible == false)
{
dtLabel1.Visible = true;
dtcb_output.Visible = true;
}
else
{
dtLabel1.Visible = false;
dtcb_output.Visible = false;
}
if (dtaspx_output.Visible == false)
{
dtLabel2.Visible = true;
dtaspx_output.Visible = true;
}
else
{
dtLabel2.Visible = false;
dtaspx_output.Visible = false;
}
}
}
}
For now source highlighting is not needed, unless its easy to do.
Thx in advance.
If you're refering to the actual code of your code behind file, you have a problem. As the file will be compiled and then be placed as intermediate code in a dynamic link library (.dll), you don't have access to the .aspx.cs file any more. The only way to go would be to include the code behind file with the deployd project and open it with a FileStream (or whatever) to read it and display it's content.