处理dropdownlist. [英] Dealing with dropdownlist .

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

问题描述

我需要一些帮助,我有2个下拉列表

在第一个中有一些值,当我在第一个滴中选择值时,我需要用第二个滴中的值填充与我选择的内容相关的内容

只是如何在第一滴中编写此代码,代码将是什么?????



thanx alot

I need some help I have 2 dropdownlist

in the first one there''s some values ,I need when I choose value in the first drop the second drop fill with the things related to what I selected


just how to write this code in first drop what the code will be ?????



thanx alot

推荐答案

尝试一下,

Try this,

<asp:dropdownlist autopostback="true" id="dd1" runat="server" onselectedindexchanged="dd_select_changed" xmlns:asp="#unknown">

       </asp:dropdownlist>
       <asp:dropdownlist id="dd2" runat="server" xmlns:asp="#unknown">
       </asp:dropdownlist>



CS



CS

 protected void Page_Load(object sender, EventArgs e)
   {
connection();
       string sql = "select * from tblCountry ";
       cmd = new SqlCommand(sql, conn);
       da = new SqlDataAdapter(cmd);
       da.Fill(ds);


           for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
           {
               dd1.DataSource = ds.Tables[0];
               dd1.DataBind();
               dd1.DataTextField = "CountryName";

               dd1.DataValueField = "CountryName";
           }
  }


protected void dd_select_changed(object sender, EventArgs e)
   {
       connection();
       string sql2 = "select * from tblCity where Country='" + dd1.SelectedItem.Value.ToString() + "'";
       cmd = new SqlCommand(sql2, conn);
       da = new SqlDataAdapter(cmd);
       DataSet ds2 = new DataSet();
       da.Fill(ds2);
           dd2.DataSource = ds2.Tables[0];
           dd2.DataBind();
       dd2.DataTextField = "CityName";
       dd2.DataValueField = "CityName";
   }


tblCountry


tblCountry

CountryName

India
US
UK



tblCity



tblCity

CityName        Country

London	        UK
Delhi	        India
san francisco	US


请使用以下解决方案:

Please use below solution:

<div>
       <asp:dropdownlist id="ddl1" runat="server" autopostback="true" xmlns:asp="#unknown">
           onselectedindexchanged="ddl1_SelectedIndexChanged">
           <asp:listitem text="1" value="1"></asp:listitem>
           <asp:listitem text="2" value="2"></asp:listitem>
       </asp:dropdownlist>
       <asp:dropdownlist id="ddl2" runat="server" xmlns:asp="#unknown">
       </asp:dropdownlist>
   </div>



在后面的代码



at code behind

protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Now here you can get selected value.
        //ddl1.SelectedItem.Text  

        if (ddl1.SelectedItem.Text == "1")
        { 
        
            ddl2.Items.Insert(0,new ListItem("test1","test1"));
            ddl2.Items.Insert(0,new ListItem("test21","test21"));
        }
        else if (ddl1.SelectedItem.Text == "2")
        {

            ddl2.Items.Insert(0, new ListItem("test2", "test2"));
            ddl2.Items.Insert(0, new ListItem("test22", "test22"));
        }

        // And also you can fill it using  data table or get the value from the database

    }




让我知道该解决方案为您服务的方法:)




Let me know this solution work for you :)


在ASP .NET中,您可以使用下面的JQuery代码. Itz通用解决方案.

In ASP .NET, you can use the below JQuery code. Itz generic solution.

<select id="mainDropDown" onchange="getSubcatValue()">
     //Options for mainDropDown list
</select>

<select id="subDropDown"></select>

<script type="text/javascript>
    function getSubcatValue()
    {
        var chosenValue =


这篇关于处理dropdownlist.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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