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");
}
Related
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();
}
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);
}
}
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");
}
this doesn't work
protected void ods_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
{
if (#Session["PaginaAnterior"] == #"http://www.francawdesign.com.br/veiculos/paginas/anunciar/carro/identificacao.aspx")
Response.Redirect("~/paginas/anunciar/carro/identificacao.aspx");
}
this on the other hand works
protected void ods_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
{
if (#"http://www.francawdesign.com.br/veiculos/paginas/anunciar/carro/identificacao.aspx" == #"http://www.francawdesign.com.br/veiculos/paginas/anunciar/carro/identificacao.aspx")
Response.Redirect("~/paginas/anunciar/carro/identificacao.aspx");
}
this also doesn't work myGlobalVariable has the same values as the string it compares
protected void ods_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
{
if (#myGlobalVariable == #"http://www.francawdesign.com.br/veiculos/paginas/anunciar/carro/identificacao.aspx")
Response.Redirect("~/paginas/anunciar/carro/identificacao.aspx");
}
since a long time I have this doubt. My guess is that this features are not available at the time of this event. Is that correct?
hello i m doing a very simple Asp.net application project
namespace WebApplication1
{
public partial class WebUserControl1 : System.Web.UI.UserControl
{
market m = new market();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void button_clickSell(object sender, EventArgs e)
{
float price = float.Parse(this.BoxIdPrezzo.Text);
m.insertProd("xxx", 10, "yyy");
m.addOfferForProd("ooo", 5, "gggg");
m.insertProd(this.BoxIdDescrizione.Text,price,this.BoxIdUtente.Text);
String s;
m.outMarket(out s);
this.Output.Text = s; //the output here work good
this.Output.Visible = true;
}
protected void button_clickView(object sender, EventArgs e)
{
String s;
m.outMarket(out s);
this.Output.Text = s; // here seem to have lost the reference to product why?
this.Output.Visible = true;
}
}
}
the problem is that when i click on button1 which call button_clickSell everything works good but when i click on button2 which call button_clickView products seem to not be anymore in the Market object, but this is pretty strange because in market object i have a list of product and m.outMarket in the first time work propely.
That is because of how pages work. Every time you make a request or a post-back to the page the values will be lost in that variable.
You will need to hold that in a session or something similar.
Here is a very basic example of using a session.
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Collection"] == null)
{
Session["Collection"] = new List<int>();
}//if
}
protected void button_clickSell(object sender, EventArgs e)
{
List<int> collection = (List<int>)Session["Collection"];
collection.Add(7);
collection.Add(9);
}
protected void button_clickView(object sender, EventArgs e)
{
List<int> collection = (List<int>)Session["Collection"];
collection.Add(10);
}
you can view this post on MSDN: ASP.NET Session State Overview
Session should be used when information is required across the
pages. Now the matter for the two buttons lying on the same page. So
ViewState is Best option.
protected void Page_Load(object sender, EventArgs e)
{
if (ViewState["Collection"] == null)
{
ViewState["Collection"] = new List<int>();
}//if
}
protected void button_clickSell(object sender, EventArgs e)
{
List<int> collection = (List<int>)ViewState["Collection"];
collection.Add(7);
collection.Add(9);
}
protected void button_clickView(object sender, EventArgs e)
{
List<int> collection = (List<int>)ViewState["Collection"];
collection.Add(10);
}