Access HTML page in C# - c#

Basically I have two files:
details.html
details.cs
I would like to use details.cs to write values to details.html but the html textbox still stays the same.
details.html
<%# Page Language="C#"
AutoEventWireup="true"
CodeBehind="details.cs"
Inherits="Details.DetailsHTML" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form>
<input id="txt_details" type="text" name="txt_details" runat="server"/>
</form>
</body>
</html>
details.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Data;
namespace Details
{
public partial class DetailsHTML : Page
{
//Declare controls used
protected System.Web.UI.HtmlControls.HtmlInputText txt_details;
protected void Page_Load(object sender, EventArgs e)
{
string strValue = Page.Request.Form ["txt_details"].ToString();
strValue = "test";
}
}
}

You can not access control in .html from .cs file. You are trying to use aspx page as you are inherting from Page class but naming .html, you are problably not using visual studio. Use details.aspx and details.aspx.cs. If you do not have Visual Studio then you can download free express version from here. This link explains how to create web application project using visual studio. The article Creating a Basic Web Page with Code Separation in Visual Studio will help you in creating the web page and access html control on server side.
txt_details.Value = "your value";

txt_details.Value = "test";
This should help

You can acess by a name of control eg:
txt_details.Value = "Test"

Related

How do you reference HTML/ASP elements with C# objects?

I'm new to ASP.NET and C#, I've primarily worked with Java. I want to dynamically add nodes to a tree view. I've followed a few tutorials but whenever I implement them they do not appear to work. I keep getting an error: "The name 'MyTreeView' does not exist in the current context".
Here's the C# code:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace LocalTest
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
MyTreeView.Nodes.Add(new TreeNode("Node1"));
MyTreeView.Nodes[0].ChildNodes.Add(new TreeNode("ChildNode"));
}
}
}
}
And the HTML/ASP:
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="MyTreeView" Runat="server">
</asp:TreeView>
</div>
</form>
</body>
</html>
"MyTreeView" is the ID of the treeview contained in the Default.aspx file.
The few tutorials I have followed seem to access the treeview directly with its ID as seen above which makes very little sense to me. For instance, if I were to do this in Android I would have to use the findViewById and establish a link between the XML and the Object.
Does anyone have any suggestions? I've bumbled around this site for a bit and I'veonly found similar issues but I cannot get a clear answer.
Thank you.
Looking at the code above, you are using what's known as ASP.Net WebForms where there are "server side" controls (e.g. <asp:TreeView ID="MyTreeView" Runat="server">)
The "page" (html aspx) needs to somehow "wire itself" to the code (c# file). It will need a "server-side" directive like so:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebFormsApplication.WebForm1" %>
<!DOCTYPE html>
//...the rest of the html goes here plus web controls, etc.
At which point, you can see stuff like Inherits="WebFormsApplication.WebForm1" that maps to the Class:
namespace WebFormsApplication
{
public partial class WebForm1 : System.Web.UI.Page
....
found in the WebForm1.aspx.cs file, also in the directive CodeBehind="WebForm1.aspx.cs"
So voila :)
establish a link between the XML and the Object.
Hth...
You do not have TreeView control in default.aspx. It is in .html file and default.aspx.cs can not see TreeView control.
It seems you are missing Page directive in your asp.net page
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

Pre-set formatting on asp.net pages.

I am newish to Asp.net. I have inherited a web application project. When I create a new page it already has some formatting on the top even though I don't use any master page when creating the page. This I can see when I build the page. I am using ASP.Net 4.0, C# language.
For the life of me I cant figure out as in the underlying code for the file there is no reference to any css or any elements. The code for page.aspx is as follows:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="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">
<head runat="server">
<title></title>
</head>
<body>
</body>
</html>
The code behind is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
Thanks in advance for any help I can get.
Oh Actually when you create a page in Visual studio. He create some by default code file and a project structure. Just why you show different type of file on solutions.
See on your aspx page header its define cs code file as CodeFile="Test.aspx.cs" and also Inherits test.
Also .csproj file that contains assembly references
.projRefs.sln file that contains the current project and all referenced projects
I figured it out. There was a theme applied which was adding reference to a stylesheet when compiling the page. I added disable theming tag which removed this reference for the particular page.

How does a Client call a method in a server?

I am just learning how to use TCP client server relationships. I have been looking for hours and hours but I do not see any way that a Client can get input from the user and then the client sent a byte[] array to the server and have it do anything but compare strings, is there some way that I can have it call a method with this input? or am I missing something?
I have heard of something called serialization, though there seem to be several ways to use it, can someone point me in the right direction if this is correct?
Thanks for any reply
For a basic .NET website, I like to use the page methods for a quick easy way to make client to server calls. Consider the following aspx page and its code behind. You can expose public static server methods to javascript by adding the [WebMethod] attribute. Then you can call the function from javascript using PageMethods.NameOfFunction(). Just make sure you have a scriptmanager on the aspx page with the EnablePageMethods property set to "true".
Default.aspx
<%# 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 runat="server">
<title></title>
<script type="text/javascript">
function init() {
var str = "bytes";
var data = [];
for (var i = 0; i < str.length; ++i)
{
data.push(str.charCodeAt(i));
data.push(0);
}
PageMethods.DoSomething(data);
}
</script>
</head>
<body onload="init();">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptMan" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
</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;
using System.Xml.Linq;
using System.Web.Services;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static void DoSomething(byte[] data)
{
//Do something with data from javascript
}
}

