Having trouble with simple event handler, c#, asp.net - c#

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.

Related

Error: ASP.NET Ajax client-side framework failed to load after some updates on host

I have a web site in windows host = https://blobloblo/WebForm5.aspx
When run that link on FireFox developer & take a look at Console logs,
I will see this error :
Error: ASP.NET Ajax client-side framework failed to
Because of this error many parts of this web site do n't work correctly.
Yesterdays every thing was ok.
But today they closed the host for some hours & did some updates such as plesk's update.
After running the host again this error appeared.
What should i tell them to fix this problem?
WebForm5 ASPX :
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm5.aspx.cs" Inherits="Virtual_Visa_Cards.WebForm5" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" Height="26px" OnClick="Button1_Click" /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
WebForm5 C# :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Virtual_Visa_Cards
{
public partial class WebForm5 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "This is Ajax Call = CallBack";
}
}
}
Related Topic :
ASP.NET Ajax client-side framework failed to load. when put the ScriptManager on a blank page
I test all answers in the topic upper WITH NO RESULT.
In plesk i went to Web Application Firewall & put Web application firewall mode to off.
I worked one day to solve this issue.

button event not working after some idle time

i have a simple only contains a button witch have a behind code redirect to another page
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default1.aspx.cs" Inherits="Default1" %>
<!DOCTYPE html>
<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" OnClick="Button1_Click" />
</form>
</body>
</html>
and the behind code is
using System;
using System.Web;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
public partial class Default1 : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("~/Default2.aspx");
}
}
but when i open page and wait about 15 minuets and click on the button the event is not called only it seem like refresh the page
Your session is timing out.
The default session timeout value in ASP.NET is 20 Minutes.
The session timeout value can be increased in any one of the following ways:
IIS 7:
Select the website from the list of sites
Click on Session state on the right
Enter the session timeout under the cookie settings
Add the below line to the Web.config file, under the system.web section (You can change 20 to whatever value you want):
<sessionState timeout = "20" mode = "InProc" />
Add the below line to the Global.asax file, under the Session_Start method (You can change 20 to whatever value you want.:
Session.Timeout = "20";

Label false property cannot be changed programatically by button click

Having looked around at pages such as this, I cannot see why a label text will not display in a simple test page when a submit button is clicked. As the file is short, I have included all coding, in case there is anything I have not thought of in the background.
The text does not display when the button is pressed in a render generated with Ctrl+F5 in Visual Studio Express 2015. Where have I gone wrong?
Code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="labelTest2.aspx.cs"
Inherits="contact_labelTest2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Label Test</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Send"/>
<asp:Label ID="lblMessage" runat="server" Visible="false">
Test M AFTERessage</asp:Label>
</div>
</form>
</body>
</html>
Code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class contact_labelTest2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
lblMessage.Visible = true;
}
}
You should also add OnClick event to the Button. Like this:
<asp:Button ID="Button1" runat="server" Text="Send" OnClick="Button1_Click"/>

