在下拉列表中更改转发器内容更改的索引值

时间:2013-11-16 12:45:11

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

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

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());
            }


        } 

1 个答案:

答案 0 :(得分:2)

您必须进行AutoPostBack="true"下拉列表并绑定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
    }        
}