Asp.net get variable from C# code - c#

I want use variables from inherits in asp code
My code looks like that
namespace WebApplication2
{
public class Global : System.Web.HttpApplication
{
public bool abcdef = true; // my variable that i want to use in my asp code
...
}
}
ASP part looks like below:
<!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">
<%
if(webapplication1.main.abcdef) //this is incorrect, i want to correct that
{%>
my first web page
<%}
else
{%>
my second web page
<%} %>
</html>
How can I use my variable abcdef in my asp code?

<%# Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" MasterPageFile="MasterPage.Master"
Inherits="Namespace.YouClass" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script>
if(<%#this.abcdef%>)
{
//do your work
}
</script>

Related

Trying to retrieve form response in C# .aspx web form

New to C#, ASP.net from PHP and trying to convert my code.
I currently am trying the following code to retrieve the posted data from a form.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="CarPage.aspx.cs" Inherits="Ass2.CarPage"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<%
If Request.Form["Car"] == "Volvo" then
header('Location:VolvoHomepage.html');End If
If Request.Form["Car"] == "Ford" then
header('Location:FordHomepage.html');End If
If Request.Form["Car"] == "Mercedes" then
header('Location:MercedesHomepage.html');End If
If Request.Form["Car"] == "Audi" then
header('Location:AudiHomepage.html');End If
If Request.Form["Car"] == "Vauxhall" then
header('Location:VauxhallHomepage.html');End If
%>
</body>
</html>
but I keep receiving "Server Error in '/' Application."
Could anyone help please?
This line is saying that you are coding in C# language:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="CarPage.aspx.cs" Inherits="Ass2.CarPage"%>
But below you are using a mix of Visual Basic and PHP languages:
<% If Request.Form["Car"] == "Volvo" then
header('Location:VolvoHomepage.html');End If
%>
In C# the code above would be:
<% if (Request.Form["Car"] == "Volvo") {
// do your thing
}
%>
However in Web Forms framework you should declare "user controls" on the aspx file and code the "logic" into the aspx.cs file. Your aspx code may look like:
<myUserControls:VolvoHomepage runat="server" id="_ucVolvo" visible="false" />
<myUserControls:FordHomepage runat="server" id="_ucFord" visible="false" />
...
You will copy/paste each code of your html files in the corresponding user control.
And now the Page_Load method (for example) of CarPage.aspx.cs may define if the user controls are visible or not:
protected void Page_Load(object sender, eventargs e) {
if (Request.Form["Car"] == "Volvo") _ucVolvo.Visible = true;
else if (Request.Form["Car"] == "Ford") _ucFord.Visible = true;
}
Is it more clear?

Proper use of classes in web development

I used to write winforms and now I am trying to port some of my apps to web interface, but my apps seems to be not working, althought the code in my class did not change.
I have never done web development, so I am wondering if this is something what I am not doing right?
I use this code in my runEbay.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="runEbay.aspx.cs" Inherits="eBay_Item_Scanner.runEbay" %>
<div>
Home | <a href="runEbay.aspx">Run Full eBay Scan WARNING!!!!!!!
</a>
</div>
<%
string MAINPN = Request["MAINPN"] != null ? Request["MAINPN"] : "";
string EBAYCATEGORYTOSCAN = Request["EBAYCATEGORYTOSCAN"] != null ? Request["EBAYCATEGORYTOSCAN"] : "";
Response.Write("<br>MAINPN: " + MAINPN);
Response.Write("<br>EBAYCATEGORYTOSCAN: " + EBAYCATEGORYTOSCAN);
//create instance of class Program
eBay_Item_Scanner.Program eb = new eBay_Item_Scanner.Program();
if (SUBPN.Length > 0)
{
String query = #"my query using some request parameters";
//call functions from another class
eb.getEbay_com_API(query, EBAYCATEGORYTOSCAN);
}
Response.Write("<br><br>Scanning is done");
%>
<!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>
</div>
</form>
</body>
</html>
OK; the problem was that my functions were too big and they timed out the IIS. I removed the timeout and that solved the problem.

string array from c# to javascript example not working

i'm using asp.net web form fx3.5 and i'm trying to get my server-side string array into my javascript. i found a simple example that claims to work but it doesn't for me. temp variable is not recognized in ().Serialize(temp);
Here's the reference article
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ShelterExpress.UserInterface.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server" language="c#">
string[] temp;
int lengthOfTemp;
public string tempJSON = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(temp);
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
<script runat="server" language="c#">
string[] temp;
int lengthOfTemp;
public string tempJSON;
protected override void OnLoad(EventArgs e)//you have to initialize your temp and tempJSON in a method
{
base.OnLoad(e);
temp = new string[] { "Hi", ",", "ojlovecd" };//Initialize your temp here
tempJSON = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(temp);
}
</script>

How to trigger onclick event after received return value from popup which using add attribute a javascript?

How to trigger onclick event after received return value from popup aspx which using add attribute a javascript?
After adding
.Attributes.Add("onClick", "return popWin('" + NewBatchNo_TextBox.Text + "');");
Original onclick event do not fire?
if this method can not work, any other method to get value from pop up message box and return value and run click event
Thanks.
try this
Main.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Main.aspx.cs" Inherits="Main" %>
<!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 DoStuff() {
document.getElementById('Button1').click();
}
function popWin() {
var popy = window.open('popup.aspx', 'popup_form', 'menubar=no,status=no,top=100%,left=100;')
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtPopupValue" runat="server" Width="327px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Show List" />
</div>
</form>
</body>
</html>
Main.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 Main : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.Button1.Attributes.Add("onclick", "return popWin()");
}
}
}
popup.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="popup.aspx.cs" Inherits="popup" %>
<!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 validepopupform() {
window.opener.document.getElementById('txtPopupValue').value = document.getElementById('txtPop').value
self.close();
}
window.onbeforeunload = CloseEvent;
function CloseEvent() {
if (window.opener && !window.opener.closed) {
window.opener.DoStuff();
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtPop" runat="server"></asp:TextBox>
<input type='button' value='go' onclick='validepopupform()' />
</div>
</form>
</body>
</html>
The same problem I faced several times the cause is that java script cannot call code behind(c#/vb function/events).
An alternate way I used is to use hidden fields that is accessible by both java script and code behind. But then you want to call events not read values modified by java script.
For this we refreshed the page through java script when we want to trigger that code behind event and let the page load to monitor the scenario(value that java script set) and triggers the required event.
The code at the end would be real mess and unmanageable, would take lots of efforts to debug.

javascript is not working in mozilla but working in other browers

Technology Used:- Asp.Net 2.0
Code:- See Below
Description:- hello code given below is working fine in i.e. and other but not working in all mozila version.javascript is simple to devide two textbox's value. you can easily understand.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="javascript_test.aspx.cs" Inherits="javascript_test" %>
<!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" >
<script type ="text/jscript">
var _txtamount;
var _txtins;
var _txtinsamount;
function test()
{
var temp;
_txtamount = document.getElementById("txtamount");
_txtins = document.getElementById("txtins");
_txtinsamount = document.getElementById("txtinsamount");
if (_txtinsamount.value !='')
{
temp = parseFloat(_txtamount.value) / parseFloat(_txtinsamount.value);
}
else
{
temp = 0
}
_txtins.value = temp;
}
</script>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="myform" runat="server" name="myform">
<div>
<asp:TextBox ID="txtamount" runat="server" ></asp:TextBox>
<asp:TextBox ID="txtinsamount" runat="server" onblur="test();"></asp:TextBox>
<asp:TextBox ID="txtins" runat="server"></asp:TextBox></div>
</form>
</body>
</html>
You're using text/jscript as the <script> type. Use text/javascript instead:
<script type ="text/javascript">
JScript is Microsoft's own version of ECMAScript -- no wonder it works on IE.
Your <script> tag needs to be wrapped in the <head> element or in the <body> element; it can't just be a direct child of <html>.
[edit]more important is your "type" value, as the other answer here mentions.

Categories