ASP.NET Session value not changing - TicTacToe app - asp.net

So, I'm trying to make a TicTacToe application, but the problem is the clickNo counter which should be counting clicks (after every click) is not being updated after the click. I've tried using if IsPostBack == false in Page Load, as someone suggested but the only thing that it accomplished is that a "O" randomly pops out.
{
public partial class home : System.Web.UI.Page
{
public int clickedByPlayerNo, clickNo;
protected void Page_Load(object sender, EventArgs e)
{
Session["s1"] = clickNo;
}
public void buttonClicked(object Sender)
{
clickNo = (int)Session["s1"];
if (Session["s1"] != null)
{
clickNo = +1;
if (clickNo % 2 == 0)
{
clickedByPlayerNo = 2;
}
else
{
clickedByPlayerNo = 1;
}
if (clickedByPlayerNo == 1)
{
((Button)Sender).Text = "X";
}
else
{
((Button)Sender).Text = "O";
}
((Button)Sender).Enabled = false;
Session["s1"] = clickNo;
checkWinner();
}
else
{
Response.Write("Session expire");
}
}
public void checkWinner()
{
if (clickNo > 4)
{
if (btn1.Text == btn2.Text && btn2.Text == btn3.Text && btn1.Text != "")
{
displayWinner();
}
else if (btn4.Text == btn5.Text && btn5.Text == btn6.Text && btn4.Text != "")
{
displayWinner();
}
else if (btn7.Text == btn8.Text && btn8.Text == btn9.Text && btn7.Text != "")
{
displayWinner();
}
else if (btn1.Text == btn4.Text && btn4.Text == btn7.Text && btn1.Text != "")
{
displayWinner();
}
else if (btn2.Text == btn5.Text && btn5.Text == btn8.Text && btn2.Text != "")
{
displayWinner();
}
else if (btn3.Text == btn6.Text && btn6.Text == btn9.Text && btn3.Text != "")
{
displayWinner();
}
else if (btn1.Text == btn5.Text && btn5.Text == btn9.Text && btn1.Text != "")
{
displayWinner();
}
else if (btn3.Text == btn5.Text && btn5.Text == btn7.Text && btn3.Text != "")
{
displayWinner();
}
else if (clickNo == 9)
{
Response.Write("Game Draw");
clearWindow();
}
else
{
Response.Write("Session expire");
}
}
}
public void displayWinner()
{
if (clickedByPlayerNo == 1)
{
Response.Write("Winner is A");
}
else
Response.Write("Winner is B");
clearWindow();
}
public void clearWindow()
{
btn1.Text = "";
btn1.Enabled = false;
btn2.Text = "";
btn2.Enabled = false;
btn3.Text = "";
btn3.Enabled = false;
btn4.Text = "";
btn4.Enabled = false;
btn5.Text = "";
btn5.Enabled = false;
btn6.Text = "";
btn6.Enabled = false;
btn7.Text = "";
btn7.Enabled = false;
btn8.Text = "";
btn8.Enabled = false;
btn9.Text = "";
btn9.Enabled = false;
}
protected void btn1_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
protected void btn2_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
protected void btn3_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
protected void btn4_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
protected void btn5_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
protected void btn6_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
protected void btn7_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
protected void btn8_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
protected void btn9_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
}
}
My sincerest thanks to Red, I've been searching the net and looking at the monitor for 10 hours straight. Here's the updated part of the code:
public partial class home : System.Web.UI.Page
{
public int clickedByPlayerNo, clickNo;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["s1"] = "-1";
}
}
public void buttonClicked(object Sender)
{
clickNo = Convert.ToInt32(Session["s1"]);
if (Session["s1"] != null)
{
int someValue = clickNo + 1;
if (clickNo % 2 == 0)
{
clickedByPlayerNo = 2;
}
else
{
clickedByPlayerNo = 1;
}
if (clickedByPlayerNo == 1)
{
((Button)Sender).Text = "X";
}
else
{
((Button)Sender).Text = "O";
}
((Button)Sender).Enabled = false;
Session["s1"] = someValue;
}
checkWinner();
}

