In my project i want to deny going back to the page when i click back button in toolbar.
How its possible in asp.net and c#.
Thank you.
Just put this javascript in the html section of an aspx page above the head section.
This causes every back to return with a forward.
<script type = "text/javascript" >
function disableBackButton()
{
window.history.forward();
}
setTimeout("disableBackButton()", 0);
</script>
plus have a look at this article
https://web.archive.org/web/20210927201700/http://www.4guysfromrolla.com/webtech/111500-1.shtml
If you want to do this by code-behind.Put following code in Page_Load or Page_Init event of the page
protected void Page_Init(object Sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
Response.Cache.SetNoStore();
}
Related
I have searched and implemented a code where I am able to disable the back button of the browser. But I am facing a problem like when I log in and don't do anything and click back button it takes me to login page and then a forward button appears. I don't want the back button to get functional on my home screen immediately when I am logged in. But the back button should be enable within my application but not exactly after i log in. Following is my code that I have used in my master page coding. Please help as I am working on live application.
<script type="text/javascript">
function disableBackButton() {
window.history.forward(1);
//alert("You cannot Nevigate To Back!!!");
}
</script>
<body onload="disableBackButton()" >
</body>
On code behind(Server side) write this:
protected void Page_Load(object sender, EventArgs e)
{
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoServerCaching();
HttpContext.Current.Response.Cache.SetNoStore();
}
and on client side write this:
<script type="text/javascript" language="javascript">
window.history.forward(1);
document.attachEvent("onkeydown", my_onkeydown_handler);
function my_onkeydown_handler()
{
switch (event.keyCode)
{
case 116 : // F5;
event.returnValue = false;
event.keyCode = 0;
window.status = "We have disabled F5";
break;
}
}
</script>
Lot's of way's here
I have a webform with a "clear all fields" button, but it's not working properly. When I click the button on the rendered page, it directs me to another page on the local server, but not the one I have listed here.
protected void btn_Clear_Click(object sender, EventArgs e)
{
Response.Redirect("~/ContentRequest/PR_Event.aspx", true);
}
I'm not sure what I can do...
You can simply use Request.RawUrl to redirect back to current page.
Response.Redirect(Request.RawUrl, true)
However, if you want to just clear fields, you might want to use jQuery or javascript at client-side to avoid a round trip to server.
I am using the below code to call a javascript function from aspx page load. But it's not working.
protected void Page_Load(object sender, EventArgs e)
{
btn_add_more_pack.Attributes.Add("OnClick", "openform()");
}
The javascript :
function openform()
{
try {
alert('enter');
}
catch (ex) {
}
}
I think the problem could be OnClick it should be onclick though it doesn't matter (not case sensitive) but you can try like below.
So on page load it should be as follows
protected void Page_Load(object sender, EventArgs e)
{
btn_add_more_pack.Attributes.Add("onclick", "openform()");
}
I have copied your code and tried in my system. Everything is working fine. But, one catch here:
I am using VS-2010 to work on your item.
Where have you added your javascript function? Is it inside .aspx page head section or a separate file?
If you have added the above Javascript function inside .aspx page head section, then you don't need to alter anything. Everything works fine.
But, if you have added the above Javascript function inside a separate .js file, you should remember to add reference in the .aspx head section like below
<script src="pathToJavascriptFile.js" type="text/javascript"></script>
I need to understand that whether page is being called inside iframe or not at code behind. Is this possible?
I need to determine it in master page code behind.
asp.net 4.0, C#
In general, no.
Of course you can emit client script that detects iframe and reloads the page with e.g. a querystring.
It's not possible. However there's a workaround this. You can use querystring and check on page load if that querystring contains value, for example:
<iframe src="Default.aspx?iframe=true" />
In your Default.aspx.cs file:
protected void Page_Load(object sender, EventArgs e)
{
if(!string.IsNullOrEmpty(Request.QueryString["iframe"]))
{
if(Convert.ToBoolean(Request.QueryString["iframe"])
{
// this page is loaded in an iframe
}
}
}
I'm sure this is fairly straightforward but i'm having trouble getting it to work. I want to add a javascript function to my page, but only when the page postsback.
So I have a button that calls some server-side code, and when it is finished and the page is re-loading I want the javascript function to be called.
Thinking about this i guess I could just add a hidden variable and set it when the button is clicked, but i think i'd rather just insert the javascript onto the page when it is loading back.
Is this possible, or even a good way to do it?
Thanks,
Neil
Edit: Okay this is my OnClick method in the C# code.
protected void Save(object sender, EventArgs e)
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script type=\"text/javascript\">alert('hello world');</script>");
EnforcementMatch(false);
EnforcementMatch(true);
ApplicationNotMatch();
ApplicationMatch();
Response.Redirect(Request.Url.ToString());
}
Another Edit: Just realised that the response.redirect at the bottom reloads my page cancelling out the code I put in, duh!
You can use ClientScriptManager.RegisterClientScriptBlock
http://msdn.microsoft.com/en-us/library/btf44dc9.aspx
If you place it on the button click event, you don't have to worry if it's a postback or not.
You know about the IsPostBack function, right?
IsPostBack (MSDN)
if (IsPostBack)
{
ClientScript.RegisterClientScriptBlock()
}
This script will be fired with every single postback from updatepanel
<script type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(function () {
alert('hello world');
});
</script>
Place it in a separate script block, only to be rendered on postback.
<script type="text/javascript" runat="server" visible="<%#this.IsPostBack %>">
TheCode();
</script>