Refresh asp.net page when clicked

I need to refresh the page when I click the button without increasing the hit counter.

+8
source share
8 answers
  • Create a class to support achievement counters

    public static class Counter { private static long hit; public static void HitCounter() { hit++; } public static long GetCounter() { return hit; } } 
  • Increase counter value on 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 to itself and display the counter value by pressing the button

     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 } 
+6
source share

This is to the code for redirecting to the same page.

 Response.Redirect(Request.RawUrl); 
+36
source share

You can do Response.redirect("YourPage",false) , which will refresh your page and also increase the counter.

+2
source share

At the click of a button, you can try the following.

 protected void button1_Click(object sender, EventArgs e) { Response.Redirect("~/Admin/Admin.aspx"); } 

And on the Load page you can check whether the download is from this button, and then increase the score.

  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

+1
source share

When you say, refresh the page, your new instance of the page that you are creating so that you need to have a static variable/session variable or method to store and get the number of views on your page.

Regarding the page refresh, Response.Redirect(Request.RawUrl); or window.location=window.location will do the job for you.

+1
source share

Page reloading can be done using javascript code. Use the HTML button and implement it as ...

 <input type="button" value="Reload Page" onClick="document.location.reload(true)"> 
0
source share

  XmlDocument doc = new XmlDocument(); doc.Load(@"F:\dji\A18rad\A18rad\XMLFile1.xml"); List<vreme> vreme = new List<vreme>(); string grad = Request.Form["grad"]; foreach (XmlNode cvor in doc.SelectNodes("/vreme/Prognoza")) { if (grad == cvor["NazivMesta"].InnerText) vreme.Add(new vreme { mesto = cvor["NazivMesta"].InnerText, maxtemp = cvor["MaxTemperatura"].InnerText, mintemp = cvor["MinTemperatura"].InnerText, vremee = cvor["Vreme"].InnerText }); } return View(vreme); } public ActionResult maxtemperature() { XmlDocument doc = new XmlDocument(); doc.Load(@"F:\dji\A18rad\A18rad\XMLFile1.xml"); List<vreme> vreme = new List<vreme>(); foreach (XmlNode cvor in doc.SelectNodes("/vreme/Prognoza")) { vreme.Add(new vreme { mesto = cvor["NazivMesta"].InnerText, maxtemp = cvor["MaxTemperatura"].InnerText }); } return View(vreme); } } } @*@{ ViewBag.Title = "maxtemperature"; } @Html.ActionLink("Vreme Po izboru","index","home") <h2>maxtemperature</h2> <table border="10"> <tr><th>Mesto</th> <th>MaxTemp</th> </tr> @foreach (A18rad.Models.vreme vr in Model) { <tr> <td>@vr.mesto</td> <td>@vr.maxtemp</td> </tr> } </table>*@ @*@{ ViewBag.Title = "Index"; } @Html.ActionLink("MaxTemperature","maxtemperature","home") @using(Html.BeginForm("Index","Home")){ <h2>Index</h2> <span>Grad:</span><select name="grad"> <option value="Nis">Nis</option> <option value="Beograd">Beograd</option> <option value="Kopaonik">Kopaonik</option> </select> <input type="submit" value="Moze" /><br /> foreach (A18rad.Models.vreme vr in Model) { <span>Min temperatura:</span> @vr.mintemp<br /> <span>Max temperatura:</span> @vr.maxtemp<br /> if(vr.vremee =="Kisa"){ <span>Vreme:</span> <img src ="kisa.jpg" /> } else if(vr.vremee =="Sneg"){ <img src="sneg.jpg" /> } else if (vr.vremee == "Vedro") { <img src ="vedro.png" /><br /> } } }*@ 
0
source share

Add one unique session or cookie to the button click event, then redirect the page to the same URL using Request.RawUrl. Now add the code to the Page_Load event to receive this session or cookies. If the session / cookie matches, you can find out that the page has been redirected using the refresh button. So decrease the hit counter by 1 so that it stays the same.

Increase the hit counter.

0
source share

All Articles