Take a closer look at Page_Load method. Every time you have a postback posted by one of your buttons, Page_Load method is called. What you need to do is to fill the session variable only once at page load. Here is what I would do:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["s1"] = "0";
}
}

Related

Force String.Format "{0:P4}" to show + sign

I have a decimal column in my Database where values are stored as 12.35
We show it as 12.35%
The client wants to show +12.35% if the value is positive(just for this one field). How I do get it to show the +sign.
We format the textedit as P4 in the getter String.Format("{0:P4}", value);
This is what I've tried:
I was able to do this by using Fomrat event handler. I am looking for a cleaner way instead of the below code.
private void txtMargin_FormatEditValue(object sender, DevExpress.XtraEditors.Controls.ConvertEditValueEventArgs e)
{
if (e.Value != null)
{
if (e.Value.ToString().IndexOfAny(new char[] { '-', '+' }) < 0)
{
string val = e.Value.ToString();
val = val.Replace("%", "");
e.Value = string.Format("+{0}", (Convert.ToDouble(val) / 100).ToString("P4"));
e.Handled = true;
}
else
{
string val = e.Value.ToString();
val = val.Replace("%", "");
e.Value = (Convert.ToDouble(val) / 100).ToString("P4");
}
e.Handled = true;
}
}
private void txtMargin_ParseEditValue(object sender, DevExpress.XtraEditors.Controls.ConvertEditValueEventArgs e)
{
if (e.Value != null)
{
if (e.Value.ToString().IndexOf('%') < 0)
{
e.Value = (Convert.ToDouble(e.Value.ToString()) / 100).ToString("P4");
}
}
}
In your form load past this code :
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textEdit1.Properties.Mask.EditMask = "+#0.0000% ;-#0.0000%";
textEdit1.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric;
textEdit1.Properties.Mask.UseMaskAsDisplayFormat = false;
textEdit1.Properties.EditFormat.FormatString = "+#0.0000% ;-#0.0000%";;
}
And in you TextBox Handel the event "`CustomDisplayText`" as :
private void textEdit1_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
{
if (e.Value != null && !e.Value.Equals (""))
e.DisplayText = (Convert.ToDouble(e.Value.ToString()) / 100).ToString("+#0.0000 % ;-#0.0000 %");
}

Custom ASPxGridViewExporter

I have an aspxgridview, I change displayed text via "CustomColumnDisplayText" event, My problem is when I want to use ASPxGridViewExporter for excel output, one of columns shows wrong data. I don't know how to use ASPxGridViewExporter RenderBrick event.
protected void ASPxGridView1_CustomColumnDisplayText(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDisplayTextEventArgs e)
{
if (e.Column.PropertiesEdit.DisplayFormatString.Equals("Status"))
{
if (Convert.ToInt64(e.Value) == 2)
e.Value = "Successful";
else if (e.GetFieldValue("SaleReferenceId") == null || e.GetFieldValue("SaleReferenceId").ToString() == string.Empty || e.GetFieldValue("SaleReferenceId").ToString().Trim().Equals(""))
e.Value = "Invalid";
else if (e.GetFieldValue("saleOrderId") == null || e.GetFieldValue("saleOrderId").ToString() == string.Empty || e.GetFieldValue("saleOrderId").ToString().Trim().Equals(""))
e.Value = "Invalid";
else
e.Value = "Unsuccessful";
e.DisplayText = e.Value.ToString();
}
}
protected void ASPxGridViewExporter1_RenderBrick(object sender, DevExpress.Web.ASPxGridView.Export.ASPxGridViewExportRenderingEventArgs e)
{
// I don't know how to use it
}
I don't know how but this snippet works fine for me and the result is exactly what I wanted!
protected void ASPxGridViewExporter1_RenderBrick(object sender, DevExpress.Web.ASPxGridView.Export.ASPxGridViewExportRenderingEventArgs e)
{
try
{
GridViewDataColumn dataColumn = e.Column as GridViewDataColumn;
if (dataColumn.FieldName == "Status")
{
e.TextValue = "123";
}
}
catch (Exception ex)
{
}
}

