C# 我需要在选择下拉列表值时,复选框控件应在asp.net中动态更改

C# 我需要在选择下拉列表值时,复选框控件应在asp.net中动态更改,c#,asp.net,sql-server-2008,C#,Asp.net,Sql Server 2008,我们的团队使用SQL server作为后端。在web页面上,我们有DropdownList中的值,这些值动态地反映在表的流列上,其中包含各种流(艺术、商业、科学等)的数据。 现在,在DDL中选择流时,我们需要提供复选框选项,仅显示与所选流相关的主题。但我们所有的主题都在另一张表的一列中。如何仅检索流的相关主题 以下是aspx代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherit

我们的团队使用SQL server作为后端。在web页面上,我们有DropdownList中的值,这些值动态地反映在表的流列上,其中包含各种流(艺术、商业、科学等)的数据。 现在,在DDL中选择流时,我们需要提供复选框选项,仅显示与所选流相关的主题。但我们所有的主题都在另一张表的一列中。如何仅检索流的相关主题

以下是aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:DropDownList ID="ddlStream" runat="server" DataSourceID="SqlDataSource1" DataTextField="Branch_Name" DataValueField="Branch_Name" AutoPostBack="True" OnSelectedIndexChanged="ddlStream_SelectedIndexChanged">
        </asp:DropDownList>
        <br />
        <br />
        <asp:CheckBoxList ID="chkSubjects" runat="server" DataSourceID="SqlDataSource2" DataTextField="SUBJECT" DataValueField="SUBJECT">
        </asp:CheckBoxList>
        <br />
        <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource2">
            <Columns>
                <asp:BoundField DataField="SUBJECT" HeaderText="SUBJECT" SortExpression="SUBJECT" />
            </Columns>
        </asp:GridView>
        <br />
        <br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ERPConnectionString %>"  SelectCommand="SELECT [Branch_Name] FROM [Branch_Master]"></asp:SqlDataSource>
        <br />
        <br />
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ERPConnectionString %>" SelectCommand="SELECT [SUBJECT] FROM [Course_Master]"></asp:SqlDataSource>

    </div>
    </form>
</body>
</html>








所有主题都在一列中,而不是在单独的列中,这就是导致问题的原因。是否需要基于流创建多个列?这不能通过所有主题的一个公共列来完成吗? 我最初尝试在DDL和复选框的扩展上配置数据源选项,但它检索列中的所有数据

谢谢你帮助我更好地理解我的问题并更好地构建它。你需要更多的投入吗

问候,, 桑托什很简单

您必须使用dropdownlist的选定索引更改事件

  • 假设您的dropdownlist名称为ddlStream

  • 假设您的复选框列表名为chkStream

  • 将dropdownlist的autopostback属性设置为TRUE

     protected void ddlStream_SelectedIndexChanged(object sender, EventArgs e)
       {          
        SqlConnection Con = new SqlConnection("connectionString");
        string query = "select [column name] from [table name] where [column name] ="+ddlStream.SelectedItem.Value;
    
        SqlCommand com = new SqlCommand(query, Con);
        chkStream.DataValueField = "Columnname";
        chkStream.DataTextField = "Columnname";
        conn.Open();
        DataReader reader1 = comm.ExecuteReader();
        chkStream.DataSource = reader1;
        chkStream.DataBind();   
      }
    

可能需要包含代码才能获得这方面的帮助,但我可以说,对于类似的问题,我将所有选项都放在客户端代码中,然后使用javascript动态切换页面。您可以挂接SelectedIndexChanged事件,进行一些服务器端处理,以获取复选框值并设置它们。然而,正如@Hogan所建议的那样,在客户机上使用jquery和ajax会更好/更干净。