在后面的代码中获取 bootstrap dateTimePicker 值 asp.net

Get bootstrap dateTimePicker value in code behind asp.net

我的 Asp.net 项目中有以下 dateTimePicker,效果很好。我现在需要从后面的代码中访问所选日期和时间的值。

<label for="couponDatetimepickerEnd">Coupon Valid To</label>
<br/>
<div class="row">
  <div class="col-sm-6">
    <div class="form-group">
      <div class="input-group date" id="couponDatetimepickerEnd" runat="server">
        <input type="text" class="form-control" />
        <span class="input-group-addon">
          <span class="glyphicon glyphicon-calendar"></span>
        </span>
      </div>
    </div>
  </div>
</div>

这是我的 JavaScript 代码,用于启动 dateTimePicker

<script type="text/javascript">
  $(document).ready(function () {
    $('#couponDatetimepickerEnd').datetimepicker({
      sideBySide: true
    });
  });

  function getEndDate() {
    return $('#couponDatetimepickerEnd').val()
  };
</script>

如您所见,我添加了另一个函数 getEndDate(),我希望它能获取值并 return 在调用时将其添加到我的代码中。

这是我试图获取值的代码。如您所见,我尝试了两种不同的方法来访问我的 dateTimePickers 的值,但都不起作用。

protected void setCouponOk_Click(object sender, EventArgs e)
{
    var offerStartString = ScriptManager.RegisterStartupScript(
        this, 
        this.GetType(), 
        "startDate", 
        "getStartDate();", 
        true);

    var offerEndString = ScriptManager.RegisterStartupScript(
        this, 
        this.GetType(), 
        "endDate", 
        "getEndDate();", 
        true);

    //var offerStartString = Page.Request.Form["couponDatetimepickerStart"];
    //var offerEndString = Page.Request.Form["couponDatetimepickerEnd"];
    DateTime offerStart = DateTime.Parse(offerStartString);
    DateTime offerEnd = DateTime.Parse(offerEndString);
}

您无法像这样在服务器端获取值 - 代码在客户端 运行 无法将其发送到服务器。

您可以将 input 设为 runat="server" 并通过控件的 Id 访问该值,或者在请求期间检查 Request 集合。