check if button is clicked in if() condition aspx.net

I want to check if btnTest_Click is clicked in another Button6_Click event.Below is my code.....Please help
protected void btnTest_Click(object sender, EventArgs e)
{
Session["Counter1"] = newValue;
Session["Counter"] = newValue;
if (Session["Markici"] != null || Session["Markici"] != null)
{
var clickedRow = ((Button)sender).NamingContainer as GridViewRow;
var clickedIndex = clickedRow.RowIndex;
/*decimal*/ old = dtCurrentTable.Rows[clickedIndex].Field<decimal>("Kolicina");
decimal oldIznos = dtCurrentTable.Rows[clickedIndex].Field<decimal>("VkIznos");
decimal VkDanok = dtCurrentTable.Rows[clickedIndex].Field<decimal>("VkDanok");
string Cena1 = dtCurrentTable.Rows[clickedIndex].Field<string>("Cena1");
int TarifaID = dtCurrentTable.Rows[clickedIndex].Field<Int16>("TarifaID");
}
protected void Button6_Click(object sender, EventArgs e)
{
// how to check here if btnTest_Click is clickied
if()
}
as per Kevin's answer but instead of:
protected bool testPassed;
Have this:
protected bool testPassed
{
get { return (bool)ViewState["testpassed"]; }
set { ViewState["testpassed"] = value; }
}
By accessing the value of this property via view state the value will persist between requests.
I would declare a class level boolean called testPassed.
Set it to false in the onload event if it is not a Postback.
Set it to true in the btnTest_Click event handler
Edit to add an example:
protected bool testPassed
{
get { return (bool)ViewState["testpassed"]; }
set { ViewState["testpassed"] = value; }
}
protected override void OnLoad(EventArgs e)
{
if (!Page.IsPostBack)
{
testPassed=false;
}
}
protected void btnTest_Click(object sender, EventArgs e)
{
Session["Counter1"] = newValue;
Session["Counter"] = newValue;
if (Session["Markici"] != null || Session["Markici"] != null)
{
var clickedRow = ((Button)sender).NamingContainer as GridViewRow;
var clickedIndex = clickedRow.RowIndex;
/*decimal*/ old = dtCurrentTable.Rows[clickedIndex].Field<decimal>("Kolicina");
decimal oldIznos = dtCurrentTable.Rows[clickedIndex].Field<decimal>("VkIznos");
decimal VkDanok = dtCurrentTable.Rows[clickedIndex].Field<decimal>("VkDanok");
string Cena1 = dtCurrentTable.Rows[clickedIndex].Field<string>("Cena1");
int TarifaID = dtCurrentTable.Rows[clickedIndex].Field<Int16>("TarifaID");
testPassed=true;
}
protected void Button6_Click(object sender, EventArgs e)
{
// how to check here if btnTest_Click is clickied
if(testPassed)
}

Truncate text in AspxGridView cells

Is there a way to truncate long text in AspxGridView cells?
I've read and implemented this solution. , http://documentation.devexpress.com/#AspNet/DevExpressWebASPxGridViewASPxGridView_CustomColumnDisplayTexttopic
...which of course works BUT only for one column and I need to create this with several columns.
Here is my solution so far
protected void AsPxGridView1CustomColumnDisplayText(object sender, ASPxGridViewColumnDisplayTextEventArgs e)
{
if (e.Column.FieldName != "AnalysisFeedbackAuto") return;
if (e.Value.ToString().Length > 13)
{
var displayText = Regex.Replace(e.Value.ToString(), "<.*?>", string.Empty).Substring(0, 10);
e.DisplayText = string.Concat(displayText, "...");
}
}
Any advice?
Thanks
=== UPDATE ===
Obviously this was the solution
protected void AsPxGridView1CustomColumnDisplayText(object sender, ASPxGridViewColumnDisplayTextEventArgs e)
{
if (e.Column.FieldName == "AnalysisFeedbackAuto"
|| e.Column.FieldName == "AnalysisResults"
|| e.Column.FieldName == "AnalysisAnswers"
)
{
if (e.Value.ToString().Length > 13)
{
var displayText = Regex.Replace(e.Value.ToString(), "<.*?>", string.Empty).Substring(0, 10);
e.DisplayText = string.Concat(displayText, "...");
}
}
}
Here are solutions from DevExpress:
http://www.devexpress.com/issue=Q300507
http://www.devexpress.com/issue=Q303093
protected void AsPxGridView1CustomColumnDisplayText(object sender, ASPxGridViewColumnDisplayTextEventArgs e)
{
if (e.Column.FieldName == "AnalysisFeedbackAuto"
|| e.Column.FieldName == "AnalysisResults"
|| e.Column.FieldName == "AnalysisAnswers"
)
{
if (e.Value.ToString().Length > 13)
{
var displayText = Regex.Replace(e.Value.ToString(), "<.*?>", string.Empty).Substring(0, 10);
e.DisplayText = string.Concat(displayText, "...");
}
}
}

Login page fault

I had login Page and it worked well but remember me check box didn't work well and I couldn't know where the error please anyone help me this is my code
public partial class _Default : System.Web.UI.Page
{
public TextBox Usertxt,Passwordtxt;
protected void Page_Load(object sender, EventArgs e)
{
Usertxt = (TextBox)Login1.FindControl("UserName");
Passwordtxt = (TextBox)Login1.FindControl("Password");
if (!IsPostBack)
{
if (Request.Cookies["Mycookie"] != null)
{
HttpCookie Cookie = Request.Cookies.Get("Mycookie");
Usertxt.Text=Cookie.Values["UserName"];
Passwordtxt.Text=Cookie.Values["Password"];
}
}
}
protected void LoginButton_Click(object sender, EventArgs e)
{
Usertxt = (TextBox)Login1.FindControl("UserName");
Passwordtxt = (TextBox)Login1.FindControl("Password");
Literal LBL;
LBL = (Literal)Login1.FindControl("FailureText");
if (FormsAuthentication.Authenticate(Usertxt.Text, Passwordtxt.Text))
{
Response.Redirect("");
}
else
{
Login1.FindControl("FailureText");
LBL.Text = "error ocurred";
}
}
}
Thank you more I got the answer and this the right code
public partial class _Default : System.Web.UI.Page
{
public TextBox Usertxt, Passwordtxt;
public Literal LBL;
public CheckBox CHB;
protected void Page_Load(object sender, EventArgs e)
{
Usertxt = (TextBox)Login1.FindControl("UserName");
Passwordtxt = (TextBox)Login1.FindControl("Password");
if (!IsPostBack)
{
if (Request.Cookies["MyCookie"] != null)
{
HttpCookie Cookie = Request.Cookies.Get("MyCookie");
Usertxt.Text = Cookie.Values["UserName"];
Passwordtxt.Text = Cookie.Values["Password"];
}
}
}
protected void LoginButton_Click(object sender, EventArgs e)
{
Usertxt = (TextBox)Login1.FindControl("UserName");
Passwordtxt = (TextBox)Login1.FindControl("Password");
CHB = (CheckBox)Login1.FindControl("RememberMe");
LBL = (Literal)Login1.FindControl("FailureText");
HttpCookie MyCookie = new HttpCookie("MyCookie");
bool IsRememberme = CHB.Checked;
if (IsRememberme)
{
MyCookie.Values.Add("UserName", Usertxt.Text);
MyCookie.Values.Add("Password", Passwordtxt.Text);
MyCookie.Expires = DateTime.Now.AddDays(15);
}
else
{
MyCookie.Values.Add("UserName", string.Empty);
MyCookie.Values.Add("Password", string.Empty);
MyCookie.Expires = DateTime.Now.AddDays(5);
}
Response.Cookies.Add(MyCookie);
if (FormsAuthentication.Authenticate(Usertxt.Text, Passwordtxt.Text))
{
Response.Redirect("");
}
else
{
Login1.FindControl("FailureText");
LBL.Text = "error ocurred";
}
}
}

Resources