我在下拉列表中有问题 [英] i had a problem in drop down list

查看:66
本文介绍了我在下拉列表中有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我有一个名为"addcategory"的表
其中有4个栏目类别,主题,问题,答案"
以aspx格式,我有两个下拉列表,一个用于添加类别,另一个用于广告主题
首先,我想在dropdown1中添加类别,然后从中选择一个类别,然后在dropdown2中添加相关主题,我还需要两个下拉列表中都没有重复的值,如果一个类别是该类别下的特定主题,也应该我可以在主题下拉列表中吗?请事先向我详细介绍thanx
我的编码如下:

 使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用使用System.Web.UI.WebControls;
使用 System.Data;
使用 System.Data.SqlClient;
使用 System.Configuration;


公共 部分  class  addexams:System.Web .UI.页面
{
     #region
 字符串 c = ConfigurationManager.ConnectionStrings [" ].ConnectionString;
     int  i;
     #endregion

    受保护的 无效 Page_Load(对象发​​件人,EventArgs e)
    {
        SqlConnection sc =  SqlConnection(c);
        sc.Open();
        SqlCommand scom =  SqlCommand(" ,sc);
        SqlDataReader d = scom.ExecuteReader();
        如果(d.HasRows)
        {
            同时(d.Read())
            {
                RemoveDuplicateItems(DropDownList1);
                DropDownList1.Items.Add(d.GetString( 0 ));

            }
        }

        sc.Close();
    }

      
    受保护的 无效 Button1_Click(对象发​​件人,EventArgs e)
    {
        Response.Redirect(" );
    }

    受保护的 无效 DropDownList1_SelectedIndexChanged(对象发​​件人,EventArgs e)
    {
        SqlConnection sc =  SqlConnection(c);
        sc.Open();
        SqlCommand scom =  SqlCommand(" ,sc);
        SqlDataReader d = scom.ExecuteReader();
        如果(d.HasRows)
        {
            同时(d.Read())
            {
               RemoveDuplicateItems(DropDownList2);
                DropDownList2.Items.Add(d.GetString( 0 ));

            }
        }

    //  sc.Close(); 
    // } 

    公共 静态  void  RemoveDuplicateItems(DropDownList ddl)
    {
         for ( int  i =  0 ; i <  ddl.Items.Count; i ++)
        {
            ddl.SelectedIndex = i;
            字符串 currValue = ddl.SelectedItem.ToString();
             for ( int 计数器= i +  1  ; counter <  ddl.Items.Count; counter ++)
            {
                ddl.SelectedIndex =计数器;
                如果(currValue == ddl.SelectedItem.ToString())
                {
                    ddl.Items.RemoveAt(counter);
                    计数器=计数器- 1 ;
                }
            }
        }
    }

} 



请纠正并发送正确的编码

解决方案

尝试一下,

受保护的void DropDownList1_SelectedIndexChanged(对象发送者,EventArgs e)
{
SqlConnection sc =新的SqlConnection(c);
sc.Open();
SqlCommand scom = new SqlCommand(从addexams中选择不同的主题,其中category =""+ DropDownList1.selectedItem.text +"'',sc);
SqlDataReader d = scom.ExecuteReader();
如果(d.HasRows)
{
而(d.Read())
{
RemoveDuplicateItems(DropDownList2);
DropDownList2.Items.Add(d.GetString(0));

}
}

sc.Close();
}

希望对您有帮助.
Yogesh Pednekar


使用"DISTINCT"关键字.
从addexams中选择不同的类别


hi i had a table named as ''addcategory''
in that there 4 cols ''category,subject,question,answer''
in aspx form i have two drop down lists one for add category and other to ad subject
first i want to add category in dropdown1 and from that i select one category then add the related subjects in dropdown2 i also need there is no repeated values in both drop down lists ,also if one category is the particular subjects which are under that category should be in the subject drop down list wt can i do ? plz hlp me in detailed thanx a lot in advance
my codings are below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


public partial class addexams : System.Web.UI.Page
{
    #region
    string c = ConfigurationManager.ConnectionStrings["eexam"].ConnectionString;
    int i;
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection sc = new SqlConnection(c);
        sc.Open();
        SqlCommand scom = new SqlCommand("select category from addexams", sc);
        SqlDataReader d = scom.ExecuteReader();
        if (d.HasRows)
        {
            while (d.Read())
            {
                RemoveDuplicateItems(DropDownList1);
                DropDownList1.Items.Add(d.GetString(0));

            }
        }

        sc.Close();
    }

      
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("add.aspx");
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection sc = new SqlConnection(c);
        sc.Open();
        SqlCommand scom = new SqlCommand("select category from addexams", sc);
        SqlDataReader d = scom.ExecuteReader();
        if (d.HasRows)
        {
            while (d.Read())
            {
               RemoveDuplicateItems(DropDownList2);
                DropDownList2.Items.Add(d.GetString(0));

            }
        }

    //    sc.Close();
    //}

    public static void RemoveDuplicateItems(DropDownList ddl)
    {
        for (int i = 0; i < ddl.Items.Count; i++)
        {
            ddl.SelectedIndex = i;
            string currValue = ddl.SelectedItem.ToString();
            for (int counter = i + 1; counter < ddl.Items.Count; counter++)
            {
                ddl.SelectedIndex = counter;
                if (currValue == ddl.SelectedItem.ToString())
                {
                    ddl.Items.RemoveAt(counter);
                    counter = counter - 1;
                }
            }
        }
    }

}



plz correct this and send the right codings

解决方案

Try this,

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection sc = new SqlConnection(c);
sc.Open();
SqlCommand scom = new SqlCommand("select distinct subjects from addexams where category = ''" + DropDownList1.selectedItem.text + "''", sc);
SqlDataReader d = scom.ExecuteReader();
if (d.HasRows)
{
while (d.Read())
{
RemoveDuplicateItems(DropDownList2);
DropDownList2.Items.Add(d.GetString(0));

}
}

sc.Close();
}

I hope this will help you.
Yogesh Pednekar


Use ''DISTINCT'' keyword.
select distinct category from addexams


这篇关于我在下拉列表中有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