我如何填充我的下拉列表 [英] How Can I Populate My Dropdownlist

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

问题描述

DROPDOWNLIST1: i have a table say "cat" with columns-id,category and categoryid... i successfully populated its value from the database.
DROPDOWNLIST2: Also have another table called "products" with columns-id,name,categoryid.i need to populate my DROPDOWNLIST2 based on categoryid i retrieve from DROPDOWNLIST1

我是用C#做的,但我有一个问题,即每次我在页面加载后在DROPDOWNLIST1中选择一个选项,只有那时DROPDOWNLIST2的值被更新。我需要避免这种情况,因此我需要javascript中的相同代码。对javascrpt.please新的帮助我

这里是我的工作代码

i did it using C#,but i have one problem ie each time i choose an option in DROPDOWNLIST1 after the page load, only then the value of DROPDOWNLIST2 is updated.i need to avoid this hence i need code for the same in javascript.im new to javascrpt.please help me
here is my working code

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            bindcatdropdown();

        }
    }
    protected void bindcatdropdown()
    {

        DataTable table = new DataTable();
        using (SqlConnection con = new SqlConnection(constr))
        {
            string sql = "select category,categoryid from cat ORDER BY categoryid";
            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    da.Fill(table);
                }
            }

        }
        DropDownList1.DataSource = table;
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, new ListItem("--Select--", "0"));
       }




protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
       DataTable table = new DataTable();
       //int id = Convert.ToInt32(DropDownList1.SelectedValue);
       int id = int.Parse(DropDownList1.SelectedValue.ToString());
       using (SqlConnection con = new SqlConnection(constr))
       {
           string sql = "select name,categoryid from products WHERE categoryid=@cid ORDER BY categoryid";
           using (SqlCommand cmd = new SqlCommand(sql, con))
           {
               using (SqlDataAdapter da = new SqlDataAdapter(cmd))
               {
                   cmd.Parameters.AddWithValue("@cid", id);
                   da.Fill(table);
               }
           }

       }
       DropDownList2.DataSource = table;
       DropDownList2.DataBind();
   }

推荐答案





按照我的理解,你正在创建级联下拉列表



请参考以下链接:

http://www.aspdotnet-suresh.com/2013/10/jquery-cascading-dropdown-list-in- aspnet.html [ ^ ]

我以前读过这个。这是一个很好的。



还要检查这些:

http://stackoverflow.com/questions/7437381/how-to-set-selected-value-of-dropdownlist-with-jquery [ ^ ]



http://aspsnippets.com/Articles/AJAX-Cascading-DropDownList-using-jQuery-in-ASP.Net.aspx [ ^ ]



供参考: http://api.jquery.com/change/ [ ^ ]



希望这会对你有所帮助。 :)



问候,

Praneet
Hi,

As per my understanding, you are creating cascading dropdownlist

Please refer to the following links:
http://www.aspdotnet-suresh.com/2013/10/jquery-cascading-dropdown-list-in-aspnet.html[^]
I read this sometime ago. It is a good one.

Also check these out:
http://stackoverflow.com/questions/7437381/how-to-set-selected-value-of-dropdownlist-with-jquery[^]

http://aspsnippets.com/Articles/AJAX-Cascading-DropDownList-using-jQuery-in-ASP.Net.aspx[^]

For Reference : http://api.jquery.com/change/[^]

Hope this helps you. :)

Regards,
Praneet


你不能用JavaScript做到这一点。 br />


您有两种选择。您可以使用Ajax Cascading DropDownList或对Web方法背后的代码执行jQuery Ajax调用。在里面,绑定你的DropDownList。



1. Ajax Cascading Dropdownlist Sample with database with asp.net [ ^ ]

2. 使用AJAX WEBMETHOD / 从DATATABLE中删除BROP DOWNDOWNLIST [ ^ ]
You can't do this using JavaScript.

You have two options. You can go for Ajax Cascading DropDownList or do a jQuery Ajax call to the Code behind Web Method. Inside that, bind your DropDownList.

1. Ajax Cascading Dropdownlist Sample with database using asp.net[^]
2. BIND DROPDOWNLIST FROM DATATABLE USING AJAX WEBMETHOD/[^]


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

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