当用户在下拉列表中单击不同的选项时,更改页面的内容
本文关键字:选项 下拉列表 单击 用户 | 更新日期: 2023-09-27 18:15:04
我得到了一个下拉列表。用户在选择不同的值时,我应该从下拉列表中得到用户所选择的选项的选项文本,然后从数据库中找到数据,并在网页上显示相关内容。如果用户再次更改选项,我应该根据选择的选项文本去检索不同的数据,并在网页上显示不同的内容。
我怎样才能做到这一点?谁有什么好主意让我开始?我在网上查了一下,但找不到有用的东西。
这是我的代码:
ASPX文件:
<asp:Dropdownlist ID="SelectionDropDownList"
runat="server" Width="136px"
EnableViewState="True"
AppendDataBoundItems="true">
</asp:Dropdownlist>
CS文件:
//how the dropdown list is being populated out. Dropdown list is being populated out from what the user has selected from a listbox.
public void BindSomething()
{
DateTime choosenDate = DateTime.MinValue;
using (SqlConnection conn = new SqlConnection(dbConn))
{
using (SqlCommand cmd = new SqlCommand(spretrieve, conn))
{
//Lost to hold the values
List<DateTime> listCopy = new List<DateTime>();
DateTime dt;
string values = String.Join(", ", ListBox1.Items.Cast<ListItem>().Where(i => i.Selected).Select(i => i.Text));
if (values.Contains("Select All"))
{
//Loop through each items in listbox and then add it to list
foreach (ListItem li in ListBox1.Items)
{
if (DateTime.TryParse(li.Text, out dt))
{
listCopy.Add(dt);
}
}
}
else
{
//Loop through each items in listbox and then add it to list
foreach (ListItem li in ListBox1.Items)
{
//check if item is selected
if (li.Selected == true)
{
//add items to list
listCopy.Add(DateTime.Parse(li.Text));
}
}
}
//compare and sort so that the latest date comes on top
listCopy.Sort((x, y) => y.CompareTo(x));
//clear the items in dropdownlist
SelectionDropDownList.Items.Clear();
//set the datasource to dropdownlist
SelectionDropDownList.DataSource = listCopy;
//set the dateformatstring in dropdownlist
SelectionDropDownList.DataTextFormatString = "{0:dd-MMM-yyyy}";
//Bind the dropdownlist
SelectionDropDownList.DataBind();
}
如果有人能帮助我,我很感激,非常感谢!!
你可以尝试使用jQuery
$(document).ready(function() {
$('#SelectionDropDownList').change(function(){
//here you can add code for whatever you want to show
});
});