Create a class for maintain hit counters
public static class Counter
{
private static long hit;
public static void HitCounter()
{
hit++;
}
public static long GetCounter()
{
return hit;
}
}
Increment the value of counter at page load event
protected void Page_Load(object sender, EventArgs e)
{
Counter.HitCounter(); // call static function of static class Counter to increment the counter value
}
Redirect the page on itself and display the counter value on button click
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(Request.RawUrl.ToString()); // redirect on itself
Response.Write("<br /> Counter =" + Counter.GetCounter() ); // display counter value
}
When you say refresh the page, its new instance of the page that you are creating so you need to either have a static variable/session variable
or a method
to store and retrieve the count of hits on your page.
As far as refreshing the page is concerned, Response.Redirect(Request.RawUrl);
or window.location=window.location
would do the job for you.
XmlDocument doc = new XmlDocument();_x000D_
doc.Load(@"F:\dji\A18rad\A18rad\XMLFile1.xml");_x000D_
List<vreme> vreme = new List<vreme>();_x000D_
string grad = Request.Form["grad"];_x000D_
_x000D_
foreach (XmlNode cvor in doc.SelectNodes("/vreme/Prognoza"))_x000D_
{_x000D_
if (grad == cvor["NazivMesta"].InnerText)_x000D_
vreme.Add(new vreme_x000D_
{_x000D_
mesto = cvor["NazivMesta"].InnerText,_x000D_
maxtemp = cvor["MaxTemperatura"].InnerText,_x000D_
mintemp = cvor["MinTemperatura"].InnerText,_x000D_
vremee = cvor["Vreme"].InnerText_x000D_
});_x000D_
}_x000D_
return View(vreme);_x000D_
}_x000D_
public ActionResult maxtemperature()_x000D_
{_x000D_
XmlDocument doc = new XmlDocument();_x000D_
doc.Load(@"F:\dji\A18rad\A18rad\XMLFile1.xml");_x000D_
List<vreme> vreme = new List<vreme>();_x000D_
_x000D_
foreach (XmlNode cvor in doc.SelectNodes("/vreme/Prognoza"))_x000D_
{_x000D_
vreme.Add(new vreme_x000D_
{_x000D_
mesto = cvor["NazivMesta"].InnerText,_x000D_
maxtemp = cvor["MaxTemperatura"].InnerText_x000D_
});_x000D_
}_x000D_
return View(vreme);_x000D_
}_x000D_
}_x000D_
}_x000D_
@*@{_x000D_
ViewBag.Title = "maxtemperature";_x000D_
}_x000D_
@Html.ActionLink("Vreme Po izboru","index","home")_x000D_
<h2>maxtemperature</h2>_x000D_
<table border="10">_x000D_
<tr><th>Mesto</th>_x000D_
<th>MaxTemp</th>_x000D_
</tr>_x000D_
@foreach (A18rad.Models.vreme vr in Model)_x000D_
{_x000D_
<tr>_x000D_
<td>@vr.mesto</td>_x000D_
<td>@vr.maxtemp</td>_x000D_
</tr>_x000D_
}_x000D_
</table>*@_x000D_
@*@{_x000D_
ViewBag.Title = "Index";_x000D_
}_x000D_
@Html.ActionLink("MaxTemperature","maxtemperature","home")_x000D_
@using(Html.BeginForm("Index","Home")){_x000D_
<h2>Index</h2>_x000D_
_x000D_
_x000D_
<span>Grad:</span><select name="grad">_x000D_
<option value="Nis">Nis</option>_x000D_
<option value="Beograd">Beograd</option>_x000D_
<option value="Kopaonik">Kopaonik</option>_x000D_
</select>_x000D_
<input type="submit" value="Moze" /><br />_x000D_
foreach (A18rad.Models.vreme vr in Model)_x000D_
{_x000D_
<span>Min temperatura:</span> @vr.mintemp<br />_x000D_
<span>Max temperatura:</span> @vr.maxtemp<br />_x000D_
if(vr.vremee =="Kisa"){_x000D_
<span>Vreme:</span> <img src ="kisa.jpg" />_x000D_
}_x000D_
else if(vr.vremee =="Sneg"){_x000D_
<img src="sneg.jpg" />_x000D_
} else if (vr.vremee == "Vedro") { _x000D_
_x000D_
<img src ="vedro.png" /><br />_x000D_
}_x000D_
}_x000D_
_x000D_
_x000D_
_x000D_
_x000D_
}*@
_x000D_
You can do Response.redirect("YourPage",false)
that will refresh your page and also increase counter.
On button click you can try the following.
protected void button1_Click(object sender, EventArgs e)
{
Response.Redirect("~/Admin/Admin.aspx");
}
And on PageLoad you can check whether the loading is coming from that button then increase the count.
protected void Page_Load(object sender, EventArgs e)
{
StackTrace stackTrace = new StackTrace();
string eventName = stackTrace.GetFrame(1).GetMethod().Name; // this will the event name.
if (eventName == "button1_Click")
{
// code to increase the count;
}
}
Thanks
Add one unique session or cookie in button click event then redirect page on same URL using Request.RawUrl Now add code in Page_Load event to grab that session or coockie. If session/cookie matches then you can knows that page redirected using refresh button. So decrease hit counter by 1 to keep it on same number do hitcountr -= hitconter
Else increase the hit counter.
That on code behind redirect to the same page.
Response.Redirect(Request.RawUrl);
Page reload can be done using javascript code. Use either a HTML button and implement it like...
<input type="button" value="Reload Page" onClick="document.location.reload(true)">
Source: Stackoverflow.com