how can i create a dynamic page in asp.net (C#)?

I added a page "First.aspx" in my website application. Inside First.aspx page i have a button named btnbutton.
"onclick" Event of "btnbutton" a new dynamic page should open.
how can i do this.?
Please Remember the new dynamic page created, is not in existing in the application. This page should be created at runtime and dynamic also.
please help me out!
If you are asking about generating ASP.NET pages at runtime, that is impossible. The reason is the following:
You need to compile the code of the
ASP.NET page before run it. And that
is impossible after your web
application has started.
However, if you are asking about navigation between pages, then, you could use Response.Redirect:
Response.Redirect("http://www.stackoverflow.com/");
You can create new Dynamic File in ASP.net website (Not in a ASP.net Web Application).
But there is a problem . Once you created a file the whole Website will be restarted for compiling the newly created file. So you Session data will be lost.
Here is the code to create new file.
string fielName = Server.MapPath("~/file.aspx");
//File.Create(fielName);
//File.AppendText(fielName);
// create a writer and open the file
TextWriter tw = new StreamWriter(fielName);
// write a line of text to the file
tw.WriteLine(#"<%# Page Language=""C#"" AutoEventWireup=""true"" CodeFile=""file.aspx.cs"" Inherits=""file"" %>
<!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>
");
// close the stream
tw.Close();
tw = new StreamWriter(fielName + ".cs");
// write a line of text to the file
tw.WriteLine(#"using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class file : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(""new File "");
}
}
");
// close the stream
tw.Close();

Updating Label Text while Method is Running

Developing is more of a hobby at the moment; so apologies if this is a basic question, although after a couple of hours searching the web I can't seem to find an answer. I am currenttly building a support tool for my team which runs a number of SQL queries & BCP processes and returns the results. Some of these actions take quite a while and I would like to update the web UI with status messages. Previously I have been using the Label1.Text = text;
However now I would like to update the Label1.text multiple times while my method is running. Try as I might though it only displays the last update in the method.
I have coded a small test to try get to the bottom of this: -
Web Page
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Template._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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="progress" runat="server" Text=""></asp:Label><br />
<asp:Image ID="Image1" runat="server" ImageUrl="~/Image/loader.gif"
Visible="False" />
</div>
</form>
</body>
</html>
C# CodeBehind
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace Template
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
uiUpdate("OUT", 1);
wait();
uiUpdate("In", 0);
}
protected void uiUpdate(string text, Int32 image)
{
if (image == 1)
{
Image1.Visible = true;
}
else
{
Image1.Visible = false;
}
progress.Text = text;
}
protected void wait()
{
DateTime wait;
wait = DateTime.Now + TimeSpan.FromSeconds(10);
while (wait > DateTime.Now)
{
}
}
Is there a way I can get the Label1 & Image to update as the Page_load is running?
Or am I looking at this the totally wrong way (no doubt!)
Thanks
HTML as a full page request is not in the least bit granular:
your browser sends a request (of all the form variables, cookies, etc etc) to the server
the server does a bit of processing and sends a response (typically html)
your browser processes the response and renders the display
All your updates are happening in the middle bullet, so no: there is no way to make the browser update: your code is executing on the server, with no dialog with the client
If you need this type of behaviour, you would have to look at AJAX tools - at each step doing "just enough" work to get to the next milestone and update the caption... with regular ASP.NET UpdatePanel may be of interest; for MVC or other (non-ASP.NET) setups, jQuery would be your friend. Unfortunately, this isn't a trivial topic.
If I understand your question correct you can't do this in regular asp.net, you need to use ajax, and make an ajax method in which you then update the text of the label on runtime. You can use the ajax MS scriptmanager to do this.
If it is not for high performance you can use an update panel otherwise you should just use the scriptmanager by yourself.

Categories