 |
|
 |
This seems go be good counter. I created a footer control (FooterControl.ascx) and placed that counter control inside it. The problem I am having is that when I do refresh of page it keep incrementing the counter. I want this to be increment at Session or Application level. Please let me know how I can achieve that.
Thanks
Syed Anis
|
|
|
|
 |
|
 |
Hi,
If you look below in my post titled "Re: Reset to zero..." where I responded to Syerwinlee, that will probably contain the solution you're looking for.
Regards, David Hobbs
|
|
|
|
 |
|
 |
hi,
The hit counter will be set to zero when link button and image button is clicked. Any solution for this?
I also noticed that when a user click on a hyperlink that bring him to the next page, the hit counter will also be increased by one. How can this problems be solved?
Thanx!
|
|
|
|
 |
|
 |
Hi,
This bit of replacement code might solve the first problem... I had not allowed for postbacks in the initial version, so any button clicks would make the counter appear to go to zero. Try replacing the Value property with the following code:
[Bindable(true),Category("Appearance"),DefaultValue("9999")]
public int Value
{
get { return (int)ViewState["mCount"]; }
set { ViewState["mCount"] = value; }
}
This uses ViewState, so the counter should be kept across postbacks. Please let me know if that solves it.
To solve the second problem, I guess you could use a session variable. Something like this:
if (Page.Session["DSHalreadycounted"]==null)
{
//increment the counter here
Page.Session["DSHalreadycounted"]="Y";
}
This would mean that each browser session would only record one hit when the user first uses the application. After that the counter should not increment.
I hope that helps. Let me know how you get on with that.
|
|
|
|
 |
|
 |
So sorry for late reply, have been bz with something else...
i added the control to my user control, and use drag and drop in using the hit counter control. I am not really sure where can i add this piece of code. Thanks!
|
|
|
|
 |
|
 |
Hi David,
I have tried out the solution u provided.
Now the hit counter will not increased by one in the same browser or reset to zero when i refresh my page, or click on a hyperlink, or click on a button that does postback or no postback.
I replaced the onLoad method with the following code:
protected override void OnLoad(EventArgs e)
{
if (Context != null && !Page.IsPostBack)
{
if (Page.Session["DSHalreadycounted"]==null)
{
//increment the counter here
Value = increment();
Page.Session["DSHalreadycounted"]="Y";
}
}
Value = getCounterFromFile();
base.OnLoad(e);
}
I have also tried to replace the Value property with the code u provided, but it does not work out. Anyhow, the above code had solved my problems.
Thanks alot David! U r cool!
|
|
|
|
 |
|
 |
It is a fantastic control because I too sometimes do not want to bother with a SQL database or the images.
Having external images for the digits is however nice when you want more than the boring fonts available in a default windows installation.
Although I have not tried it I think one point of concern is the write permissions. It would be good to have a separate folder for this purpose rather than the one the application is (security).
Additionally I am concerned about how the control behaves under stress. For example peak periods. What about conflicts during the update of the text file? perhaps some lock-management code would make it more robust.
But other than that I think it is just fantastic, really!
|
|
|
|
 |
|
 |
Thanks for the feedback. If you wanted to put the counter file in another folder you can set the TextFileName parameter to use another folder as long as it is inside the application somewhere, so you could set the parameter to counter\count.txt and have a special folder that way.
Locking would be a great idea also, if I get some time to do more work on this control that will be first on my list.
I personally don't want to put in option of using images, but that's only because I designed the control to be really simple, and it goes against one of my design goals for this control. But if anybody out there wants to implement that for themselves then feel free!
|
|
|
|
 |
|
 |
David,
Great component. I am having a problem during peak activity times. The counter resets to zero. I am under the impression that the FileIOPermissionAccess.Append is not active and when a new visitor activates an increase with the file already open, it creates a new file set at zero. Is this true? I was going to get into the code and make sure that an attempt to open an already open file simple returns and doesn't try and opening the file again. This effectively misses a hit but doesn't reset the counter to zero. Has anyone else run into this problem? Am I missing something? Is there an update to resolve this?
Jim
Jim
|
|
|
|
 |
|
 |
Hi Jim,
At the moment there is no update since I posted this version. To be honest I have not done any work on this component for a while. You are however the first person to report a problem like this, so I have not been aware of any issues with this component when websites are under load.
From what you're saying it does seem likely that it's something to do with two threads trying to write to the file at the same time. I'm wondering if we could introduce some locking to try and solve this.
Regards, David
|
|
|
|
 |
|
 |
Your hitcounter requires that the application has FileIOPermissionAccess.Write, FileIOPermissionAccess.Append and FileIOPermissionAccess.Read rights to the folder hosting it.
I don't know how to setting those rights
Can you tell me how to do this
|
|
|
|
 |
