disable sunday in asp.net calender - asp.net

How to disable Sunday in asp.net calendar control? I am using C#.

You can create a method for the dayrender event in the control. Something like this:
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date.DayOfWeek != DayOfWeek.Sunday) return;
e.Cell.ApplyStyle(new Style { BackColor = System.Drawing.Color.Gray });
e.Day.IsSelectable = false;
}

Related

Disable Future Dates on asp:calendar control

How do I disable future dates on an asp:calendar control?
For example, today is October 22 2013, I want to disable dates from October 23 onward,
and re-enable them when it is the current date.
You will need to use the DayRender method.
C# Code Behind
public void Calendar1_DayRender(object o, DayRenderEventArgs e)
{
e.Cell.BackColor = System.Drawing.Color.Empty;
e.Cell.ForeColor = System.Drawing.Color.DarkRed;
//if the days are not part of the current month
if (e.Day.IsOtherMonth)
{
e.Cell.Text = "";
}
}
Then in the calendar, you need to place
ASP
<asp:Calendar ID="Calendar1" runat="server" Height="145px" Width="77px" OnDayRender="Calendar1_DayRender"></asp:Calendar>
You add the OnDayRender="Calendar1_DayRender" to the calendar control.
Hope this helps!
EDIT: I misread that. But if you want to just to "enable" the current day and no other days, then this will work..
Code Behind
public void Calendar1_DayRender(object o, DayRenderEventArgs e)
{
e.Cell.BackColor = System.Drawing.Color.Empty;
e.Cell.ForeColor = System.Drawing.Color.DarkRed;
if (e.Day.IsToday)
{
e.Cell.BackColor = System.Drawing.Color.Blue;
}
else
{
e.Cell.Text = "";
}
}
You can use this trick to disable future dates in Calendar Control.
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date > DateTime.Today)
{
e.Day.IsSelectable = false;
}
}

Disable GridViewColoum on Page_Load

I have a Delete-Button in my gridview that i used to create with an TemplateField + LinkButton + OnRowCommand.
Now a normal user should not be able to use this button - or better not to see this button at all.
How to disable a coloumn in a gridView on the page Log event?
Try this:
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// get the column here and your condition to make that disabled
e.Row.Cells[index].Visible = false;
}
}
You can also hide like:
((DataControlField)gridView.Columns
.Cast<DataControlField>()
.Where(fld => (fld.HeaderText == "Title"))
.SingleOrDefault()).Visible = false;
Use this: column visible use before bind grid otherwise error occurring.
protected void Page_Load(object sender, EventArgs e)
{
gridView.DataSource = "yourDatasource";
gridView.DataBind();
gridView.Columns[ColumnIndex].Visible =false;
}
Try this
In your Page_Load
GridView1.Columns[0].Visible = false;
then column 0 of the grid become disable and the other column of the grid resize automatically.

Dropdown selected change event fire on button Click event

I have write the code in .Net. When I click the Buttun then fired below event. Please help how to do this.
protected void ddldesignation_SelectedIndexChanged(object sender, EventArgs e)
{
Code.
}
Check if the button is in the page's form collection, you can use that as a condition to stop your drop down list event firing.
protected void ddldesignation_SelectedIndexChanged(object sender, EventArgs e)
{
bool runEventCode = true;
foreach (string ctl in Page.Request.Form)
{
Control c = Page.FindControl(ctl);
if (c is Button)
{
runEventCode = false;
break;
}
}
if (runEventCode)
{
//Event handler code here
}
}

Control Add PostBack Problem

I Add Control Dynamiclly but; easc Postback event my controls are gone. I Can not see again my controls.
So How can I add control ?
Because you must recreate your controls on every postback,
see this article
Add the controls in the Page's Init event and they will be preserved in viewstate when posting back. Make sure they have a unique ID.
See this link...
ASP.NET Add Control on postback
A very trivial example..
public partial class MyPage : Page
{
TextBox tb;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
tb = new TextBox();
tb.ID = "testtb";
Page.Form.Controls.Add(tb);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//tb.Text will have whatever text the user entered upon postback
}
}
You should always assign a unique ID to the UserControl in its ID property after control is loaded. And you should always recreate UserControl on postback.
To preserve posback data (i.e. TextBox'es) you must load UserControl in overriden LoadViewState method after calling base.LoadViewState - before postback data are handled.
Add controls in runtime and save on postback:
int NumberOfControls = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["b1"] = 0;
}
else
{
if ((int)ViewState["b1"] > 0)
{
CreateBtn();
}
}
}
protected void btn1_Click(object sender, EventArgs e)
{
NumberOfControls = (int)ViewState["b1"];
Button b1 = new Button();
// b1.Attributes.Add("onclick", "x()");
b1.Text = "test2";
b1.ID = "b1_" + ++NumberOfControls;
b1.Click +=new System.EventHandler(btn11);
Panel1.Controls.Add(b1);
ViewState["b1"] = NumberOfControls;
}
protected void CreateBtn()
{
for (int i = 0; i < (int)ViewState["b1"];i++)
{
Button b1 = new Button();
// b1.Attributes.Add("onclick", "x()");
b1.Text = "test2";
b1.ID = "b1_" + i;
b1.Click += new System.EventHandler(btn11);
Panel1.Controls.Add(b1);
}
}
protected void btn11(object sender, System.EventArgs e)
{
Response.Redirect("AboutUs.aspx");
}

CheckedChanged event for Dynamically generated Checkbox column in DataGrid(Asp.Net)

I have a datagrid (Asp.Net) with dynamically generated checkbox column..I am not able to generate the checkedChanged event for the checkbox..
Here is my code:
public class ItemTemplate : ITemplate
{
//Instantiates the checkbox
void ITemplate.InstantiateIn(Control container)
{
CheckBox box = new CheckBox();
box.CheckedChanged += new EventHandler(this.OnCheckChanged);
box.AutoPostBack = true;
box.EnableViewState = true;
box.Text = text;
box.ID = id;
container.Controls.Add(box);
}
public event EventHandler CheckedChanged;
private void OnCheckChanged(object sender, EventArgs e)
{
if (CheckedChanged != null)
{
CheckedChanged(sender, e);
}
}
}
and Here is the event
private void OnCheckChanged(object sender, EventArgs e)
{
}
Thanks In advance
When do you add your custom column? If it is on load, then it is too late. Load it on init. I.e. following works with your code:
protected void Page_Init(object sender, EventArgs e)
{
ItemTemplate myTemplate = new ItemTemplate();
myTemplate.CheckedChanged += new EventHandler(myTemplate_CheckedChanged);
TemplateField col = new TemplateField();
col.ItemTemplate = myTemplate;
col.ItemStyle.Wrap = false;
grid.Columns.Add(col);
}
If your checkbox ID's are not being set the same way on every postback, then they can never be connected to the event handlers when it comes time to process the events. Where is your field "id" coming from?

Resources