简体   繁体   English

在asp.net后面的代码中获取引导程序dateTimePicker值

[英]Get bootstrap dateTimePicker value in code behind asp.net

I have the following dateTimePicker in my Asp.net project which works fine. 我的Asp.net项目中有以下dateTimePicker ,效果很好。 I now need to access the value of the selected date and time from my code behind. 现在,我需要从后面的代码中访问所选日期和时间的值。

<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>

Here is my JavaScript code to launch the dateTimePicker 这是我的JavaScript代码以启动dateTimePicker

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

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

As you can see I've added another function getEndDate() which I hoped would get the value and return it to my code behind when called. 如您所见,我添加了另一个函数getEndDate() ,希望该函数能够获取值并将其返回到调用后的代码中。

Here is my code behind where I'm trying to get the value. 这是我试图获取价值的代码。 As you can see I've tried two different ways to access the values of my dateTimePickers, neither works. 如您所见,我尝试了两种不同的方法来访问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);
}

You can't get the value on server side like this - the code is being run on the client and no way to send it to the server. 您无法像这样在服务器端获取值-代码正在客户端上运行,并且无法将其发送到服务器。

You can make the input to be runat="server" and access the value via Id of the control, or examine the Request collection during request. 您可以将input设置为runat="server"并通过控件的Id访问该值,或者在Request期间检查Request集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM