ASP.NET将多个复选框值传递给SELECT查询

时间:2017-04-21 13:35:01

标签: asp.net

我正在根据可能的模块选择显示学生的时间表。每次选中一个复选框时,我想通过选择的值“ModuleId”在SELECT查询中使用,以显示所有选定模块的时间表。因此,如果用户选中3个复选框,则所选的每一行中的“ModuleId”将被传递到SELECT查询中。

我不知道如何存储每个选定的“ModuleId”并将其添加到我的选择查询中。

以下是我检索选中值的方法:

<asp:TemplateField>
    <ItemStyle HorizontalAlign="Center" Width="40px"></ItemStyle>
          <ItemTemplate>
             <asp:CheckBox ID="chkRow" runat="server" ToolTip='<%# Eval("ModuleId") %>' OnCheckedChanged="module_Changed" />
         </ItemTemplate>
<asp:TemplateField>

下面是我在标签中显示值的方法(仅用于测试目的):

     protected void module_Changed(object sender, EventArgs e)
      {
         // Retrieve the check box ModuleId value to add to my SELECT query
         string moduleid = ((CheckBox)sender).ToolTip;
     }

下面是我的方法,其中包含用于显示时间表的选择查询:

 public String[] getModulesAtCurrentSlot(int timeslotInt, String moduleID, String Day)
        {
            List<String> modulesList = new List<string>();
            if (conn.State.ToString() == "Closed")
            {
                conn.Open();
            }
            SqlCommand newCmd = conn.CreateCommand();
            newCmd.Connection = conn;
            newCmd.CommandType = CommandType.Text;
            newCmd.CommandText = "SELECT DISTINCT Module.ModuleCode,ClassType.ClassTypeName,Convert(time,Class.StartTime), Convert(time,Class.EndTime),Building.BuildingName,RoomCode.RoomCode,Class.Color" +
                     " FROM Class INNER JOIN Module ON Class.ModuleId = Module.ModuleId INNER JOIN RoomCode ON Class.RoomCodeId = RoomCode.RoomcodeId INNER JOIN Building ON RoomCode.BuildingId = Building.BuildingId INNER JOIN Days ON Class.DayId = Days.DayID INNER JOIN ClassType ON Class.ClassTypeId = ClassType.ClassTypeId WHERE " +
                     " Module.ModuleId = " + moduleID + " AND Convert(Date,StartTime) = '" + Day + "' AND " + timeslotInt.ToString() + " BETWEEN ClassScheduleStartTimeId and ClassScheduleEndTimeId";
            SqlDataReader dr = newCmd.ExecuteReader();
            while (dr.Read())
            {
                String current = "<div class='slot' " + (!dr.IsDBNull(6) ? "style=\"background-color: " + dr.GetString(6) + ";\"" : "") + ">";
                current += "<div class='line1'>" + dr.GetString(0) + "&nbsp;" + dr.GetString(1) + "</div>";// +"<br />";
                current += "<div class='line2'>" + dr.GetTimeSpan(2).ToString().TrimEnd('0').TrimEnd('0').TrimEnd(':') + "&nbsp;-&nbsp;" + dr.GetTimeSpan(3).ToString().TrimEnd('0').TrimEnd('0').TrimEnd(':') + "</div>";// +"<br />";
                current += "<div class='line3'>" + dr.GetString(4) + "&nbsp;" + dr.GetString(5) + "</div>";
                current += "</div>";
                modulesList.Add(current);
            }
            conn.Close();
            return modulesList.ToArray();
        }

在上一页上,时间表只显示一个ModuleId的数据,我使用下面的查询字符串来传递值。

 String module_ID = "2";
        if (Request.QueryString["module"] != null)
        {
            module_ID = Request.QueryString["module"];
        }
        else
        {
            Response.Write("Missing ?module=XX from url :(");
            Response.End();// EndRequest;
        }

DBAccess.cs截图: DBAccess.cs screenshot

错误截图: Errors screenshot

1 个答案:

答案 0 :(得分:1)

如果我理解正确,你可以有一个会话变量,其中包含一个包含所有选定moduleID的字符串列表

然后你可以使用module_Changed事件从这个List中添加或删除moduleID,然后在循环中为列表中的每个moduleid调用getModulesAtCurrentSlot,并将返回的字符串[] s连接成一个更长的字符串[]或List然后显示

下面的代码中可能存在一些错误,因为我只是从内存中执行此操作,但它应该会给你一个想法!

protected void module_Changed(object sender, EventArgs e)
{
    List<string> lst;
    if( Session["lst"]!=null)
        lst = (List<string>)Session["lst"];
    else
           Session.Add("lst", new List<string>());


     // Retrieve the check box ModuleId value to add to my SELECT query
     string moduleid = ((CheckBox)sender).ToolTip;

    // add your own code to check if checkbox is checked or unchecked to see if you need to add or remove the ID from the list

     // to add
     if(lst.Contains(moduleid) == false)
         lst.Add(moduleid);

     // to remove - add your own code

     List<string> lstResult = new List<string>();
     foreach(var moduleID in lst)
     {
         lstResult.Add(getModulesAtCurrentSlot(timeslotInt, moduleID, Day));
     }

     // do something to display lstResult
     // e.g. drag a Gridview control on your aspx page and bind the results list to it - this is just to give you a rough idea but you'll need to play around with it to get it to work as you want, and hopefully learn something in the process ;)
     Gridview1.DataSource = lstResult;
     Gridview1.Databind();

 }