Link is not working as expectation - asp.net

I have a user control of menu which have links on which user is redirected to the desired page. When I am using that user control in my aspx page, on first time when user click to the link he is redirected to the destination, but when user again click that link it is showing the page stating that "cannot find server", the same thing is happened to the other links also.
In my user control I am using:
Private void Link1_Click(Object sender, eventargs e)
{
Response.Redirect("Secondpage.aspx");
}
Private void Link2_Click(Object sender, eventargs e)
{
Response.Redirect("Thirdpage.aspx");
}
Unexpectdly, the same code is working fine on the production server but throwing issue in the developmwnt server.
I am not sure the cause of the error.. Thanks in advance.

your event is not found on the server as it is defined as Private
I think Private should be Protected
Protected void Link2_Click(Object sender, eventargs e)
{
Response.Redirect("Thirdpage.aspx");
}

Related

getting error in logout using timer in asp.net

whenever I am clicking the logout button it is going to the logout page but Timer1_Tick function is not working and the execution is being stopped in the logout page.It is not going to GuestUser_HOME.aspx page.
The code in the logout page is here.
public partial class WebForm24 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
//string redirectUrl = FormsAuthentication.LoginUrl + "?
ReturnUrl=index2.html";
FormsAuthentication.SignOut();
Session["email"] = null;
Response.Redirect("GuestUser_HOME.aspx");
}
}
Are you sure your timer had started !?
check IsEnabled property before runtime to find out it`s enable or not.
but a better solution is like below ,
first :
Create a function for implement sign out operation.
second :
use this function in right place , for example in Click Event of button or Page Load event of sign out page.
third :
if you want implement this operation in a TimerTickEvent , you must stop timer after complete log out operation , and check if user logged in run SignOut operation not e

How to clear a specific session when user left the page

I have a page. There are some session variables those are used in a specified page only. How to clear those session variables when user left the page.
When user left the page using the menu/URL.
I have tried with
protected void Page_Unload(object sender, EventArgs e)
{
Session["Rules"] = null;
Session.Remove("Rules");
}
But it is now working :(
Please suggest.

Adding FileUpload control via code-behind

Because of the environment I work in I need to add controls to a page via code-behind. I have done it dozens of times. For some reason the FileUpload control is giving me grief. here is my code:
FileUpload fileUpload = new FileUpload();
fileUpload.ID = "FileUploadControl";
this.Controls.Add(fileUpload);
The page looks as though it is timing out and display this error, "Internet Explorer cannot display the webpage".
When I remove the last line (the Add), then the page renders just fine.
Any ideas?
Thanks!!
You didn't mentioned which event handler you have used. Please try this,
FileUpload file;
protected void Page_Load(object sender, EventArgs e)
{
file= new FileUpload();
PlaceHolder1.Controls.Add(file);
}
protected void Button1_Click(object sender, EventArgs e)
{
if(file.HasFile)
{
file.SaveAs(MapPath("~/" + file.FileName));
}
}

Click event not work properly

In my website I wrote that code:
protected void Page_Load(object sender, EventArgs e){ LinkButton lbtnTopicAddress = new LinkButton(); lbtnTopicAddress.Click += lbtnSpecificTopic1_Click;}
protected void lbtnSpecificTopic1_Click(object sender, EventArgs e){ Server.Transfer("~/SpecificTopic.aspx)"
}
But when I press on the link in run time, the caller doesn't go to the EventHandler method.
Why?
Note,
I wrote code like that in many pages in the same website but it work only in one page.
i added that code to many page in website but it worded only in one page every page has its specific code and no relation between them I hope you understand me thanks
I need help pleaseeeeeeee..........................
Did you mean to miss off a ;and a } here?
protected void lbtnSpecificTopic1_Click(object sender, EventArgs e){ Server.Transfer("~/SpecificTopic.aspx)"
I assume you've put a breakpoint in to ensure it isn't being fired?
I'm not exactly sure but I've got a feeling that instead of Page_Load you need to use Page_Init so your code would look this this:
protected void Page_Init(object sender, EventArgs e)
{
LinkButton lbtnTopicAddress = new LinkButton();
lbtnTopicAddress.Click += lbtnSpecificTopic1_Click;
}
protected void lbtnSpecificTopic1_Click(object sender, EventArgs e)
{
Server.Transfer("~/SpecificTopic.aspx");
}
p.s. 5 mins formatting your code can work wonders when trying to debug
Are you adding the button to the controls on your page, or are you trying to find the "lbtnTopicAddress" control on your page?
Simply declaring the button won't do anything -- you have to get a reference to the control itself from the page.

Getting number of instances of asp.net website

I have an asp.net website and i want to get the number of users currently viewing my site. I am aware that there are some third party softwares available, that would give me the list of the users online but i don't want to do that.
Does anyone know how this can be achieved in asp.net? May be if there are any server variables that would keep a track of the website instances that gives the number of users currently visiting the site. Please help me.
if you want to count users which are using your website at the moment you can use the following code in your global.asax file:
private int activeUsers = 0;
protected void Session_Start(Object sender, EventArgs e)
{
activeUsers++;
Context.Items["activeUsers"] = activeUsers;
}
protected void Application_BeginRequest(Object sender, EventArgs e)
{
Context.Items.Add("activeUsers", activeUsers);
}
protected void Session_End(Object sender, EventArgs e)
{
if(activeUsers > 0)
activeUsers--;
}
protected void Application_End(Object sender, EventArgs e)
{
activeUsers = 0;
}
I would use performance counters instead.
Look under
ASP.NET Application Performance Counters
http://msdn.microsoft.com/en-us/library/fxk122b4.aspx

Resources