点击按钮刷新ASP.NET页面

8
我需要在按钮点击时刷新页面,但不增加访问计数器。

1
为什么这是糟糕的问题。我只是想问,我不想在按钮点击时刷新页面来增加我的点击计数器。 - VimalSingh
是的,我的问题没有表达清楚。我会贴上我的代码。 - VimalSingh
2
你“简单地询问”的意思是让别人为你做事,而你自己没有展示出你已经尝试过的工作。这就是为什么有人会抱怨你的原因。你创建了一个没有添加任何代码的问题。SO不在这里为你工作,而是支持你成为最好的程序员。所以不要紧张,只需在你的问题中写更多细节并展示给我们你到目前为止所做的工作。 - M_Griffiths
8个回答

42

在代码后面重定向到同一页。

Response.Redirect(Request.RawUrl);

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
    }
    

2
您可以使用 Response.redirect("YourPage",false) 来刷新页面并增加计数器。

但这也会增加点击计数器,而我不想在刷新时增加点击计数器。 - VimalSingh

1

在按钮点击时,您可以尝试以下操作。

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

在页面加载时,您可以检查加载是否来自该按钮,然后增加计数。
       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;
              }
          }

谢谢


1
当你说要刷新页面时,你正在创建该页面的新实例,因此您需要使用静态变量/会话变量或方法来存储和检索页面上的命中次数。
就页面刷新而言,Response.Redirect(Request.RawUrl); 或 window.location=window.location 可以为您完成工作。

1
使用JavaScript代码可以重新加载页面。使用HTML按钮并像这样实现...
<input type="button" value="Reload Page" onClick="document.location.reload(true)">

0
在按钮单击事件中添加一个唯一的会话或cookie,然后使用Request.RawUrl重定向到同一URL上的页面。 现在在Page_Load事件中添加代码来获取该会话或cookie。 如果会话/cookie匹配,则可以知道页面是使用刷新按钮重定向的。 因此,要将点击计数器减1以保持相同的数字,请执行hitcountr -= hitconter。 否则,增加点击计数器。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接