简体   繁体   中英

Get bootstrap dateTimePicker value in code behind asp.net

I have the following dateTimePicker in my Asp.net project which works fine. 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

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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