在CalendarExtender的selectedDate上执行select Query

时间:2015-07-14 08:01:52

标签: javascript sql asp.net ajax asp.net-ajax

我想在asp.net中对CalendarExtender的selectedDate值执行select查询。 我有一个虚拟按钮(可见=假)并在日历Extendar上执行按钮点击事件,借助

  

OnClientDateSelectionChanged ="&了checkdate#34;

和javascript函数" checkDate"是

  

<script type="text/javascript">
>         function checkDate(sender, args) {
>             var Clientdate = sender._selectedDate;
>             __doPostBack('Button1', '');
>         } </script>

按钮点击事件后面的文件后面的代码是

> protected void Button1_Click(object sender, EventArgs e)
>         {
>             //txtDate.Text = Convert.ToString(CalendarExtender1.SelectedDate);
>             String cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
>             using (SqlConnection con = new SqlConnection(cs))
>             {
>                 gvAlreadyAllocated.Visible = true;
>                 SqlCommand cmd = new SqlCommand("select Date,EmpName,TimeSlot,Topic,ClassroomNo from tblTimeSlotDetails where
> Date=@CalenderDate", con);
>                 con.Open();
>                 cmd.Parameters.AddWithValue("@CalenderDate",);
>                 using (SqlDataReader rdr = cmd.ExecuteReader())
>                 {
>                     DataTable dt = new DataTable();
>                     dt.Columns.Add("Date");
>                     dt.Columns.Add("Name");
>                     dt.Columns.Add("TimeSlot");
>                     dt.Columns.Add("Topic");
>                     dt.Columns.Add("Classroom Number");
> 
>                     while (rdr.Read())
>                     {
>                         DataRow dataRow = dt.NewRow();
> 
>                         dataRow["Date"] = Convert.ToString(rdr["Date"]);
> 
>                         dataRow["Name"] = rdr["Empname"];
>                         dataRow["TimeSlot"] = rdr["TimeSlot"];
>                         dataRow["Topic"] = rdr["Topic"];
>                         dataRow["Classroom Number"] = rdr["ClassroomNo"];
>                         dt.Rows.Add(dataRow);
>                     }
>                     //gvAlreadyAllocated.Columns[0].HeaderText = "Already allocate data for this Date";
>                     gvAlreadyAllocated.DataSource = dt;
>                     gvAlreadyAllocated.DataBind();
> 
>                     //gvAlreadyAllocated.DataSource = rdr;
>                     //gvAlreadyAllocated.DataBind();
>                 }
>             }
>         }

但是如何将值传递给@Calenderdate参数?

  

cmd.Parameters.AddWithValue(&#34; @ CalenderDate&#34;,);

我想显示一个gridview,其中我的selectedDate与数据库的日期匹配。

1 个答案:

答案 0 :(得分:0)

您需要在date和您要匹配的value中投放列

SqlCommand cmd = new SqlCommand("select Date,EmpName,TimeSlot,Topic,ClassroomNo from tblTimeSlotDetails where cast(Datecol as Date)=cast(@CalenderDate as Date),con

参数值将是calenderextender选定日期的值。

cmd.Parameters.AddWithValue("@CalenderDate",Convert.ToString(CalendarExtender1.SelectedDate));