Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Need to generate the day name as a disabled text on a form each time I select a date from the date calendar text i.e. txt_Date on a form


I tried this, but had an error txt_Date.Text;


C#
protected void txt_Date_TextChanged(object sender, EventArgs e)
        {

            //String Test;

            DateTime Test = txt_Date.Text;
            DayOfWeek dow = Test.DayOfWeek;
            txt_Day.Text = dow.ToString();


        }

Any help please

Thanks

What I have tried:

I tried this, but had an error txt_Date.Text;

C#
protected void txt_Date_TextChanged(object sender, EventArgs e)
        {

            //String Test;

            DateTime Test = txt_Date.Text;
            DayOfWeek dow = Test.DayOfWeek;
            txt_Day.Text = dow.ToString();


        }
Posted
Updated 5-Oct-18 18:52pm
v2

Since Text is a valid property of many controls, and specifically of the TextBox and Label controls - which are the ones you probably want to use for this - and you don't tell us what the error message is, the best guess is many fold:
1) You don't have any control called txt_Date. Create one.
2) txt_Date is not a Label or TextBox. find out what it is.
3) You can't just assign a string value to a DateTime - that doesn't work any more than you can push a square peg into a round hole. You need to convert it to a DateTime value first, preferably with validation.
C#
DateTime test;
if (!DateTime.TryParse(txt_Date.Text, out test))
   {
   ... report input problem to user ...
   return;
   }

If you are using a DateTimePicker to select the date, then us the Value property, which is already a DateTime:
C#
DateTime test = txt_Date.Value;
But change the damn name because that isn't a text based control...
 
Share this answer
 
C#
protected void cal_SelectionChanged(object sender, EventArgs e)
        {
            txtDate.Text = cal.SelectedDate.Date.ToShortDateString();
            txtDay.Text = cal.SelectedDate.Date.DayOfWeek.ToString();
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900