C# 单击按钮刷新asp.net页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17779973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refresh an asp.net page on button click
提问by VimalSingh
I need to refresh a page on button click without increasing the hit counter.
我需要在不增加点击计数器的情况下点击按钮刷新页面。
采纳答案by SiwachGaurav
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 }
创建一个用于维护命中计数器的类
public static class Counter { private static long hit; public static void HitCounter() { hit++; } public static long GetCounter() { return hit; } }
在页面加载事件增加计数器的值
protected void Page_Load(object sender, EventArgs e) { Counter.HitCounter(); // call static function of static class Counter to increment the counter value }
重定向页面本身并在按钮单击时显示计数器值
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 }
回答by Bibek Gautam
You can do Response.redirect("YourPage",false)
that will refresh your page and also increase counter.
您可以这样做Response.redirect("YourPage",false)
将刷新您的页面并增加计数器。
回答by Pushpendra
Page reload can be done using javascript code. Use either a HTML button and implement it like...
页面重新加载可以使用 javascript 代码完成。使用 HTML 按钮并实现它,例如...
<input type="button" value="Reload Page" onClick="document.location.reload(true)">
回答by Saritha.S.R
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.
在 PageLoad 上,您可以检查加载是否来自该按钮,然后增加计数。
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
谢谢
回答by Ishan Jain
That on code behind redirect to the same page.
后面的代码重定向到同一页面。
Response.Redirect(Request.RawUrl);
回答by Vinay Pandey
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.
当您说刷新页面时,它是您正在创建的页面的新实例,因此您需要使用 astatic variable/session variable
或 amethod
来存储和检索页面上的点击次数。
As far as refreshing the page is concerned, Response.Redirect(Request.RawUrl);
or window.location=window.location
would do the job for you.
就刷新页面而言,Response.Redirect(Request.RawUrl);
或者window.location=window.location
会为您完成这项工作。
回答by Marko
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 />
}
}
}*@
回答by Prakash Mhasavekar
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
在按钮单击事件中添加一个唯一的会话或 cookie,然后使用 Request.RawUrl 在同一 URL 上重定向页面 现在在 Page_Load 事件中添加代码以获取该会话或 coockie。如果会话/cookie 匹配,那么您可以知道使用刷新按钮重定向的页面。因此,将命中计数器减少 1 以使其保持相同的数字 do hitcountr -= hitconter
Else increase the hit counter.
否则增加命中计数器。