Passing textbox value to ASP.NET class - asp.net

I always got this error:Compiler Error Message: CS0103: The name 'txtUserName' does not exist in the current context
mypage.aspx
<% #Page Language="C#" Inherits="myclass" Src="myclass.cs" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Login Form</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
Username:
</td>
<td>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="Writedata" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
myclass.cs
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Text;
public class myclass:Page
{
protected void WriteData(object sender, EventArgs e){
string customer_id=txtUserName.Text;
string postData="customer_id="+customer_id;
byte[] data= Encoding.UTF8.GetBytes (postData);
// Prepare web request...
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http://myserverurl.com/mypage.php");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();
}
}
Any help here?
Thanks,

Try changing your class declaration to "public partial class myclass:System.Web.UI.Page". The partial keyword is, I believe, critical for the compiler to know the balance of the class definition is created in temp/intermediate files it won't know about otherwise.

Related

will it change .aspx.cs file to take data from html element after converting asp controls into html?

my question was that when I send the request to the web server, of course, the web server will take the request and give me a page that will be in the HTML format so I want to know when the web server will execute asp server-side controls into HTML format than in the Testing. aspx.cs file will be changed as well? it means these two lines int no1 = Convert.ToInt32(textbox1.Text);
int no2 = Convert.ToInt32(textbox2.Text);
these two lines will be converted into like this int no1=Convert.ToInt32(Response["textbox1"]);
int no1=Convert.ToInt32(Response["textbox2"]); these two lines will be converted like this or not
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Testing.aspx.cs" Inherits="login.Testing" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body style="background-image:url(C:\Users\Sudarshan\source\repos\login\login\Images\uWjEogFLUTBc8mSvagdiuP.jpg)">
<form id="form1" runat="server">
<div>
<table align="center">
<tr>
<td>
Enter the Number 1:<asp:TextBox ID="textbox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Enter the Number 2:<asp:TextBox ID="textbox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<center>
<br />
<br />
<asp:Button ID="button1" runat ="server" Text="Add" Height="38px" OnClick="Unnamed1_Click" Width="101px" /> </center>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
--------------------------------------------------------------
Testing.aspx.cs
using System;
using System.Collections.Generic;
using System. Linq;
using System. Web;
using System.Web.UI;
using System.Web. UI.WebControls;
namespace login
{
public partial class Testing: System.Web. UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Unnamed1_Click(object sender, EventArgs e)
{
int no1 = Convert.ToInt32(textbox1.Text);
int no2 = Convert.ToInt32(textbox2.Text);
int result;
result = no1 + no2;
Response.Write(result);
}
}
}

Compilation Error: CS1061

