adding number of days in calendar extender - asp.net

I have been working on a project and I got stuck with a problem i.e I am able to select first and last date using asp.net calendar control but i want it to be done with calendar extender
the code shown as below is for calender control ..please provide me the solution for calendar extender with required modification..............
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
Calendar1.SelectionMode = CalendarSelectionMode.DayWeek;
//ArrayList selectedDates = new ArrayList();
string s = Calendar1.SelectedDate.ToShortDateString();
DateTime today = DateTime.Parse(s);
DateTime firstDay = today.AddDays(-(double)(today.DayOfWeek));
DateTime lastDay = today.AddDays(6 - (double)(today.DayOfWeek));
for (int loop = 0; loop < 7; loop++)
Calendar1.SelectedDates.Add(firstDay.AddDays(loop));
lblStartOfWeek.Text = firstDay.ToLongDateString();
lblEndOfWeek.Text = lastDay.ToLongDateString();
lblStartOfWeek.Visible = true;
lblEndOfWeek.Visible = true;
checkLessonPlan();
}

protected void Calendar1_ontextchanged(object sender, EventArgs e)
{
string numberofdays= Convert.ToDateTime(this.TextBox1.Text).DayOfWeek.ToString("d");
DateTime select = Convert.ToDateTime(TextBox1.Text);
lblStartOfWeek.Text = select.AddDays(-Convert.ToInt32(numberofdays)).ToString("dd MMMM yyyy");
lblEndOfWeek.Text = select.AddDays(6-Convert.ToInt32(numberofdays)).ToString("dd MMMM yyyy");
}

Related

Asp.Net Calendar DateTime and Hour

I need to be able to select both the date and the time from an ASP.Net Calendar Control, (instead of just the date). How do I accomplish this?
This is my code so far:
protected void btnEkle_Click(object sender, EventArgs e) {
DateTime selected = Tarih.SelectedDate;
DateTime dt = new DateTime(selected.Year, selected.Month, selected.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
using (KargoTakipEntities kargo = new KargoTakipEntities()) {
KargoTakipSorgu kargoekle = new KargoTakipSorgu { Tarih = dt, }
}
}

Can't dynamically add controlvalidation to textbox

After several searches on the Internet finally decided to drop a question.
At runtime I want the following to happen.
If you push a button a textbox will be added to the controls and a custom validator must be attached to the textbox and fire.
It does not fire. Why?
Here is my code
Thank you for looking into this.
public partial class WebUserControl1 : System.Web.UI.UserControl
{
CustomValidator rv2 = new CustomValidator();
protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
{
rv2.ID = "rev2";
rv2.ErrorMessage = "Not numeric input";
rv2.ClientValidationFunction = "";
rv2.ServerValidate += new ServerValidateEventHandler(GetalValidator);
Controls.Add(rv2);
}
}
protected void ButtonClick(object sender, EventArgs args)
{
TextBox tb1 = new TextBox();
tb1.ID = "tb2";
tb1.Visible = true;
tb1.Width = 30;
this.Controls.Add(tb1);`enter code here`
rv2.EnableClientScript = false;
rv2.ControlToValidate = "tb2";
}
private bool IsNumber(string someText)
{
int number;
bool result = Int32.TryParse(someText, out number);
return result;
}
protected void GetalValidator(object source, ServerValidateEventArgs args)
{
args.IsValid = IsNumber(args.Value);
}
}
here is part of code thorugh which you can add validation on your dynamically created control.
TextBox tb1 = new TextBox();
tb1.ID = "tb2";
tb1.Visible = true;
tb1.Width = 30;
RequiredFieldValidator regfv = new RequiredFieldValidator();
regfv.ID = "regfv";
regfv.ControlToValidate = tb1.ID;
regfv.ErrorMessage = "My Error";
this.Controls.Add(tb1);
this.Controls.Add(regfv);

highlight selected week of asp.net calendar

