当DropDownList的SelectedValue为true时显示表C#

时间:2015-04-17 12:39:46

标签: c# asp.net drop-down-menu

我想在DropDownList ddlKlasse的SelectedValue为true时显示一个表。我使用带有母版页的asp.net空网页表单。 这个想法是:

  1. 当ddlKlasse.SelectedValue =" 2" table tblDubbelTwee必须显示
  2. 当ddlKlasse.SelectedValue =" 5"必须显示table tblVierMet
  3. 当ddlKlasse.SelectedValue =" 9"必须显示表tblAchtMet
  4. 我从数据库中获取了下拉列表中的值。

    我现在的代码是:

    在我的Page_Load中:

    protected void Page_Load(object sender, EventArgs e)
        {
            BindDropDownListKlasse();
            BindDropDownListVereniging();
    
    
            if (!Page.IsPostBack)
            {
                tblDubbelTwee.Visible = false;
                tblVierMet.Visible = false;
                tblAchtMet.Visible = false;
    
    
            }
        }
    

    用于绑定下拉列表:

    private void BindDropDownListKlasse()
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    using (SqlCommand com = new SqlCommand())
                    {
                        com.CommandText = "SELECT DISTINCT AantalDeelnemers, Naam FROM Klasse;";
                        com.Connection = conn;
                        conn.Open();
                        SqlDataAdapter da = new SqlDataAdapter(com);
                        DataTable dt = new DataTable();
                        da.Fill(dt);
                        ddlKlasse.DataSource = dt;
                        ddlKlasse.DataValueField = "AantalDeelnemers";
                        ddlKlasse.DataTextField = "Naam";
                        ddlKlasse.DataBind();
                        conn.Close();
    
                        //Adding "Kies de klasse" optie in dropdownlist voor validatie
                        ddlKlasse.Items.Insert(0, new ListItem("Kies de klasse", "0"));
                    }
                }
            }
            catch
            {
    
            }
        }
    

    用于显示表格:

    protected void ddlKlasse_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListItem selectedListItemDubbelTwee = ddlKlasse.Items.FindByValue("2");
            if (selectedListItemDubbelTwee != null)
            {
                selectedListItemDubbelTwee.Selected = true;
                tblDubbelTwee.Visible = true;
                tblVierMet.Visible = false;
                tblAchtMet.Visible = false;
            };
    
            ListItem selectedListItemVierMet = ddlKlasse.Items.FindByValue("5");
            if (selectedListItemVierMet != null)
            {
                selectedListItemVierMet.Selected = true;
                tblVierMet.Visible = true;
                tblDubbelTwee.Visible = false;
                tblAchtMet.Visible = false;
            };
    
            ListItem selectedListItemAchtMet = ddlKlasse.Items.FindByValue("9");
            if (selectedListItemAchtMet != null)
            {
                selectedListItemAchtMet.Selected = true;
                tblAchtMet.Visible = true;
                tblDubbelTwee.Visible = false;
                tblVierMet.Visible = false;
            };
        }
    

    我的DropDownList:

    <div class="form-inline">
        <div class="form-group">
            <asp:DropDownList ID="ddlKlasse" class="form-control" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlKlasse_SelectedIndexChanged"></asp:DropDownList>
        </div>
    </div>
    

    我的一张桌子:

    <asp:Table ID="tblDubbelTwee" runat="server" class="table">
        <asp:TableHeaderRow>
            <asp:TableHeaderCell>Naam</asp:TableHeaderCell>
            <asp:TableHeaderCell>Email</asp:TableHeaderCell>
            <asp:TableHeaderCell>Lidmaatschapsnr</asp:TableHeaderCell>
        </asp:TableHeaderRow>
    
        <asp:TableRow>
            <asp:TableCell>
                <asp:TextBox ID="txtNaam" type="text" class="form-control" runat="server"></asp:TextBox>
            </asp:TableCell>
            <asp:TableCell>
                <asp:TextBox ID="txtEmail" type="text" class="form-control" runat="server"></asp:TextBox>
            </asp:TableCell>
            <asp:TableCell>
                <asp:TextBox ID="txtLidmaatschapsnr" type="text" class="form-control" runat="server"></asp:TextBox>
            </asp:TableCell>
        </asp:TableRow>
        <asp:TableRow>
            <asp:TableCell>
                <asp:TextBox ID="txtNaam2" type="text" class="form-control" runat="server"></asp:TextBox>
            </asp:TableCell>
            <asp:TableCell>
                <asp:TextBox ID="txtEmail2" type="text" class="form-control" runat="server"></asp:TextBox>
            </asp:TableCell>
            <asp:TableCell>
                <asp:TextBox ID="txtLidmaatschapsnr2" type="text" class="form-control" runat="server"></asp:TextBox>
            </asp:TableCell>
        </asp:TableRow>
    </asp:Table>
    

    但是当我运行我的项目并在我的DropDownList中选择一个字段时,它会给出错误:System.Web.HttpException:不允许在DropDownList中选择多个项目。

    你能帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:1)

您当前的DDL索引更改处理程序正在执行与您所描述的完全不同的操作。它基本上检查列表项是否存在于ddl列表中(无论是否选中),然后尝试选择它。由于存在3个项目,因此您实际上是在尝试选择3个项目,这是不允许的。

您可能正在寻找的是按所选值切换:

switch (ddlKlasse.SelectValue)
{
    case "2":
            tblDubbelTwee.Visible = true;
            tblVierMet.Visible = false;
            tblAchtMet.Visible = false;
            break;
    case "5": //similar here
    case "9": //similar here
    default:
    // exception or something
}

答案 1 :(得分:0)

refere this code  
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindgrid();

            }
        }

        void bindgrid()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select DISTINCT Class from addmitionform1", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            ddlclass.DataSource = ds;
            ddlclass.DataTextField = "Class";
            ddlclass.DataValueField = "Class";
            ddlclass.DataBind();
            ddlclass.Items.Insert(0, new ListItem("--Select--", "0"));

        }

protected void ddlKlasse_SelectedIndexChanged(object sender, EventArgs e)
    {
          string id = (ddlclass.SelectedValue.ToString());
            con.Close();
            SqlDataAdapter da1 = new SqlDataAdapter("Select id,FirstName from addmitionform1 where Class= '" + id + "'", con);
            //SqlDataAdapter da1 = new SqlDataAdapter("Select * from addmitionform1 ", con);
            DataSet ds1 = new DataSet();
            da1.Fill(ds1);
            GridView1.DataSource = ds1;
            GridView1.DataBind();
    }

答案 2 :(得分:0)

首先在BindDropDownListKlasse()内移动BindDropDownListVereniging()if (!IsPostBack),正如HarveySpecter建议的那样。
并将ddlKlasse_SelectedIndexChanged()更改为:

protected void ddlKlasse_SelectedIndexChanged(object sender, EventArgs e)
{
    bool showDubbelTwee = false, showVierMet = false, showAchtMet = false;
    switch (ddlKlasse.SelectedValue)
    {
        case "2":
            showDubbelTwee = true;
            break;
        case "5":
            showVierMet = true;
            break;
        case "9":
            showAchtMet = true;
            break;
    }

    tblAchtMet.Visible = showVierMet;
    tblDubbelTwee.Visible = showDubbelTwee;
    tblVierMet.Visible = showVierMet;
}

您现在在ddlKlasse_SelectedIndexChanged()中正在执行的操作是检查下拉列表中是否存在每个值,如果存在,请选择它并显示相应的表格。
相反,您需要检查选择了哪个值并显示相应的表格。