I understand there are few answers quite similar on this site but I am unable to figure out what I am doing wrong here. Full error message are here:
Server Error in '/' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1061: 'ASP.webform1_aspx' does not contain a definition for 'addNumbers' and no extension method 'addNumbers' accepting a first argument of type 'ASP.webform1_aspx' could be found (are you missing a using directive or an assembly reference?)
Source Error:
Line 37:
Line 38:
Line 39:
Line 40:
Line 41:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script>
function addNumbers()
{
var firstNumber = parseFloat(document.getElementById("TextBox1").value);
var secondNumber = parseFloat(document.getElementById("TextBox2").value);
document.getElementById("TextBox3").value = firstNumber + secondNumber;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>First Number: </td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Second Number: </td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Result: </td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="Button1" runat="server" Text="Add" OnClick="addNumbers()" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
Code from as aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
This:
<asp:Button ID="Button1" runat="server" Text="Add" OnClick="addNumbers()" />
requires your to have OnClick method in code-behind file. I think that you need normal, not server-side button:
<button onclick="addNumbers()">Add</button>

ASP:DynamicControl rendering HTML rather then displaying source

I have a ASP dynamic scaffolding project in which I am customizing a details view. In my data source I have a few fields containing HTML and I would like to display the rendered HTML on the page rather then the source HTML as it is currently doing. The control looks like this.
<tr>
<td class="DDLightHeader">
<asp:Label ID="lblStatementHtml" runat="server" Text="Statement" />
</td>
<td>
<asp:DynamicControl ID="dcStatementHtml" runat="server" DataField="StatementHtml" />
</td>
</tr>
I am confused on how to proceed since the default view for this control is the Text.ascx which is apparently nothing more then a literal (the same base control I use to render the HTML on my site. I am comfortable with creating a new FieldTemplate to render the HTML, but how can I force the html to render rather then display its source view.
To resolve the above issue I had to create anew FieldTemplate
HTML.ascx
<%# Control Language="C#" CodeBehind="HTML.ascx.cs" Inherits="ProductDynamicCMS.HTML" %>
<asp:Literal runat="server" ID="Literal1" />
HTML.cs
using System;
using System.Web.DynamicData;
using System.Web.UI;
namespace ProductDynamicCMS
{
public partial class HTML : FieldTemplateUserControl
{
protected override void OnDataBinding(EventArgs e)
{
base.OnDataBinding(e);
object val = FieldValue;
if (val != null)
Literal1.Text = val.ToString();
}
public override Control DataControl
{
get
{
return Literal1;
}
}
}
}
Then applied the UIHint for this class to the field display in the details view template.
<tr>
<td class="DDLightHeader">
<asp:Label ID="lblStatementHtml" runat="server" Text="Statement" />
</td>
<td>
<asp:DynamicControl ID="dcStatementHtml" runat="server" DataField="StatementHtml" UIHint="HTML" />
</td>
</tr>
And all was well.

ASP.net - Could Not Load Type Nname.name

I am new to this programming language and I need help with this. Not very sure what is wrong. But it kept prompting the error below and it would not display the data from MySQL workbench database.
Server Error in '/' Application.
--------------------------------------------------------------------------------
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: Could not load type 'Nname.name'.
Source Error:
Line 1: <%# Master Language="C#" AutoEventWireup="true" CodeBehind="name.Master.cs" Inherits="Nname.name" %>
This is my form page:
<asp:Content ID="Content2" ContentPlaceHolderID="phContent" runat="server">
<table>
<tr>
<th>
ID
</th>
<th>
Name
</th>
<th>
Country Code
</th>
<th>
Disctrict
</th>
<th>
Population
</th>
</tr>
<tr class="large">
<td>
<asp:Label ID="ID" runat="server" Text=""></asp:Label>
</td>
<td>
<asp:Label ID="Name" runat="server" Text=""></asp:Label>
</td>
<td>
<asp:Label ID="CountryCode" runat="server" Text=""></asp:Label>
</td>
<td>
<asp:Label ID="District" runat="server" Text=""></asp:Label>
</td>
<td>
<asp:Label ID="Population" runat="server" Text=""></asp:Label>
</td>
</tr>
</table>
</asp:Content>
This is my database code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace P1
{
public class records
{
MySqlConnection con = null;
MySqlCommand cmd = null;
MySqlDataReader rdr ;
//DataSet ds = new DataSet(cmd,con); //
MySqlDataAdapter da = new MySqlDataAdapter();
public void read()
{
try
{
con = new MySqlConnection("Server=myServerName;Database=myDatabaseName;Uid=myID;Pwd=myPassword");
con.Open();
string cmd = "SELECT * FROM city WHERE ID=6;";
//da.Fill(ds);
con.Close();
}
catch()
{ }
}
}
}
You need to edit your master page and change
Inherits="Nname.name"
to
Inherits="name.name"
or
Inherits="name.master"

asp.net postbacks won't fire

I've made a simple ASP.net website and everything works fine on my computer. But on my colleague's computer no postbacks are fired although he uses the same OS (Win 7) AND the same browser as I do (IE9).
I don't have any access to his computer right now which makes it kind of hard to debug. Does anybody know what could prevent postbacks on one computer although they work on a different one with the same browser?
(I also tried it on a 3rd computer with different browser and OS and it worked there too)
//update: some code
The following code is one of the pages where the problem occures.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="NewList.aspx.cs" Inherits="QAList.NewList" %>
<!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>Items</title>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
<link href="qalist.css" rel="stylesheet" type="text/css" />
<script src="lib/jquery-1.4.3.min.js" type="text/javascript"></script>
<script src="lib/jquery-ui-1.8.5.custom.min.js" type="text/javascript"></script>
<link rel="stylesheet" href="css/smoothness/jquery-ui-1.8.5.custom.css" type="text/css" media="screen" charset="utf-8" />
<style type="text/css">
input[disabled]
{
background-color:#888888;
}
.Filled
{
width: 98%;
}
.Property
{ margin-bottom: 0px;
}
.Date
{
/* this class is only used to indicate that the value is a DateTime value */
}
.PropertyTable td
{
border-bottom: 1px solid #D8D8D8;
padding:1px;
background-color:#F6F6F6;
}
.Suppler_style
{
color: #0000BB;
}
</style>
<script type="text/javascript">
$(function () {
$(".Date").datepicker({ dateFormat: "yy/mm/dd" });
});
</script>
</head>
<body>
<form id="form1" runat="server">
<table style="width: 80%;" border="0" class="PropertyTable" cellspacing="0" cellpadding="0">
<tr>
<td style="width: 227px;">
Project Number</td>
<td>
<asp:TextBox ID="ProjectNumber" runat="server" CssClass="Property" Width="200px"></asp:TextBox>
<asp:Button ID="btApplyPnr" runat="server" Text=" apply "
onclick="btApplyPnr_Click" />
</td>
</tr>
<tr>
<td>
Contract No.</td>
<td>
<asp:DropDownList ID="ddContractNo" runat="server" AutoPostBack="True"
onselectedindexchanged="ContractNo_SelectedIndexChanged"
CssClass="Filled">
</asp:DropDownList>
<input type="hidden" class="Property" id="V_QA_PO_tp_listId" runat="server" />
<input type="hidden" class="Property" id="V_QA_PO_tp_ID" runat="server" />
<input type="hidden" class="Property" id="ContractNo" runat="server" />
</td>
</tr>
<tr>
<td>
ITP No</td>
<td>
<asp:TextBox ID="ITPNo" runat="server" CssClass="Filled Property"></asp:TextBox>
</td>
</tr>
<tr>
<td class="Suppler_style">
ITP Name</td>
<td>
<asp:TextBox ID="ITPName" runat="server" CssClass="Filled Property"></asp:TextBox>
</td>
</tr>
<tr>
<td class="Suppler_style">
Status Delivery/Finish Date</td>
<td>
<asp:TextBox ID="StatusDeliveryFinishDate" runat="server"
CssClass="Filled Property Date"></asp:TextBox>
</td>
</tr>
</table>
</form>
</body>
</html>
And the C#-Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace QAList
{
public partial class NewList : System.Web.UI.Page
{
private string current_contract = "0";
private string current_pnr;
protected void Page_Load(object sender, EventArgs e)
{
current_contract = ddContractNo.SelectedValue;
}
protected void Page_PreRender(object sender, EventArgs e)
{
//load Contract Numbers
ddContractNo.Items.Clear();
ddContractNo.Items.Add(new ListItem("-", "0"));
foreach (DataRow contract in DBAccess.Instance.getContractNumbers(current_pnr).Rows)
{
ddContractNo.Items.Add(new ListItem(contract["ProjectNumber"] + " - " + contract["ContractNo"] + " - " + contract["GoodsServicesSupplied"], contract["tp_listId"] + "_" + contract["tp_ID"]));
}
try
{
ddContractNo.SelectedValue = current_contract;
}
catch (ArgumentOutOfRangeException)
{
ddContractNo.SelectedValue = null;
applyNewContractValue();
}
}
protected void ContractNo_SelectedIndexChanged(object sender, EventArgs e)
{
applyNewContractValue();
}
private void applyNewContractValue()
{
current_contract = ddContractNo.SelectedValue;
if (!String.IsNullOrEmpty(current_contract) && current_contract != "0")
{
Guid tp_listId = new Guid(current_contract.Split('_')[0]);
int tp_ID = Convert.ToInt32(current_contract.Split('_')[1]);
DataRow row = DBAccess.Instance.getContractInfos(tp_listId, tp_ID);
if (row == null)
return;
ITPName.Text = row.IsNull("GoodsServicesSupplied") ? "" : row["GoodsServicesSupplied"].ToString();
if (!row.IsNull("PlannedDeliveryexworks"))
{
DateTime tmp = (DateTime)row["PlannedDeliveryexworks"];
StatusDeliveryFinishDate.Text = tmp.Year + "/" + fillZeros(tmp.Month, 2) + "/" + fillZeros(tmp.Day, 2);
}
}
else
{
ITPName.Text = "";
}
}
private string fillZeros(int nr, int min_length)
{
string res = nr.ToString();
while (res.Length < min_length)
res = "0" + res;
return res;
}
protected void btApplyPnr_Click(object sender, EventArgs e)
{
current_pnr = ProjectNumber.Text;
}
}
}
Check if your collegue got some script blocker installed, those can prevent postbacks since they are Javascript driven.
In case this helps someone:
I had something similar occur and it turned out that it was due to some validation controls on the page. They were for some hidden text boxes that were not actually visible and had no validation group specified. On one of the computers the errors were ignored and the postback worked, whereas on the second computer the postback failed due to validation errors. Because the text boxes were not currently visible, the problem was not immediately obvious.
Once I set up validation groups, the problem went away. I don't know why the behaviour was different on the two computers as both are running Chrome on Windows 10.

Resources