Monday, March 26, 2012

Ajax Auto Postback I think needed

Alright I am attempting to make a site to do ping tests and I would like to make it look as realistic as the command prompt as possible. That is as far as having one result returned at a time.

currently it returns all of the pings responses all at once instead of one at a time. you can check out the code in working order at http://tools.shotdrive.com

here is my code behind page code

protected void DNSCheck_btn_Click(object sender, EventArgs e)
{
Timer1.Enabled = true;
}

protected void pingfunction()
{
try
{
//
switch (PingStatus)
{
case "Success":

lblStatus.Text += "Reply from: " + Convert.ToString(pingreply.Address) + " \r";
lblStatus.Text += "Bytes: " + Convert.ToString(pingreply.Buffer.Length.ToString()) + " \r ";
lblStatus.Text += "Time: " + Convert.ToString(pingreply.RoundtripTime) + " \r";
lblStatus.Text += "TTL: " + Convert.ToString(pingreply.Options.Ttl) + " \r <br>";

//ResponseText += "Reply from: " + Convert.ToString(pingreply.Address) + " \r";
//ResponseText += "Bytes: " + Convert.ToString(pingreply.Buffer.Length.ToString()) + " \r ";
//ResponseText += "Time: " + Convert.ToString(pingreply.RoundtripTime) + " \r";
//ResponseText += "TTL: " + Convert.ToString(pingreply.Options.Ttl) + " \r <br>";
break;

case "TimedOut":
lblStatus.Text += "Request timed out.<br>";

//lblStatus.DataBind();
break;

case "DestinationHostUnreachable":
lblStatus.Text += "Destination Host Unreachable<br>";
//lblStatus.DataBind();
break;

case "DestinationUnreachable":
lblStatus.Text += "Ping request could not find host r.red.com. Please check the name and try again.<br>";
//lblStatus.DataBind();
break;
case "BadDestination":
lblStatus.Text += "Ping request could not find host r.red.com. Please check the name and try again.<br>";
//lblStatus.DataBind();
break;

case "BadRoute":
lblStatus.Text += "Ping request could not find host " + txtHost.Text.Trim() + " Please check the name and try again <br>";
//lblStatus.DataBind();
break;

default:
lblStatus.Text = "SOME Error";
break;

}

}
catch (Exception err)
{

lblStatus.Text += "Ping request could not find host " + txtHost.Text.Trim() + " Please check the name and try again <br>";

}
}
protected void Timer1_Tick(object sender, EventArgs e)
{

lblStatus.Text = null;

for (int q = 0; q < Convert.ToInt32(tb_PingCount.Text); q++)
{
String host = txtHost.Text.Trim();
pingreply = ping.Send(host);
PingStatus = pingreply.Status.ToString();
pingfunction();

}
}

and my aspx page code

<%@dotnet.itags.org. Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %
<%@dotnet.itags.org. Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.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:ScriptManager ID="ScriptManager1" runat="server" />

<div style="left: 134px; width: 895px; position: relative; top: 0px; height: 502px">

<asp:Button ID="DNSCheck_btn" runat="server" OnClick="DNSCheck_btn_Click" Style="left: 313px;
position: relative; top: -1px" Text="Submit" /><asp:TextBox ID="txtHost" runat="server" Style="left: -74px;
position: relative; top: -3px"></asp:TextBox><asp:RegularExpressionValidator
ID="RegularExpressionValidator1" runat="server" ControlToValidate="tb_PingCount"
ErrorMessage="Invalid Input" Style="left: 16px; position: relative; top: -4px"
ValidationExpression="^(.?\d*)$"></asp:RegularExpressionValidator><asp:TextBox ID="tb_PingCount" runat="server"
Style="left: -131px; position: relative; top: -2px" Width="39px">1</asp:TextBox><br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" RenderMode="Inline">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Enabled="False" Interval="5000" OnTick="Timer1_Tick">
</asp:Timer>

<div style="left: 0px; width: 417px; position: relative; top: 0px; height: 42px">
</div>

<asp:Label ID="lblStatus" runat="server" Height="185px"
Style="left: -14px; position: relative; top: 4px" Width="391px"></asp:Label>
<asp:Label ID="lbltest" runat="server" Style="left: 68px; position: relative; top: -12px"
Width="169px"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DNSCheck_btn" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<br />
</div>

</div>
</form>
</body>
</html>

Any Help on this would be greatly appreciated

Some logical chage to your code can do that for you!

1. Add a hidden field. Add following line within update panll contenttemplate

<asp:HiddenField runat=server ID="counter" />

2.Add folloing lines in DNSCheck_btn_Click function

counter.Value = tb_PingCount.Text;
lblStatus.Text = "";

3.Modify Timer1_Tick as follows

protected void Timer1_Tick(object sender, EventArgs e)
{

// lblStatus.Text = null;

//for (int q = 0; q < Convert.ToInt32(tb_PingCount.Text); q++)
//{
String host = txtHost.Text.Trim();
pingreply = ping.Send(host);
PingStatus = pingreply.Status.ToString();
pingfunction();
int count=Convert.ToInt32(counter.Value);
count=count-1;
counter.Value = count.ToString();
if (count == 0)
Timer1.Enabled = false;
//}
}

This should work. I hope you understood the logic.


Thank you that worked perfectly

No comments:

Post a Comment