【问题标题】:Change Index value in dropdownlist the repeater content cahnge更改下拉列表中的索引值转发器内容更改
【发布时间】:2013-11-16 12:45:11
【问题描述】:

我想根据下拉列表选择的索引在转发器中显示数据。例如,当我在下拉列表中选择“帮助”项时,转发器将在数据库中显示帮助类别内容。否则选择汽车将显示汽车类别。我可以知道如何控制我的下拉列表吗?

if (!IsPostBack)
        {
            try
            {
                MySqlConnection connStr = new MySqlConnection();
                connStr.ConnectionString = "Server = localhost; Database = healthlivin; Uid = root; Pwd = khei92;";
                String stSearch = "SELECT t.title, p.userName, t.threadID FROM thread t , person p WHERE p.PersonID = t.PersonID AND t.categories = @categories";
                MySqlCommand cmdSearch = new MySqlCommand(stSearch, connStr);
                cmdSearch.Parameters.AddWithValue("@categories", categoriesDropDownList.SelectedValue);

                connStr.Open();

                MySqlDataReader dtrRead = cmdSearch.ExecuteReader();
                categoriesRepeater.DataSource = dtrRead;
                categoriesRepeater.DataBind();
                dtrRead.Close();
                dtrRead = null;
                //MessageBox.Show("Connected");
                connStr.Close();
            }
            catch (Exception ex)
            {
                //MessageBox.Show("X Connected" + ex.ToString());
            }


        } 

【问题讨论】:

    标签: c# asp.net repeater html-select


    【解决方案1】:

    您必须创建AutoPostBack="true" of dropdown 并绑定SelectedIndexChanged 事件,您将在其中获取 selectedIndex 并使用它来绑定转发器。

    HTML

    <asp:DropDownList ID="ddl" runat="server" AutoPostBack="True" 
        onselectedindexchanged="itemSelected">
    </asp:DropDownList>
    

    背后的代码

    protected void itemSelected(object sender, EventArgs e)
    {
        if(ddl.SelectedIndex == 1)
        {
            //Bind Help   
        }        
        else
        if(ddl.SelectedIndex == 2)
        {
            //Bind Car
        }        
    }
    

    【讨论】:

    • 嗯~对不起...我必须为每个 selectedIndex 编写选择查询吗?因为我有 10 个类别。
    猜你喜欢
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多