I have answer for this ,this is one of the area which fastens the developement process bcoz we may be repeatdly using some components or controls in the entire project,suppose a gridview with some functionality or calendar control with some functinality or a adrotar or login control.
So developing of user control will be one time ,which we can repeadtly use in our entire application.Now let us see how to develop a web user control.
- Go to your visual studio File-> New-> Website-> give the name for yourproject and specify the location
- Now open the solution explorer(ctrl+w,s),right click on the project ->Add new item ->Now select Webuser control from the screen(the extenstion will be .ascx) name it to coustomcalendar.
- Now got to design view of coustomcalendar.ascx and drag a drop a calendar and on to your screen.Now your code should look below
--------------------------------------------------------------------------------------------------------
Now here we are making a calendar control which displays date on the label after you select the date from calendar.
Also it makes weekend selection true or false based on property we set
Now code to display date in the lable when date in calendar is selected will be in calendar selection changed event.Double click on calendar in coustomcalendar.ascx.
The code for the selection changed ecvent is as below
--------------------------------------------------------------------------------------------------------
protected void clndr_weekend_SelectionChanged(object sender, EventArgs e)
{
lbl_date.Text = clndr_weekend.SelectedDate.ToString("yyyy-MM-dd");
}
--------------------------------------------------------------------------------------------------------
Now am going to declare a property to make weekend selection true or false.
----------------------------------------------------------------------------------------------------------
private bool _isweekendselectable;
public bool Isweekendselectable
{
get { return _isweekendselectable; }
set { _isweekendselectable = value; }
}
--------------------------------------------------------------------------------------------------
I declared a property ,now i have to make the weekend dates enable true/false based on property.
This magic is done in dayrender event on calendar event.
----------------------------------------------------------------------------------------------------
protected void clndr_weekend_DayRender(object sender, DayRenderEventArgs e)
{
if (!Isweekendselectable)
{
if (e.Day.IsWeekend)
{
e.Cell.Controls.Clear();
e.Cell.Text = e.Day.DayNumberText;
}
}
}
---------------------------------------------------------------------------------------------------------
Now we are done with using control,let us summarize the steps
- Adding webusercontrol by going to new items
- Drag and Drop a claendar and label control
- Write a code in calendar selection changed event to display date in label.
- Create a property to make weekends enabled true/false
- Write a code in calendar dayrender event to make weekends enabled true/false
- Go to default.aspx->go to designe view->drag the coustomcalendar.ascx from solution explorer on to your page.
- Now go to properties(f4) and you can find property named IsWeekendselectable change it to true/false and test.
Happy coding ;)
No comments:
Post a Comment