Set Properties and create events for a user control
I'm creating a web user control in asp.net using C# in which i can select a date from a calendar and display it in a textbox. when i select a date from the calender it ha开发者_运维百科s to be displayed in the textbox. now i need to set my own properties by which i can select datetime patterns in cs codefile. for example
usercontrol1.dd-mm-yyyy.
this is one example. now i want all the datetime patterns of "en-us". when i use that usercontrol in another page i want to set any of the properties(datetime patterns) to that control. please help me!!!
i tried this coding but no use... plz review it and give me solution
public partial class DateControl : System.Web.UI.UserControl {
string dateformat;
public string Dateformat
{
get { return dateformat;}
set { dateformat = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
PageLoad();
lnlbtnChangeDate.Visible = false;
ddlDateFormat.Visible = false;
Calendar.Visible = false;
}
lblError.Visible = false;
}
public void PageLoad()
{
if (txtBoxDate.Text != "")
{
Calendar.Visible = false;
}
CultureInfo ci = new CultureInfo("fr-fr");
string[] format = ci.DateTimeFormat.GetAllDateTimePatterns();
foreach (string i in format)
{
ddlDateFormat.Items.Add(i);
}
}
protected void lnkbtnPickDate_Click(object sender, EventArgs e)
{
Calendar.Visible = true;
lnlbtnChangeDate.Visible = true;
ddlDateFormat.Visible = false;
}
public void Calendar_SelectionChanged1(object sender, EventArgs e)
{
txtBoxDate.Text = Calendar.SelectedDate.ToShortDateString();
}
protected void ddlDateFormat_SelectedIndexChanged(object sender, EventArgs e)
{
txtBoxDate.Text = Calendar.SelectedDate.ToString(ddlDateFormat.SelectedValue.ToString());
}
protected void lnlbtnChangeDate_Click(object sender, EventArgs e)
{
Calendar.Visible = false;
if (txtBoxDate.Text == "")
{
lblError.Visible = true;
}
else
{
lblError.Visible = false;
lnlbtnChangeDate.Visible = true;
ddlDateFormat.Visible = true;
}
}
protected void lnkbtnClear_Click(object sender, EventArgs e)
{
txtBoxDate.Text = "";
Calendar.Visible = false;
lnlbtnChangeDate.Visible = false;
ddlDateFormat.Visible = false;
lblError.Visible = false;
}
i said that i want set properties for my user control and create events for that.... plz help me
Not sure I get it right at the question is not very clear, but anyway :
You could just create Properties for you user controls, and assign Enums to them
public enum My_UserControl_DateFormats
{
YYYYMMDD = 1,
YYYYMMDDHH = 2,
YYYYMMDDHHmmSS = 3
}
And in the setter code of your properties handle the logic to assign the Date Format (For Example) according to the enum value (using switch/case)
It's one among many possibilities.
精彩评论