|
 |
I think that this is because of the permissions of the account which ASP.Net runs under. This can be changed by editing the <processModel> Element of your machine.config file. Specifically you need to change the userName and the password values to a user account which has permission to read and write to the file storing the hit count data.
You'll need to either create an account which has the relevant permissions and use that account in the <processModel> section of machine.config, or you can take a shortcut and set the userName value to 'System' and the password to 'AutoGenerate'. This is not a recommended solution for a production server, but would be fine for development and test purposes.
Documentation on <processModel> can be found here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/gngrfprocessmodelsection.asp[^]
|
|
|
|
 |
|
 |
It's OK when I run your demo, but when I put the hitcounter on my app, it dosen't work. I think it's not because of the permission of the accont which asp.net run under
|
|
|
|
 |
|
 |
I had my hit counter installed and working in less than 1 minute!
Thank you very much David!
Sergio Cossa
Argentina
|
|
|
|
 |
|
 |
I tested it in Netscape 7.1 and Opera 7 and found that you cannot see the counter digits - they appear the same colour as the background. Below is the fix needed in the Render method. Swap the existing foreach loop with this new version:
foreach (char c in mCount.ToString().PadLeft(mPadding,'0'))
{
WebControl td=new WebControl(HtmlTextWriterTag.Td);
td.CopyBaseAttributes(this);
td.RenderBeginTag(output);
output.Write(c);
td.RenderEndTag(output);
}
This minor change makes a big difference. You now get a similar look in the other browsers.
Regards, David
|
|
|
|
 |
|
 |
I have taken everybody's feedback into account and created this new version. Thanks again for the great ideas everybody!
Cheers, David
|
|
|
|
 |
|
 |
Each time the page is requested this control has to create a filesystem object to open the text file to read the present value, and then create another filesystem object to open the file to write the incremented value.
Surely this amount of additional hard disk traffic is going to become a problem when the page is hit many times?
Wouldn't it be better to store the counter value as an application variable and only write it to the filesystem periodically?
Brian
----@
|
|
|
|
 |
|
 |
You're right that performance may become an issue for sites that have really high levels of traffic. Although I do think that this control would be fine for the type of site I originally designed it for.
However, I suppose a good solution would be to use the Cache object and set an expiry time (which could be a new property exposed by the HitCounter control).
When the cache item expires the counter can be stored to disk and then a new cache item created with another expiry time. That would make the application save the data to disk on a periodic basis. It should also make most of the reads from the counter value very fast.
I'll have a go and see what I can come up with, when I'm happy I'll post a new version...
|
|
|
|
 |
|
 |
You could put your filesystem object and the counter value of it in the cache or in an Application variable, incrementing and writing it to dsk for each hit.
You would save some resources on creating and opening the filesystem object (since it's allready there), and save time not needing to read from disk before incrementing.
At the same time, you would get the safety of allways recording the current hit count to disk. (In case of a powerfailiure or something...)
Have a look at my latest article about Object Prevalence with Bamboo Prevalence.
|
|
|
|
 |
|
 |
Being a web thickie, could you show an example of this code in the context which it used?
I'll eaglely await before voting
I am that is
|
|
|
|
 |
|
 |
Great counter!
I have one suggestion though. It would be nice if we could hide the counter from the user, but still have its increment capability. In other words, make the control invisible but still work.
Nice post!
Carl
|
|
|
|
 |
|
 |
This would be very simple to implement, by moving the first line from Render to a new override of OnLoad, and then just setting the control's visible property to false.
ie:
protected override OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.Context != null && !Page.IsPostBack)
{
this.Value = increment();
}
}
Hope this helps!
--
Matt Berther
http://www.mattberther.com
|
|
|
|
 |
|
 |
Good idea. When I next update the article I will use your suggestion above, so that the control can be used to keep track of the hit count without displaying the value to the user, if that's what you need.
Thanks, David
|
|
|
|
 |
|
 |
I know it was easy to implement, but I was suggesting to the author so he can include a property to turn on/off the counter display. A lot more convenient than changing the code, isn't?
Take it easy!
Carl
|
|
|
|
 |
|
 |
There will be a new version of this control ready later today, I've written the code and now I'm testing it out.
You will be able to use the counter without displaying the result, by setting the visible property to false. Additionally, there is a new WriteDelay property which allows you to say how frequently the count is saved to disk. If you set the WriteDelay property to anything other than zero the count is stored in an application variable and only saved to disk at the delay you have specified.
Thanks for all the comments and suggestions guys.
Cheers, David
|
|
|
|
 |