显示总数没有。两个日期之间的天数 [英] displaying total no. of days between two dates
问题描述
我有3个textbox.first两个文本框包含calander控件(从日期/到日期)。我想实现这样的功能,当我选择第二个文本框日期时,应显示总天数第三个文本框,即(来自).i达到了这个程度:
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);
但是,我无法像之前所说的那样在第三个文本框中显示日期。问题是,在哪里实现此代码,以便我可以获得所需的输出。任何帮助都将是非常感兴趣。
你可以通过2种方式使用...
1)文本框文本更改事件
2)按钮单击envet
< asp:TextBox ID = TextBox3 runat = server > < / asp:TextBox >
< br />
< asp:TextBox ID = TextBox4 runat = server AutoPostBack = True
ontextchanged = TextBox4_TextChanged > < / asp:TextBox >
< asp:按钮ID = Button2 runat = server onclick = Button2_Click Text = 按钮 />
< br />
< asp:TextBox ID = TextBox5 runat = server > < / asp:TextBox >
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);
}
受保护 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);
}
如果您发现,它适合您,请将其标记为ans。
谢谢
Asp.Net/C#.Net帮助 [ ^ ]
Hemant Singh
< blockquote>我已经尝试过你的代码,它确实对我有用。尝试在代码上添加断点并检查值是否来自文本框控件?
您可以在第二个日历中添加代码选择更改的事件
受保护 void Calendar2_SelectionChanged ( object sender,EventArgs e)
{
}
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.
You can aplly by using 2 way...
1) textbox text changed event
2) button click envet
<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>
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
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 ?
you can add code in second calendar Selection changed event
protected void Calendar2_SelectionChanged(object sender, EventArgs e) { }
这篇关于显示总数没有。两个日期之间的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!