如何使用下拉列表 [英] how to use drop down list

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

问题描述

我正在创建图库,我正在使用该页面上的下拉列表。如果用户从下拉列表中选择项目,我想根据sql server数据库中的下拉列表项目选择显示数据。我已经编写了以下代码但它不能正常工作。请帮助。



I am creating gallery and I am using drop down list on that page. I want to display data based on drop down list item selection from sql server database if user selects item from drop down list.I have written following code but it is not working.Please help.

protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
          
           bind1();
       }

   }







public void bind1()
   {
       ListItem item1 = new ListItem("Choose category", "1");
       ListItem item2 = new ListItem("Landscapes", "3");
       ListItem item3 = new ListItem("Nature", "4");
       ListItem item4 = new ListItem("People", "6");
       ListItem item5 = new ListItem("Seascapes", "8");
       ListItem item6 = new ListItem("Unique", "9");
       ListItem item7 = new ListItem("Popular", "7");
       ListItem item8 = new ListItem("Patterns", "5");
       ListItem item9 = new ListItem("Abstract", "2");
       ListItem item10 = new ListItem("Other", "10");
       ddlcategory.Items.Add(item1);
       ddlcategory.Items.Add(item9);
       ddlcategory.Items.Add(item2);
       ddlcategory.Items.Add(item3);
       ddlcategory.Items.Add(item8);
       ddlcategory.Items.Add(item4);
       ddlcategory.Items.Add(item7);
       ddlcategory.Items.Add(item5);
       ddlcategory.Items.Add(item6);
       ddlcategory.Items.Add(item10);
   }




protected void ddlcategory_SelectedIndexChanged(object sender, EventArgs e)
   {
       DataSet ds = new DataSet();
       DataTable dt = new DataTable();
       con.Open();

       string cmdr1 = "select arts.artsId,artdetails.mainImage from artdetails INNER JOIN arts ON artdetails.artsId=arts.artsId and Category='" + ddlcategory.SelectedIndex.ToString() + "' ";

       SqlCommand cmd = new SqlCommand(cmdr1, con);
       SqlDataAdapter da = new SqlDataAdapter(cmd);
       da.Fill(dt);
       DataList1.DataSource = dt;
       DataList1.DataBind();
     }

推荐答案

请执行以下操作以使代码正常工作..



1)请确保您已将下拉列表的 AutoPostBack 属性设置为true ..



2)在您的查询中,您编写了代码来比较类别与下拉列表的selectedIndex,这是不正确的。如果您要将类别与其名称进行比较,请编写 ddlcategory.SelectedItem.Text 代替 ddlcategory.SelectedIndex.ToString()。如果您要将类别与类别ID进行比较,请编写 ddlcategory.SelectedItem.Value)代替 ddlcategory.SelectedIndex.ToString()



Happy Coding :):)
Please implement the following things to make your code working..

1) Please ensure that you have set the AutoPostBack property of dropdownlist to true..

2) In your query, you have written code to compare category with the selectedIndex of dropdownlist which is not right.. If you are going to compare the category with it's name then please write ddlcategory.SelectedItem.Text in place of ddlcategory.SelectedIndex.ToString(). If you are going to compare category with category ID then please write ddlcategory.SelectedItem.Value)in place of ddlcategory.SelectedIndex.ToString()

Happy Coding :) :)


ddlcategory.SelectedIndex.ToString()给你选中项目索引,如果你选择第二项,那么你的 SelectedIndex 1 。是你预期的类别?如果不是,您需要将其更改为 ddlcategory.SelectedValue ddlcategory.SelectedItem.Text

要触发索引更改事件,您需要同时设置AutoPostBack属性和 OnSelectedIndexChanged 事件,如下所示

ddlcategory.SelectedIndex.ToString() give you the selected item index like if you select 2nd item then your SelectedIndex is 1. is that you expected as Category ? if not you need to change it to ddlcategory.SelectedValue or ddlcategory.SelectedItem.Text
to fire the index changed event you need to have both AutoPostBack property and OnSelectedIndexChanged event set correctly as below
<asp:DropDownList id="ddlcategory"
                  AutoPostBack="True"
                  OnSelectedIndexChanged="ddlcategory_SelectedIndexChanged"
                  runat="server"/>





您可以在几个方面改进您的代码/应用程序。如果数据库中的所有其他数据最好将类别移动到数据库并使用该表绑定下拉列表。例如。创建名为Category的表,然后你可以有id和Name字段。



there are few areas you can improve your code/application. If all other data in database you better move your categories to your database and bind the dropdownlist using that table. for example. create table called Category and you can have id and Name field then.

using(SqlConnection con = new SqlConnectio(conString)) 
using(SqlCommand cmd = new SqlCommand("Select Id, Name from Category", con))
{
    con.Open();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    ddlcategory.DataSource = dt;
    ddlcategory.DataTextField = "Id";
    ddlcategory.DataValueField = "Name";
    ddlcategory.DataBind();
}



您选择的索引更改事件可以通过更安全的参数化SQL更改如下




And your selected index changed event can be change as below with more secure parameterized SQL

protected void ddlcategory_SelectedIndexChanged(object sender, EventArgs e)
{
    string cmdr1 = "select arts.artsId,artdetails.mainImage from artdetails INNER JOIN arts ON artdetails.artsId=arts.artsId and arts.Category=@Category";
    using (SqlConnection con = new SqlConnection("connectionString"))
    using (SqlCommand cmd = new SqlCommand(cmdr1, con))
    {
        cmd.Parameters.AddWithValue("@Category", category); //set category value using ddlcategory.SelectedValue or ddlcategory.SelectedItem.Text depending on your data in Category column
        con.Open();
        using (SqlDataAdapter da = new SqlDataAdapter(cmd))
        {
            DataTable dt = new DataTable();
            da.Fill(dt);
            DataList1.DataSource = dt;
            DataList1.DataBind();
        }

    }
}


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

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