Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 下拉列表日期格式_C#_Asp.net_Drop Down Menu - Fatal编程技术网

C# 下拉列表日期格式

C# 下拉列表日期格式,c#,asp.net,drop-down-menu,C#,Asp.net,Drop Down Menu,我使用DataTable在web表单上填充dropdownlist,并使用存储过程查询数据库。该列表仅从数据库检索一个字段,并将与gridview结合使用,以查看在特定日期进行的所有预订 除了日期的格式外,一切正常。现在它正在显示日期以及小时、分钟和秒。我曾尝试在web表单中使用DataFormatString属性,但没有成功 网络表单: <b>Please select a date:</b> <asp:DropDownList ID="ddlDateSe

我使用DataTable在web表单上填充dropdownlist,并使用存储过程查询数据库。该列表仅从数据库检索一个字段,并将与gridview结合使用,以查看在特定日期进行的所有预订

除了日期的格式外,一切正常。现在它正在显示日期以及小时、分钟和秒。我曾尝试在web表单中使用DataFormatString属性,但没有成功

网络表单:

  <b>Please select a date:</b>
  <asp:DropDownList ID="ddlDateSelection" runat="server" DataFormatString="{0:MM/dd/yyyy}"></asp:DropDownList>
  <br />
  <asp:Label ID="lblError" runat="server" CssClass="validate"></asp:Label>
请选择一个日期:

用于查询数据库的代码隐藏:

protected void Page_Load(object sender, EventArgs e)
    {
  if (!Page.IsPostBack)
    loadDates();
}

protected void loadDates()
{
  DataTable dates = new DataTable();

  using (SqlConnection con = new SqlConnection(DBAccess.GetConnectionString()))
  {
    SqlCommand cmd = new SqlCommand("usp_sel_ReservationDates", con);
    cmd.CommandType = CommandType.StoredProcedure;

    try
    {
      SqlDataAdapter adapter = new SqlDataAdapter(cmd);
      adapter.Fill(dates);
      ddlDateSelection.DataSource = dates;
      ddlDateSelection.DataTextField = "ReservationDate";
      ddlDateSelection.DataBind();
    }
    catch (Exception ex)
    {
      lblError.Text = "Something bad has happened.  Please try again.<br />Message: " + ex;
    }
  }
  ddlDateSelection.Items.Insert(0, new ListItem("Please Select", "0"));
}
受保护的无效页面加载(对象发送方,事件参数e)
{
如果(!Page.IsPostBack)
loadDates();
}
受保护的无效加载日期()
{
DataTable日期=新DataTable();
使用(SqlConnection con=newsqlconnection(DBAccess.GetConnectionString())
{
SqlCommand cmd=新的SqlCommand(“usp\u sel\u ReservationDates”,con);
cmd.CommandType=CommandType.storedProcess;
尝试
{
SqlDataAdapter=新的SqlDataAdapter(cmd);
填写日期;
ddlDateSelection.DataSource=日期;
ddlDateSelection.DataTextField=“ReservationDate”;
ddlDateSelection.DataBind();
}
捕获(例外情况除外)
{
lblError.Text=“发生了一些不好的情况。请重试。
消息:“+ex; } } ddlDateSelection.Items.Insert(0,新列表项(“请选择”,“0”)); }

任何帮助都将不胜感激,因为这是我自己的学习项目。谢谢

您可以编辑SQL代码以仅返回日期

SELECT CONVERT (DATE, GETDATE()) 

结果:2013-07-14


您可以编辑SQL代码以仅返回日期

SELECT CONVERT (DATE, GETDATE()) 

结果:2013-07-14


您可以编辑SQL代码以仅返回日期

SELECT CONVERT (DATE, GETDATE()) 

结果:2013-07-14


您可以编辑SQL代码以仅返回日期

SELECT CONVERT (DATE, GETDATE()) 

结果:2013-07-14


可能您需要引用正确的属性

 <asp:DropDownList ID="ddlDateSelection" runat="server" 
                   DataTextFormatString="{0:MM/dd/yyyy}">
 </asp:DropDownList>

您可能需要引用正确的属性

 <asp:DropDownList ID="ddlDateSelection" runat="server" 
                   DataTextFormatString="{0:MM/dd/yyyy}">
 </asp:DropDownList>

您可能需要引用正确的属性

 <asp:DropDownList ID="ddlDateSelection" runat="server" 
                   DataTextFormatString="{0:MM/dd/yyyy}">
 </asp:DropDownList>

您可能需要引用正确的属性

 <asp:DropDownList ID="ddlDateSelection" runat="server" 
                   DataTextFormatString="{0:MM/dd/yyyy}">
 </asp:DropDownList>

重复问题:看起来您想要短日期格式,尝试将DataFormatString更改为“{0:d}”。Sjramsay-这些是boundfield的属性,而不是dropdownlist…Evets Rezik-我尝试了该解决方案,但没有成功。重复问题:看起来您想要短日期格式,尝试将DataFormatString更改为简单的“{0:d}”。Sjramsay-这些是boundfield的属性,而不是dropdownlist…Evets Rezik-我尝试了该解决方案,但没有成功。重复问题:看起来您想要短日期格式,尝试将DataFormatString更改为简单的“{0:d}”。Sjramsay-这些是boundfield的属性,不是dropdownlist…Evets Rezik-我尝试过这个解决方案,但没有运气。重复问题:看起来你想要短日期格式,尝试将DataFormatString更改为简单的“{0:d}”。Sjramsay-这些是boundfield的属性,不是dropdownlist…Evets Rezik-我尝试过那个解决方案,但没有运气。谢谢Steve。成功了,谢谢史蒂夫。成功了,谢谢史蒂夫。成功了,谢谢史蒂夫。成功了。