9

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

3
  • 1
    why is it crappy qns.i am simply asking that i dont waant to increase my hit counter while refreshing page on button click. Commented Apr 17, 2014 at 2:02
  • yeah my question was not properly formed .i will paste my code as well. Commented Apr 17, 2014 at 3:57
  • 2
    What your 'simply asking' is for someone to do it for you without any work on your part to show them what you have got/tried so far. That's why you've been moaned at. You created a question without adding any code. SO is not here to do your job for you but to support you to be the best programmer you can be. Sop don't stress, just write more detail into your question and show us what you've tried so far. Commented Sep 16, 2017 at 6:58

8 Answers 8

43

That on code behind redirect to the same page.

Response.Redirect(Request.RawUrl);
Sign up to request clarification or add additional context in comments.

Comments

6
  • 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
    }
    

Comments

2

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

1 Comment

but this will increase the hit counter as well and i dont want to increase hit counter on refresh
2

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.

Comments

1

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)">

Comments

1

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

Comments

0

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.

Comments

-1

  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 />
       }
}
    
   
        
        
}*@

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.