我已经为开始日期和结束日期启动了日期选择器,我已经尝试了以下内容,但它不适用于我的空白值。
.cs代码
protected void MyButton_Click(object sender, EventArgs e)
{
BeforeDate = Convert.ToDateTime(Page.Request.Form["StartDate"]);
CurrentDate = Convert.ToDateTime(Page.Request.Form["EndDate"]);
string dt = StartDate.Text;
}
.aspx代码
<div class="DatePicker" style="height:30px;background-color:white !important;">
<div class="col-md-4 col-md-offset-2">
<asp:TextBox ID="StartDate" name="StartDate" runat="server" ReadOnly = "true" placeholder="Start Date" CssClass="form-control"></asp:TextBox>
</div>
<div class="col-md-4">
<asp:TextBox ID="EndDate" name="EndDate" runat="server" ReadOnly = "true" placeholder="End Date" CssClass="form-control"></asp:TextBox>
</div>
<div class="col-md-2">
<asp:Button ID="MyButton" runat="server" Text="Click" CommandArgument="YourArgumentValue" OnClick="MyButton_Click"/>
</div>
答案 0 :(得分:1)
您只需要阅读文本框的Text属性:
string startDate = StartDate.Text;
string endDate = EndDate.Text;
或者如果您想使用DateTime变量:
DateTime startDate = Convert.ToDateTime(StartDate.Text);
DateTime endDate = Convert.ToDateTime(EndDate.Text);
答案 1 :(得分:0)
String startDate = StartDate.Text.ToString();
String endDate = EndDate.Text.ToString();
通过提供ID和runat="server"
,您可以在后面的代码中使用所有html控件。
您应该将ID
和name
attributes
分开,不要给它们相同的价值。
还可以尝试将CausesValidation="False"
添加到按钮中。
编辑:
通过阅读您的评论您必须将变量强制转换为TextBox:
String startDate = (StartDate as TextBox).Text.ToString();