I want to display data in repeater based on the dropdownlist selected index. For example, when i choose "Help" item in dropdownlist the repeater will display the help categories content in database. Else choose Car will display car categories. May I know how can I control my 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());
}
}
You have to make AutoPostBack="true"
of dropdown and bind SelectedIndexChanged
event where you will get the selectedIndex and use that to bind the repeater.
Html
<asp:DropDownList ID="ddl" runat="server" AutoPostBack="True"
onselectedindexchanged="itemSelected">
</asp:DropDownList>
Code behind
protected void itemSelected(object sender, EventArgs e)
{
if(ddl.SelectedIndex == 1)
{
//Bind Help
}
else
if(ddl.SelectedIndex == 2)
{
//Bind Car
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.