I need to highlight the week on base of selected date in asp.net calendar. As we can specify style for the selected date, do asp.net calendar have option to specify style for the entire week of the selected date?
You can use calendar_dayrender event
public void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
e.Day.IsSelectable = false;
DataRow[] rows = Tables.Select(
String.Format(
"Date='{0}' and Month='{1}' and Year='{2}'",
e.Day.Date.Day,
e.Day.Date.Month,
e.Day.Date.Year
)
);
foreach (DataRow row in rows)
{
System.Web.UI.WebControls.Image image;
image = new System.Web.UI.WebControls.Image();
if (somecondition)
{
e.Cell.CssClass = "cssclass";
}
}

how to retain the value of global string variable even after page load in asp.net

I am having problems in retaining the string variable which I defined on the top of my scoop, everytime when page loads the string value becomes null. below is the snippet of the code:
public partial class Caravan_For_Sale : System.Web.UI.Page
{
string check;
PagedDataSource pds = new PagedDataSource(); //paging
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
DataTable dt = null;
switch (check)
{
case "0-1500":
break;
case "1500-2000":
dt = caravans.GetFilterbyPrice1();
break;
case "2000+":
break;
default:
dt = caravans.GetAllCaravans();
break;
}
// DataTable dt = caravans.GetAllCaravans();
pds.DataSource = dt.DefaultView;
pds.AllowPaging = true;
pds.PageSize = 3;//add the page index when item exceeds 12 //Convert.ToInt16(ddlPageSize.SelectedValue);
pds.CurrentPageIndex = CurrentPage;
DataList1.RepeatColumns = 3; // 4 items per line
DataList1.RepeatDirection = RepeatDirection.Horizontal;
DataList1.DataSource = pds;
DataList1.DataBind();
lnkbtnNext.Enabled = !pds.IsLastPage;
lnkbtnPrevious.Enabled = !pds.IsFirstPage;
doPaging();
}
protected void lnkPrice2_Click(object sender, EventArgs e)
{
LinkButton _sender = (LinkButton)sender;
check = _sender.CommandArgument;
// items["test"] = test;
DataTable dt = caravans.GetFilterbyPrice2();
if (dt.Rows.Count < 3)
{
lnkbtnNext.Enabled = false;
lnkbtnPrevious.Enabled = false;
}
CurrentPage = 0;
BindGrid();
}
protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName.Equals("lnkbtnPaging"))
{
CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
BindGrid();
}
}
The string check becomes null everytime when the dlPaging_ItemCommand becomes active(page loads). Any help or suggestions will be appreciated
As far as I know, you have two options:
1) Load it again.
Not sure if it's possible in your case. This is usually done when dealing with database queries.
2) Put it in the ViewState just like this:
ViewState["check"] = check;
And load it after with this:
string check = Convert.ToString(ViewState["check"]);
Your class is instantiated on every load so it will not have a global variable from page view to page view. You will need to store it somehow. Like in the querystring or a session. You can also use the viewstate.
For example
ViewState("Variable") = "Your string"
Viewstate is the way to go, as the other people have answered. Whatever you do, please don't stuff it in the session.

Asp.net calendar select current week in page load event

In the Page_Load event I would like to have the current week selected on the calendar instead of just the current day. How can this be done?
<asp:Calendar
ID="Calendar1"
runat="server"
ondayrender="Calendar1_DayRender"
SelectionMode="DayWeek"
onselectionchanged="Calendar1_SelectionChanged">
</asp:Calendar>
protected void Page_Load(object sender, EventArgs e)
{
Calendar1.SelectedDate = System.DateTime.Today;
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
e.Day.IsSelectable = false;
}
Give this a try:
Calendar1.SelectionMode = CalendarSelectionMode.DayWeek;
ArrayList selectedDates = new ArrayList();
DateTime today = DateTime.Now;
DateTime firstDay = today.AddDays(-(double)(today.DayOfWeek));
for (int loop = 0; loop < 7; loop++)
Calendar1.SelectedDates.Add(firstDay.AddDays(loop));

Resources