I am supposed to update the progress & display the corresponding message on the client side while the function is still under execution at server side.
How can I achieve this?
The function looks like:
protected void Button1_Click(object sender, EventArgs e)
{
string Result = "Success";
if (Result == "Success")
{
Label1.Text = "Plan mst Completed";
Thread.Sleep(2000); //Some functionality here
Label1.Text = "Packing date mst Started";
}
if (Result == "Success")
{
Label1.Text = "Packing date mst Completed";
Thread.Sleep(2000); //Some functionality here
Label1.Text = "Etd mst Started";
}
if (Result == "Success")
{
Label1.Text = "Etd mst Completed";
Thread.Sleep(2000); //Some functionality here
Label1.Text = "Inner box mst Started";
}
}
And I have to update the text of Label1 while the above function is still under execution.
I have tried using AJAX (although I'm still a beginner), but with no success. Here's what I did:
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="10" ontick="Timer1_Tick1">
</asp:Timer>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</form>
And the corresponding events:
protected void Page_Load(object sender, EventArgs e)
{
UpdatePanel1.UpdateMode = UpdatePanelUpdateMode.Conditional;
}
protected void Timer1_Tick1(object sender, EventArgs e)
{
UpdatePanel1.Update();
}
Any other alternatives other than AJAX such as via jQuery or otherwise are also welcome.
these might help you:
http://forums.asp.net/t/1500201.aspx
ASP.NET Asynchronous label update
Related
I am using this code from w3schools:
TryIt
How can i integrate it with asp:LinkButton or asp:Button using OnClientClick, while onClick is executing the server code.
Almost a year late to the party, but I've just implemented the same snackbar.
It needs an UpdatePanel and you need to register a client script block on your event handler:
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<div>
<asp:Button runat="server" ID="ShowSnackbar" Text="Show Snackbar" OnClick="ShowSnackbar_Click" />
</div>
<div id="snackbar">
<asp:Label runat="server" ID="Snack" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
The codebehind was tricky but it works. On page load, create a string that contains the javascript, then use that string value to register your script block...
private string snackbarScript;
protected void Page_Load(object sender, EventArgs e)
{
snackbarScript = GenerateSnackbarJS();
}
private string GenerateSnackbarJS()
{
var sb = new StringBuilder();
sb.AppendLine("var x = document.getElementById('snackbar');");
sb.AppendLine("x.className = 'show';");
sb.AppendLine("setTimeout(function(){ x.className = x.className.replace('show', ''); }, 3000);");
return sb.ToString();
}
protected void ShowSnackbar_Click(object sender, EventArgs e)
{
Snack.Text = "Here's the snackbar";
ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "snackbar", snackbarScript, true);
}
i want to run timer_Tick event on button click and stop timer pressing another button. this is my code
protected void Button1_Click(object sender, EventArgs e)
{
Timer1.Enabled = true;
}
protected void Timer1_Tick(object sender, EventArgs e)
{
Label2.Text = "Timer started";
//string png = IpAddress.Text.ToString(); //"192.168.153.12";//
if (IpAddress.Text == "" || IpAddress.Text == null)
{
Response.Write("Error");
}
else
{
ping1(IpAddress.Text);
ping2(Ipaddres1.Text);
}
}
Use update panel and write code Something like this.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click1" />
<asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="2000" Enabled="False">
</asp:Timer>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
And code behind,
protected void Button1_Click(object sender, EventArgs e)
{
Timer1.Enabled = true;
}
protected void Timer1_Tick(object sender, EventArgs e)
{
Label1.Text = "Timer started";
//string png = IpAddress.Text.ToString(); //"192.168.153.12";//
if (IpAddress.Text == "" || IpAddress.Text == null)
{
Label2.Text = "Error!!!";
}
else
{
ping1(IpAddress.Text);
ping2(Ipaddres1.Text);
}
}
when i use image control with updatepanel in asp.net, compiler gives an error : NullReference exception,
please any body help me. so what should be done to avoid such problem?
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div>
<asp:AsyncFileUpload ID="flduserphoto" runat="server"
OnClientUploadComplete="OnClientAsyncFileUploadComplete"
OnUploadedComplete="OnAsyncFileUploadComplete" Width="374px" />
<asp:Image runat="server" ID="imgPhoto" Width="150px" />
</div>
</ContentTemplate>
</UpdatePanel>
code file is,
public partial class Registration_frmUserRegistration : System.Web.UI.Page
{
DataTable dt;
#region FileUploadControl Section
protected void OnAsyncFileUploadComplete(object sender, AsyncFileUploadEventArgs e)
{
if (flduserphoto.FileBytes != null)
{
lblgender.Text = "asdf";
Context.Session.Add("SessionImage", flduserphoto.FileBytes);
}
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSave_Click(object sender, EventArgs e)
{
BALUserAddress objUserAddress = new BALUserAddress();
objUserAddress.UserType = ddlusertype.Text;
byte[]imageByte = new byte[flduserphoto.PostedFile.ContentLength];
objUserAddress.ProfilePicture=imageByte;
objUserAddress.ParentID = "0";
objUserAddress.RelationWith="Self";
objUserAddress.RegistrationDateTime= DateTime.Now;
string msg = objUserAddress.SaveUserDetails();
lblMsg.Text=msg;
mpMsg.Show();
}
}
this is my code file please check it
Try this one in form tag
<form id="form1" enctype="multipart/form-data" method="post" runat="server">
Does anybody show me an example of ext.net validation? I want to mix asp.net and ext.net validation. Or use ext.net validation only.
I've already saw these examples http://examples.ext.net/#/Form/Validation/Custom_VType/ and http://examples.ext.net/#/Form/FormPanel/Validation/ but it's not enough.
Also, I wonder why the code bottom doesn't work. It throws an exception
"Page.IsValid cannot be called before validation has taken place. It
should be queried in the event handler for a control that has
CausesValidation=True and initiated the postback, or after a call to
Page.Validate"
<script runat="server">
void Button_Click(object sender, EventArgs e) {
// Display whether the page passed validation.
if (Page.IsValid) {
Label1.Text = "Page is valid.";
}
else {
Label1.Text = "Page is not valid!";
}
}
void ServerValidation(object source, ServerValidateEventArgs args) {
try {
// Test whether the value entered into the text box is even.
int i = int.Parse(args.Value);
args.IsValid = ((i % 2) == 0);
}
catch (Exception ex) {
args.IsValid = false;
}
}
</script>
<ext:Label ID="Label1" runat="server" Text="Enter an even number:" />
<br />
<ext:TextField ID="TextField1" runat="server" />
<asp:CustomValidator runat="server" ControlToValidate="TextField1" OnServerValidate="ServerValidation"
ErrorMessage="Not an even number!" />
<ext:Button runat="server" Text="Validate" >
<DirectEvents>
<Click OnEvent="Button_Click" />
</DirectEvents>
</ext:Button>
How about this sample? And in pastebin: http://pastebin.com/hGCjnNqh
<script runat="server">
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
Page.Validate();
}
void ServerValidation(object source, ServerValidateEventArgs args)
{
try
{
// Test whether the value entered into the text box is even.
int i = int.Parse(args.Value);
args.IsValid = ((i % 2) == 0);
}
catch (Exception ex)
{
args.IsValid = false;
}
}
void Button_Click(object sender, EventArgs e) {
// Display whether the page passed validation.
if (Page.IsValid) {
Label1.Text = "Page is valid.";
} else {
Label1.Text = "Page is not valid!";
}
}
<ext:ResourceManager ID="ResourceManager1" runat="server" />
<ext:Label ID="Label1" runat="server" Text="Enter an even number:" />
<br/>
<ext:TextField ID="TextField1" runat="server" />
<asp:CustomValidator ID="CustomValidator1"
runat="server"
ControlToValidate="TextField1"
OnServerValidate="ServerValidation"
ErrorMessage="Not an even number!" />
<ext:Button ID="Button1" runat="server" Text="Validate" AutoPostBack="false" CausesValidation="true">
<DirectEvents>
<Click OnEvent="Button_Click" />
</DirectEvents>
</ext:Button>
Use ext.net component inside ext.net formpanel you don t need to use asp net validation.
I am developing an web application where i need a count down timer. Im using asp.net
asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="lblHour" Text="" runat="server"></asp:Label>
<asp:Label ID="lblMin" Text="" runat="server"></asp:Label>
<asp:Label ID="lblSec" Text="" runat="server"></asp:Label>
<asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="timer_Tick">
</asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
Code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["time"] = DateTime.Now.AddSeconds(40);
}
}
protected void timer_Tick(object sender, EventArgs e)
{
TimeSpan time1 = new TimeSpan();
time1 = (DateTime)Session["time"] - DateTime.Now;
if (time1.Seconds <= 0)
{
lblSec.Text = "TimeOut!";
}
else
{
lblSec.Text = time1.Seconds.ToString();
}
}
The problem i am having is, the timer wont decrement properly. It starts with 38, then goes to 35 then 32 and so on.
Is there a way to fix this problem?
I reckon the issue here is that when timer is triggered, the time it executes the code, it is little longer then one sec, 1 sec + some micro seconds, this is the cause of this output. Test this in your code.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["time"] = DateTime.Now.AddSeconds(40);
}
}
protected void timer_Tick(object sender, EventArgs e)
{
var endTime = (DateTime) Session["time"];
var endMin = ((DateTime)Session["time"]).Minute;
var endSec = ((DateTime)Session["time"]).Second;
var endMsec = ((DateTime)Session["time"]).Millisecond;
var currentTime = DateTime.Now;
var currentMin = currentTime.Minute;
var currentSec = currentTime.Second;
var currentMsec = currentTime.Millisecond;
var time1 = endTime - currentTime;
lblHour.Text = string.Format("End Sec - {0}:{1}:{2}", endMin, endSec, endMsec);
lblMin.Text = string.Format("Current Sec - {0}:{1}:{2}", currentMin, currentSec, currentMsec);
lblSec.Text = time1.Seconds <= 0 ? "TimeOut!" : time1.Seconds.ToString();
}