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.
Related
Here is a problem that has been driving me nuts for a couple of months. It seems to be very intricate, so I made a very simple example to demonstrate it.
I have a website using ASP .Net and hosted on a microsoft server, using IIS and a SQL Server database. The website is working fine when debugged on a local server, or when accessed from inside the server.
BUT, whenever I'm using the website from client-side (= accessing it from my computer when it is deployed on my remote server), the pages of the website seems to be loading twice every time. Meaning :
At first loading, the Page_Load event is fired twice.
Every time I'm triggering a runat="server" control, the Page_Load event AND the associated server event are triggering twice.
So, I made a little webpage, hosted on the same website but not related to any of its other pages (not using the Site.Master either). Here it is :
Client-side code :
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="TestPostback.aspx.cs" Inherits="Rosalie.TestPostback" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button runat="server" ID="btnTest" OnClick="btnTest_Click" Text="Test" />
</div>
</form>
</body>
</html>
Server-side code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace Rosalie
{
public partial class TestPostback : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Directory.Exists(Server.MapPath("Logs")))
Directory.CreateDirectory(Server.MapPath("Logs"));
StreamWriter sw = new StreamWriter(Server.MapPath("Logs/Trace.txt"), true);
sw.WriteLine(DateTime.Now + " " + getPostBackControlName() + " PageLoad\n");
sw.Flush();
sw.Close();
}
protected void btnTest_Click(object sender, EventArgs e)
{
if (!Directory.Exists(Server.MapPath("Logs")))
Directory.CreateDirectory(Server.MapPath("Logs"));
StreamWriter sw = new StreamWriter(Server.MapPath("Logs/Trace.txt"), true);
sw.WriteLine(DateTime.Now + " " + getPostBackControlName() + " ButtonEvent\n");
sw.Flush();
sw.Close();
}
// Method returning the ID of the control triggering the postback
private string getPostBackControlName()
{
...
}
}
As you can see, the goal is to write in a file everytime a server event method is called and so have a trace of the server behaviour.
Now, here is the result file when I run this page on my local server, or when I access it from inside the release server, and I do one single click on the test button :
02/10/2018 15:05:44 PageLoad
02/10/2018 15:05:46 btnTest PageLoad
02/10/2018 15:05:46 btnTest ButtonEvent
You can see that the page was loaded, then a postback was triggered by my click on the button and triggered the associated event. This is a perfectly fine and normal behaviour for this webpage.
Now, here is the result file when executing the exact same steps but from a remote client computer :
02/10/2018 15:36:19 PageLoad
02/10/2018 15:36:20 PageLoad
02/10/2018 15:36:28 btnTest PageLoad
02/10/2018 15:36:28 btnTest ButtonEvent
02/10/2018 15:36:28 btnTest PageLoad
02/10/2018 15:36:28 btnTest ButtonEvent
As you can see, the Page_Load and btnTest_Clicked method are fired twice in a row, as if the webpage was loaded twice every time the server was called.
Given the simplicity of the code, I really think this has something to do with the website configuration or the server environment, but I'm not really an expert in such things.
I searched a solution on the Internet multiple times, spending hours trying out stuff related to pure programmation, but nothing seems to work out. I'm desperate for solutions. Thank you !
first let me say that even though I am 60 years of age I am relatively new to c# and definitely new to asp.net.
I was doing a simple demo that I copied from the web, where I have a very minimal web form that only has a text box and a button. The code compiles and when run the web form it comes up and shows the proper button and text box. However, when I click the button, I should get an event to happen which does not happen. In fact, when I am debugging, I see that the program never executes to the event that increments the button click count. Of course, I am clicking the button during debug mode and I never see the click increment function executed.
Evidently the event of the button click is not being executed.
I have included a picture of the simple web form (hopefully, it will come through)
Thank You
Tom
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ViewStateDemo
{
public partial class WebForm1 : System.Web.UI.Page
{
int ClicksCount = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Text = "0";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
ClicksCount = ClicksCount + 1;
TextBox1.Text = ClicksCount.ToString();
}
}
}
Simple Web Form
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ViewStateDemo.WebForm1" %>
<!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">
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form>
</body>
</html>
You didn't wire up your event. You have to tell the button what method to call when it's clicked.
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
By the way, you're learning ASP.NET Web Forms. Web Forms future is unclear, and bleak, and it's considered by many professionals to not be that great. If you're just starting out, you should learn ASP.NET MVC.
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
}
}
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"
I am deploying a site built as a set of projects in a solution which is deployed as a series of websites (one project = one url).
The coding lanague is c# and the site uses a mssql db accessed via linq.
Everything works ok on the development server but at the moment, all pages with dynamically created elements (i.e all <asp> and custom tags) output a blank page with the following source code. Static pages (standard html and aspx) display correctly.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=windows-1252" http-equiv=Content-Type></HEAD>
<BODY></BODY></HTML>
I normally work with php on LAMP and this behaviour is unlike anything I've seen in that setup. I need some fresh ideas on causes / resolution of this error (preferably step by step / tutorial links - like I say IIS/c#.net is not my normal enviroment).
Things I have tried with no effect whatsoever:
altering permissions on site folders
altering web.config settings
Redeploying site files via copy / paste and the vb publish option with various settings
Plus a whole ton (over two days so far) of internet research
Thanks for all replies.
edit:
version numbers:
iis: v6.1 (bulid: 7601)
OS: Windows web server 2008 R2 / Service pack 1
.net: v4
second edit:
Sample page:
aspx file:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="dbtest.aspx.cs" Inherits="Rica.Yoodul.dbtest" %>
<!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>test</h1>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</div>
</form>
</body>
</html>
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.Data.SqlClient;
namespace Rica.Yoodul
{
public partial class dbtest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["RicaConnectionString"].ConnectionString);
myConnection.Open();
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand("SELECT * FROM dbo.MaritialStatus", myConnection);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
Literal1.Text = Literal1.Text + myReader["Name"].ToString();
}
myConnection.Close();
}
}
}
edit:
Something I didn't add: Based on some of the other behaviour the server is displaying, I think the most likely culprit is misconfiguration of the server.
edit:
OK I've narrowed down the cause a little. By taking out everything except the langauge attribute in the first line of the front end file I can get the server to display the static content.#
edit:
OK http://www.iis.net/ConfigReference covers the use of the iis config files. Only I went to the path indicated on the page and I don't appear to have any config files at all. How do I comfirm / fix this?
It sounds like your production server doesn't have ASP.NET correctly installed/registered, so your pages are being treated as simple text documents rather than code. This can sometimes happen if you install .NET before installing IIS, for example.
To confirm this, put together a barebones page that includes a mix of HTML and ASP.NET...
<%# Page Language="C#" %>
<html>
<head>
<title>ASP.NET Hello World</title>
</head>
<body>
<p>Hello from HTML</p>
<p><%= "Hello from ASP.NET" %></p>
</body>
</html>
If you load that up and only get the plain-HTML greeting, use your browser's "View Source" option and see what you're actually getting. If your ASP.NET source-code is shown in the source the browser's receiving, that confirms that IIS didn't treat it as an ASP.NET file - which means ASP.NET isn't installed correctly or is disabled.