Как отобразить все данные в виде сетки на основе выбранных элементов списка флажков, где данные вида сетки имеют внешний ключ, который является первичным ключом для элементов списка флажков?Ниже приведен мой код разработки:
<%@ Page Title="" Language="C#" MasterPageFile="~/User/User.master" AutoEventWireup="true" CodeFile="AddCompanyDetail.aspx.cs" Inherits="User_AddCompanyDetail" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<%-- javascript code --%>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('[id*=CheckBox2]').click(function () {
$("[id*='CheckBox1']").attr('checked', this.checked);
});
});
</script>
<%-- --%>
<br />
<div>
<asp:Label ID="Label5" runat="server" Text="We are: " Font-Underline="True" Font-Bold="True" Font-Size="Medium"></asp:Label>
</div>
<br />
<div>
<asp:CheckBox ID="chkBoxConsultant" AutoPostBack="true" Text="Consultant" runat="server" OnCheckedChanged="chkBoxConsultant_CheckedChanged" />
<asp:CheckBox ID="chkBoxSupplier" AutoPostBack="true" Text="Supplier" runat="server" OnCheckedChanged="chkBoxSupplier_CheckedChanged" />
</div>
<br />
<asp:Panel ID="panelService" Visible="false" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Our Services: " Font-Underline="True" Font-Bold="True" Font-Size="Medium"></asp:Label>
</div>
<br />
<div>
<asp:CheckBoxList ID="CheckBoxList2" AutoPostBack="true" RepeatDirection="Vertical" runat="server" OnSelectedIndexChanged="CheckBoxList2_SelectedIndexChanged">
</asp:CheckBoxList>
</div>
</asp:Panel>
<br />
<asp:Panel ID="panelItem" Visible="false" runat="server">
<div>
<asp:Label ID="Label2" runat="server" Text="Our Items: " Font-Bold="True" Font-Size="Medium" Font-Underline="True"></asp:Label>
</div>
<br />
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" BorderColor="White" CellPadding="15" CellSpacing="10">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" Text="Select All" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Id" InsertVisible="False" ShowHeader="False" SortExpression="Id" Visible="False">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Item" ShowHeader="False" SortExpression="ItemTitle">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("ItemTitle") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="ServiceId" ShowHeader="False" SortExpression="ServiceId" Visible="False">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("ServiceId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ServiceTitle" ShowHeader="False" SortExpression="ServiceTitle" Visible="False">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("ServiceTitle") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Brand Name">
<ItemTemplate>
<asp:TextBox ID="TextBox9" placeholder="Type Brand Name here" runat="server"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Range">
<ItemTemplate>
<asp:TextBox ID="TextBox10" placeholder="Type Range here" runat="server"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Catalogue">
<ItemTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="True" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="BOQ Specification">
<ItemTemplate>
<asp:FileUpload ID="FileUpload2" runat="server" AllowMultiple="True" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</asp:Panel>
</asp:Content>`
Ниже приведен код cs: Моя проблема заключается в том, что всякий раз, когда я проверяю элемент «CheckBoxList2», Gridview1 отображает только данные, относящиеся к этому, ранее проверенный элемент неотображается.Кроме того, когда я снимаю флажок со всех отмеченных элементов, просмотр сетки не очищается, и остаются данные, относящиеся к последнему проверенному элементу.Что я могу сделать, чтобы решить эту проблему?
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class User_AddCompanyDetail : System.Web.UI.Page
{
mepbddbEntities db = new mepbddbEntities();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void chkBoxConsultant_CheckedChanged(object sender, EventArgs e)
{
var data = db.Services.ToList();
if (chkBoxConsultant.Checked==true)
{
panelService.Visible = true;
CheckBoxList2.DataSource = data;
CheckBoxList2.DataTextField = "ServiceTitle";
CheckBoxList2.DataValueField = "Id";
CheckBoxList2.DataBind();
}
else if (chkBoxConsultant.Checked == false && chkBoxSupplier.Checked==true)
{
panelService.Visible = true;
panelItem.Visible = true;
}
else if (chkBoxConsultant.Checked == false && chkBoxSupplier.Checked == false)
{
panelService.Visible = false;
panelItem.Visible = false;
}
}
protected void chkBoxSupplier_CheckedChanged(object sender, EventArgs e)
{
var data = db.Services.ToList();
if (chkBoxSupplier.Checked == true)
{
panelService.Visible = true;
CheckBoxList2.DataSource = data;
CheckBoxList2.DataTextField = "ServiceTitle";
CheckBoxList2.DataValueField = "Id";
CheckBoxList2.DataBind();
}
else if (chkBoxSupplier.Checked == false && chkBoxConsultant.Checked==true)
{
panelService.Visible = true;
panelItem.Visible = false;
}
else if (chkBoxSupplier.Checked == false && chkBoxConsultant.Checked == false)
{
panelService.Visible = false;
panelItem.Visible = false;
}
}
protected void CheckBoxList2_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ListItem li in CheckBoxList2.Items)
{
var getitems = li.Value;
int i = Convert.ToInt32(getitems);
var data = db.SupplierItems.Where(d => d.ServiceId == i).ToList();
foreach (var item in data)
{
if (li.Selected && chkBoxSupplier.Checked)
{
panelItem.Visible = true;
GridView1.DataSource = data;
GridView1.DataBind();
}
}
}
}
}
Если кому-то нужны таблицы базы данных, я также могу опубликовать скрипт таблицы.