Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have 3 textbox.first two text box contains calander controls(from date/to date).i want to implement such a functionality that,as n when i select the second textbox date,total no of days should be displayed in the third textbox,that is (to-from).i have reached to this extent:

DateTime a, b;
a = Convert.ToDateTime(TextBox1.Text);
b = Convert.ToDateTime(TextBox2.Text);

System.TimeSpan span = b - a;
int ddays = (int)span.TotalDays;
textbox3.Text = Convert.ToString(ddays);


but,i am not able to display days in third textbox as i said before.The question is,where to implement this code such that i can get the desired output.Any help would be greatly appriciated.
Posted

You can aplly by using 2 way...
1) textbox text changed event
2) button click envet

C#
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
       <br />
       <asp:TextBox ID="TextBox4" runat="server" AutoPostBack="True"
           ontextchanged="TextBox4_TextChanged"></asp:TextBox>
       <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />
       <br />
       <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>





C#
protected void Button2_Click(object sender, EventArgs e)
    {
        DateTime a, b;
        a = Convert.ToDateTime(TextBox3.Text);
        b = Convert.ToDateTime(TextBox4.Text);

        System.TimeSpan span = b - a;
        int ddays = (int)span.TotalDays;
        TextBox5.Text = Convert.ToString(ddays);
    }
    protected void TextBox4_TextChanged(object sender, EventArgs e)
    {
        DateTime a, b;
        a = Convert.ToDateTime(TextBox3.Text);
        b = Convert.ToDateTime(TextBox4.Text);

        System.TimeSpan span = b - a;
        int ddays = (int)span.TotalDays;
        TextBox5.Text = Convert.ToString(ddays);
    }


If you find, it works for you please mark it as ans.

Thanks
Asp.Net/C#.Net help[^]
Hemant Singh
 
Share this answer
 
v2
Comments
Thanks7872 16-Jan-13 6:11am    
<td>
<asp:TextBox ID="TextBox1" Text="From" class="water" Tooltip="From"
runat="server">
<asp:CalendarExtender ID="CalendarExtender1" TargetControlID="TextBox1" Format="dd/MM/yyyy"
runat="server" />
</td>

<td>
<asp:TextBox ID="TextBox2" Text="To" class="water" Tooltip="To" runat="server" ontextchanged="TextBox2_TextChanged"
>
<asp:CalendarExtender ID="CalendarExtender2" TargetControlID="TextBox2" Format="dd/MM/yyyy"
runat="server" />
</td>
and the code is

protected void TextBox2_TextChanged(object sender, EventArgs e)
{
DateTime a, b;
a = Convert.ToDateTime(TextBox1.Text);
b = Convert.ToDateTime(TextBox2.Text);

System.TimeSpan span = b - a;
int ddays = (int)span.TotalDays;
d.Text = Convert.ToString(ddays);
}
further,i have used textbox which contains calender inside(onclicking textbox,shows calender),then where to place this code?i have tried all of the above solutions but not working for me...plz help...
Hemant Singh Rautela 16-Jan-13 6:13am    
try in TextBox2 control AutoPostBack="True"
Thanks7872 16-Jan-13 6:36am    
Thanks...your above answer works perfectly but it shows wrong answer....i mean from 1/1/2013 to 2/1/2013 should show 1 day,but it shows something like 31...?...oh...i need little help from your side...
Hemant Singh Rautela 16-Jan-13 6:48am    
This is not error,It occur due to date format Becuase default date format is mm/dd/yyyy
so that you get 31 days absoulty right ans.

...plz Dont forgot to mark as Ans...
I have tried with your code and it did worked for me. Try to add a break point on your code and check if the values are coming at your text box control or not ?
 
Share this answer
 
you can add code in second calendar Selection changed event

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

        }
 
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