i++ keep on incrementing during page load - c#

i++ keep on increasing whenever I reload the page.It should only increment when I trigger the button but I found out that during page reload it also increments.
I did the !IsPostBack but I still encounter the problem.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
cart_number();
}
}
private static int i;
private void cart_number()
{
lbl_cart_number.Text = i++.ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
cart_number();
}

When you reload a page, it means that it's not a IsPostBack. You should remove cart_number(); from your Page_Load. The Page_Load will be triggered each time there is an interaction between the browser and the webserver.

Remove cart_number() method call from your 'Page_Load'. No need to call that method on Page_Load. Any specific reason why you call from Page_Load()?

Try this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
cart_number("1");
}
}
private static int i;
private void cart_number(string flag)
{
int lbl=0;
lbl =int.Parse(lbl_cart_number.Text);
if(flag!="1"){
i=lbl;
if(i>=0){
lbl_cart_number.Text =( i+1).ToString();
}
}
else
{
lbl_cart_number.Text ="0";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
cart_number("2");
}

Related

basic incremental on button click

I'm trying to write a simple button that when you click it adds 1 and updates the label but it only works on the first click after that it doesn't update the label anymore
protected void Page_Load(object sender, EventArgs e)
{ }
protected void ClickerButton_Click(object sender, ImageClickEventArgs e)
{
Lbl_PairsCooked.Text = CookSneaker(NumOfPairs).ToString();
}
public int CookSneaker(int num)
{
num += 1;
return num;
}
The image button i made only works on the first click...
See the snippet below on how to get it working. Most importantly you need to read on IsPostBack the link
https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.page.ispostback?view=netframework-4.8 to understand the difference.
public partial class _Default : Page
{
int NumOfPairs;
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
//this line reads the text from label, converts and assigns to NumPairs. The UI is updated on the button click which takes this new NumOfPairs.
int.TryParse(Label1.Text, out NumOfPairs);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = CookSneaker(NumOfPairs).ToString();
}
public int CookSneaker(int num)
{
num += 1;
return num;
}
}

Global Variable loses its value after page index changed in DataGrid

My problem is losing value.I have a DataGrid with standart asp.net pagination. When I change page index, global variable named "id" loses its value. Help me Please.
int id = 0;
void Payments()
{
radioBtnList = GetData();
radioBtnList.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
Payments();
Response.Write(id); // I get value 0 :(
}
protected void radioBtnList_Changed(object sender, EventArgs e)
{
id = int.Parse(radioBtnList.SelectedItem.Text);
}
protected void dgw_pagechange(object source, DataGridPageChangedEventArgs e)
{
dgw.CurrentPageIndex = e.NewPageIndex;
dgw.DataBind();
}
Instead of storing the ID in the page which will be reloaded when the page changes you should store it somewhere that will persist over the postback.
Examples might be:
The session object
A database
A text file
You can use ViewState like this.
int id = 0;
void Payments ()
{
radioBtnList = GetData();
radioBtnList.DataBind ();
}
protected void Page_Load (object sender, EventArgs e)
{
Payments();
Response.Write(ViewState["id"]);
}
protected void radioBtnList_Changed (object sender, EventArgs e)
{
id = int.Parse (radioBtnList.SelectedItem.Text);
ViewState["id"]=id;
}
protected void dgw_pagechange (object source, DataGridPageChangedEventArgs e)
{
dgw.CurrentPageIndex = e.NewPageIndex;
dgw.DataBind();
}

How can I go back to the previous page on ASP.Net and C#

Imagine I have a button which has " OnClick="GoBack" " I want it to go to the previous page, how will C# function code look like?
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
ViewState["RefUrl"] = Request.UrlReferrer.ToString();
}
}
protected void GoBack_Click(object sender, EventArgs e)
{
object refUrl = ViewState["RefUrl"];
if (refUrl != null)
{
Response.Redirect((string)refUrl);
}
}

Buttons to switch between months in ASP.NET

I'm developing a calendar for ASP.NET. I'm not using the Calendar control because it's quite limited.
I was wondering how can I programmatically switch between different months, for example, show a previous and a next month?
Now I get to change a month only once and then the month gets stuck: if July is shown first, then I can only get to June. When I'm on June and push the next month button, it shows me August. Would AJAX be a good choice to solve this problem?
My code:
private static DateTime now = DateTime.Today;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnPrev_Click(object sender, EventArgs e)
{
lblDateCal.Text = now.AddMonths(-1).ToString("MMMM");
}
protected void btnNext_Click(object sender, EventArgs e)
{
lblDateCal.Text = now.AddMonths(+1).ToString("MMMM");
}
Each time, you are using Now() to increment or decrement the month by one. You need to save your current month you are navigating to. E.g., save the last date you navigated to in the ViewState, and use that in your click events instead of Now().
for example:
protected DateTime UpdateDate(int offset)
{
DateTime dt;
if (ViewState["LastDate"] == null)
dt = DateTime.Now.AddMonths(offset);
else
dt = ((DateTime)ViewState["LastDate"]).AddMonths(offset);
ViewState["LastDate"] = dt;
return dt;
}
protected void btnPrev_Click(object sender, EventArgs e)
{
lblDateCal.Text = UpdateDate(-1).ToString("MMMM");
}
protected void btnNext_Click(object sender, EventArgs e)
{
lblDateCal.Text = UpdateDate(1).ToString("MMMM");
}
otherwise, if you prefer to use a static variable, then you need to utilize your static variable properly, by setting it each click. That is to say, the AddMonths() method does not implicitly modify your variable.
e.g.
protected void btnPrev_Click(object sender, EventArgs e)
{
now = now.AddMonths(-1);
lblDateCal.Text = now.ToString("MMMM");
}
protected void btnNext_Click(object sender, EventArgs e)
{
now = now.AddMonths(+1);
lblDateCal.Text = now.ToString("MMMM");
}
However, since static variables are global to the application, I would not think is the best approach.
Here is a good thread on that here: static variables in asp.net/C#
The reason its happening because you are using a static DateTime variable
please try this way
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnPrev_Click(object sender, EventArgs e)
{
lblDateCal.Text = DateTime.Today.AddMonths(-1).ToString("MMMM");
}
protected void btnNext_Click(object sender, EventArgs e)
{
lblDateCal.Text = DateTime.Today.AddMonths(+1).ToString("MMMM");
}

Calling code of Button from another one in C#

I need to know if it's possible to call the Click of a button from another one.
private void myAction_Click(object sender, EventArgs e)
{
// int x;
// ...
}
private void Go_Click(object sender, EventArgs e)
{
// call the myAction_Click button
}
Thanks.
You want:
private void Go_Click(object sender, EventArgs e)
{
myAction_Click(sender, e);
}
But a better design is to pull the code out:
private void myAction_Click(object sender, EventArgs e)
{
DoSomething();
}
private void Go_Click(object sender, EventArgs e)
{
DoSomething();
}
private void DoSomething()
{
// Your code here
}
The button has a PerformClick method.
http://msdn.microsoft.com/en-us/library/hkkb40tf(v=vs.90).aspx

Categories