![](/img/trans.png)
[英]How to get hidden field value in a html button in code behind in asp.net
[英]how to get datepicker value in code behind in asp.net webforms on button click?
我已經為開始日期和結束日期啟動了日期選擇器,並且嘗試了以下操作,但是對我來說不起作用的是獲取空值。
.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>
您只需要閱讀文本框的Text屬性:
string startDate = StartDate.Text;
string endDate = EndDate.Text;
或者,如果您想使用DateTime變量:
DateTime startDate = Convert.ToDateTime(StartDate.Text);
DateTime endDate = Convert.ToDateTime(EndDate.Text);
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.