C# 更改dropdownlist中继器内容目录中的索引值

C# 更改dropdownlist中继器内容目录中的索引值,c#,asp.net,repeater,html-select,C#,Asp.net,Repeater,Html Select,我想根据dropdownlist选择的索引在中继器中显示数据。例如,当我在dropdownlist中选择“帮助”项时,中继器将在数据库中显示帮助类别内容。否则,选择汽车将显示汽车类别。我可以知道如何控制我的下拉列表吗 if (!IsPostBack) { try { MySqlConnection connStr = new MySqlConnection(); con

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

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


        } 

您必须创建下拉列表的
AutoPostBack=“true”
,并绑定
SelectedIndexChanged
事件,从中获取selectedIndex并使用它绑定中继器

Html


嗯~对不起。。。是否我必须为每个selectedIndex编写select查询?因为我有10个类别。
<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
    }        
}