Request.Form is empty (asp.net c#)

I've been doing some really clever stuff (I think) in ASP.Net c#, so much so that the simple stuff is more difficult (if that makes sense)
I have this snippet of code in my page
<form id="form1" runat="server">
<asp:HiddenField runat="server" ID="hdnConfirm" value="Hello World" />
<asp:LinkButton runat="server" PostBackUrl="/confirm.aspx" Text="Confirm">
</asp:LinkButton>
</form>
I have this snippet of code in confirm.aspx.
if !(IsPostback)
{
lblConfirm.Text = Request.Form["hdnConfirm"]
}
I was expecting this to be nice and simple but when i click the button and go to page "confirm.aspx" the Request.Form has no values. What have I missed ?
[TESTING]
I ran a test on a brand new web forms project in VS2013. Dot.Net 4.5.1 This does not work. PreviouPage is always null. Whether surrounded by (!IsPostBack) or not. Doesn't matter if the submitting control is a Button, LinkButton or Hyperlink. Request.Form["hdn"] is also null. I have restarted my computer just in case and still no joy. I am missing something really simple I am sure of it but I can't see what
This is the first page nothing in the code behind
<%# 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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinkButton runat="server" PostBackUrl="~/WebForm2.aspx">click</asp:LinkButton>
<asp:HiddenField runat="server" ID="hdn" Value="3" />
</div>
</form>
</body>
</html>
This is the second page
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication2.WebForm2" %>
<!DOCTYPE html>
<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>
Code behind
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 WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string s = ((HiddenField)this.PreviousPage.FindControl("hdn")).Value;
}
}
}
On confirm.aspx use PreviousPage.FindControl instead :
HiddenField hdnFieldName = this.PreviousPage.FindControl("hdnConfirm") as HiddenField;
string hiddenValue = string.Empty;
if (hdnFieldName != null)
{
hiddenValue = hdnFieldName.Value;
}
Here is good example to get you started.
What's going on Here:
By default with VS 2013 and Asp.Net v4.5.1 , there is a feature FriendlyUrls which is enabled.
The FriendlyUrls feature does kind of Asp.Net routing , thus changing URLs from localhost/webform1.aspx to localhost/webfrom1
PostbackUrl property will not work in combination with Asp.Net Routing. When ASP.NET routing is in use the PreviousPage URL is the final routed URL. The runtime will check for a file represented by the Routed URL now, which will be webform1 and NOT webfrom1.aspx. Since there is NO such file as webform1, so it will always set the PreviousPage to null.
Possible Solutions:
So, now you know the issue at hand.
1.) Either don't use the Routing system of Asp.Net Friendly Urls , in this case, therefore try adding the <%# PreviousPageType VirtualPath="~/WebForm1.aspx"%> to webform2.aspx page and check.
2.) OR if you keep the FriendlyURLs and hence the Routing system, make changes in your code to read the Form values using some other alternatives.

Update an asp:Label while a class is processing

I have the following aspx page:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="index" %>
<!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>
<asp:Label ID="lblStatus" runat="server" Text="Label">
</asp:Label><asp:Button ID="btnRun" runat="server" Text="Button" onclick="btnRun_Click" />
</div>
</form>
</body>
</html>
The codebehind is:
public partial class index : System.Web.UI.Page
{
protected void btnRun_Click(object sender, EventArgs e)
{
CP CPObj = new CP();
lblStatus.Text = "Starting process..";
CPObj.run();
lblStatus.Text = "Process done.";
}
}
The class needs to do some work (about a minute).
When I click the button, the label does not change and only after the class completes the job the label is changed to "Process done.".
I've looked for a solution for a really long time but could not find a clear and straight forward answer.
I've tried to run the class as a thread and got the same result.
public partial class index : System.Web.UI.Page
{
protected void btnRun_Click(object sender, EventArgs e)
{
CP CPObj = new CP();
Thread oThread = new Thread(new ThreadStart(CPObj.run));
lblStatus.Text = "Starting process..";
oThread.Start();
while (!oThread.IsAlive) ;
while (oThread.IsAlive)
{
Thread.Sleep(1000);
}
lblStatus.Text = "Process done.";
}
}
Using JavaScript seems problematic since I already have an onClick call (not sure if I'm correct here).
I'd really appreciate a response.
Thanks!
Nadav
Web pages aren't like traditional web forms.
You can't set a label to some value and then within the same callback change that value to something else and have both appear.
What you need to do is use some ajax. The simple way would be to add an update panel and go that route. Basically, you would set the label via javascript to "starting process..." then let the panel change the label once the callback is complete.
Alternatively, you could do it without ajax; but again you set the label via some javascript first. Then let your call back routine over write it.
I think you need to read about page life cycle. Is nice you are updating your label, but since this is web, your data will no go back to the browser until all the code run. So you can't update a label that doesn't exist yet on a page you are not yet sending to the browser ;)
I think JS is the solution for you (not an expert on that field) or maybe some ajax that will query the process runtime, and the process running on another thread?

Categories