Click here to Skip to main content
15,920,828 members
Home / Discussions / C#
   

C#

 
GeneralRe: Maybe not an interesting question, but... Pin
Stanciu Vlad6-Apr-06 22:33
Stanciu Vlad6-Apr-06 22:33 
GeneralRe: Maybe not an interesting question, but... Pin
Judah Gabriel Himango7-Apr-06 4:01
sponsorJudah Gabriel Himango7-Apr-06 4:01 
AnswerRe: Maybe not an interesting question, but... Pin
Graham Nimbley6-Apr-06 15:55
Graham Nimbley6-Apr-06 15:55 
QuestionHelp me..with this message..The server viewstate cache has timed out. Pin
jorgeils6-Apr-06 11:53
jorgeils6-Apr-06 11:53 
AnswerRe: Help me..with this message..The server viewstate cache has timed out. Pin
Judah Gabriel Himango6-Apr-06 12:23
sponsorJudah Gabriel Himango6-Apr-06 12:23 
QuestionPls Help me Pin
narendrakumarp6-Apr-06 10:30
narendrakumarp6-Apr-06 10:30 
AnswerRe: Pls Help me Pin
Judah Gabriel Himango6-Apr-06 12:22
sponsorJudah Gabriel Himango6-Apr-06 12:22 
QuestionList of running applications Pin
Sergey Gorchichko6-Apr-06 9:17
Sergey Gorchichko6-Apr-06 9:17 
AnswerRe: List of running applications Pin
Judah Gabriel Himango6-Apr-06 10:02
sponsorJudah Gabriel Himango6-Apr-06 10:02 
QuestionRandom in C# Pin
alee15.10.886-Apr-06 8:21
alee15.10.886-Apr-06 8:21 
GeneralRe: Random in C# Pin
Office Lineman6-Apr-06 8:35
Office Lineman6-Apr-06 8:35 
GeneralRe: Random in C# Pin
alee15.10.886-Apr-06 8:40
alee15.10.886-Apr-06 8:40 
AnswerRe: Random in C# Pin
Office Lineman6-Apr-06 8:45
Office Lineman6-Apr-06 8:45 
GeneralRe: Random in C# Pin
alee15.10.886-Apr-06 9:11
alee15.10.886-Apr-06 9:11 
GeneralRe: Random in C# Pin
junglerover776-Apr-06 16:26
junglerover776-Apr-06 16:26 
AnswerRe: Random in C# Pin
Jakob Farian Krarup6-Apr-06 8:46
Jakob Farian Krarup6-Apr-06 8:46 
Hi Alee Big Grin | :-D

It would be helpful if you told us whether this is a windows- or web application. But I guess you're talking about web.

You could store the number of times in ViewState (a hidden form field on the webpage)

Something along the lines of:
http://www.sweetsilence.dk/codeprojectprojects/limitedclicks/[^]

Here is the code below, also available at:
http://www.sweetsilence.dk/codeprojectprojects/limitedclicks/limitedclicks.zip[^]

--------------------------------------------------------------------------
public class WebForm1 : System.Web.UI.Page
{
	protected System.Web.UI.WebControls.Label lblQuote;
	protected System.Web.UI.WebControls.Label lblNumberOfClicksLabel;
	protected System.Web.UI.WebControls.Label lblNumberOfClicksValue;
	protected System.Web.UI.WebControls.Button btnShowQuote;
	
	//makes it easier to store an int in ViewState by wrapping it in a property
	protected int NumberOfClicks
	{
		get 
		{
			//if there is no value in ViewState, return 0 (zero)
			if(this.ViewState["clicks"] == null)
				return 0;
			else
				return (int)ViewState["clicks"];
		}
		set{this.ViewState["clicks"] = value;}
	}

	private void btnShowQuote_Click(object sender, System.EventArgs e)
	{
		//if there are still clicks left
		if(this.NumberOfClicks < 10)
		{
			//show quote
			lblQuote.Text = GetQuote();
			//increment number of clicks
			NumberOfClicks++;
		}
		else
		{
			//let them know that the show is over
			lblQuote.Text = "[no more quotes for you!]";
			lblQuote.ForeColor = Color.Red;
		}
	}

	public string GetQuote()
	{
		//implement your own code to get random quote
		if(this.NumberOfClicks % 2 == 0
			return "Even is pretty good";
		else
			return "Odd is good too ; )";
	}

	private void WebForm1_PreRender(object sender, System.EventArgs e)
	{
		lblNumberOfClicksValue.Text = this.NumberOfClicks.ToString();
	}
} 

Three kinds of people in the world:
- Those who can count..
- Those who can't!

-- modified at 14:47 Thursday 6th April, 2006
GeneralRe: Random in C# Pin
alee15.10.886-Apr-06 9:07
alee15.10.886-Apr-06 9:07 
GeneralRe: Random in C# Pin
Jakob Farian Krarup6-Apr-06 9:37
Jakob Farian Krarup6-Apr-06 9:37 
QuestionToolStripRenderer Pin
vishalkarlay6-Apr-06 6:49
vishalkarlay6-Apr-06 6:49 
AnswerRe: ToolStripRenderer Pin
Judah Gabriel Himango6-Apr-06 6:56
sponsorJudah Gabriel Himango6-Apr-06 6:56 
QuestionHow to declare virtual Columns Pin
mrkeivan6-Apr-06 6:20
mrkeivan6-Apr-06 6:20 
AnswerRe: How to declare virtual Columns Pin
Office Lineman6-Apr-06 8:42
Office Lineman6-Apr-06 8:42 
GeneralRe: How to declare virtual Columns Pin
mrkeivan6-Apr-06 9:11
mrkeivan6-Apr-06 9:11 
AnswerRe: How to declare virtual Columns Pin
Office Lineman6-Apr-06 9:30
Office Lineman6-Apr-06 9:30 
QuestionListBox problem Pin
Ready4u6-Apr-06 6:09
Ready4u6-Apr-06 6:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.