用户在DropDown列表中单击不同选项时更改页面内容 [英] Change the Content of the Page When User Click On A Different Option On DropDown List

查看:65
本文介绍了用户在DropDown列表中单击不同选项时更改页面内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉列表。用户在选择不同的值后,我应该从下拉列表中选择用户选择的选项文本,然后从数据库中查找数据并在网页上显示相关内容。如果用户再次更改选项,我应该根据所选的选项文本检索不同的数据,并在网页上显示不同的内容。



我怎么能做到这一点?有人有任何想法让我开始吗?我在网上搜索但找不到任何有用的东西。



这是我的代码:

 // Aspx文件:
< asp:下拉列表 ID = SelectionDropDownList runat = server 宽度 = 136px EnableViewState = AppendDataBoundItems = true >
< / asp:下拉列表 >

// Cs文件:
//如何填充下拉列表。正在从用户从列表框中选择的内容中填充下拉列表。

public void BindSomething()
{
DateTime choosenDate = DateTime.MinValue;

使用(SqlConnection conn = new SqlConnection(dbConn))
{
using(SqlCommand cmd = new SqlCommand(spretrieve,conn))
{
//丢失以保存值
列出< DateTime > listCopy = new List < DateTime > ();
DateTime dt;
字符串值= String.Join(,,ListBox1.Items.Cast < ListItem > ()。其中​​(i => i.Selected).Select(i => i.Text));
if(values.Contains(Select All))
{
//循环遍历列表框中的每个项目,然后将其添加到列表
foreach(ListBox1中的ListItem li)。项目)
{
if(DateTime.TryParse(li.Text,out dt))
{
listCopy.Add(dt);
}
}
}
其他
{
//循环浏览列表框中的每个项目,然后将其添加到列表
foreach(ListItem li在ListBox1.Items)
{
//检查项目是否被选中
if(li.Selected == true)
{
//将项目添加到列表
listCopy.Add(DateTime.Parse(li.Text));
}
}
}

//比较和排序,以便最新日期排在最前面
listCopy.Sort((x,y)= > y.CompareTo(x));
//清除下拉列表中的项目
SelectionDropDownList.Items.Clear();
//将数据源设置为dropdownlist
SelectionDropDownList.DataSource = listCopy;
//在dropdownlist中设置dateformatstring
SelectionDropDownList.DataTextFormatString ={0:dd-MMM-yyyy};
//绑定下拉列表
SelectionDropDownList.DataBind();
}





感谢有人可以帮助我,非常感谢!!

解决方案

检查下面的两个链接,了解如何根据下拉选择更改来更改页面内容。你需要使用jQuery ajax


。获取从服务器获取数据的方法。



http://stackoverflow.com/questions/6164507/change -the-content-of-a-div-based-on-selection-from-down-down-menu [ ^ ]



https://api.jquery.com/jquery.get/ [ ^ ]


hi, I got a drop down list. User upon selecting different value, I should get the option text of the option user has selected from the dropdown list, then go find the data from the database and display the relevant content on the webpage. If user change the option again, I should go retrieve different data based on option text selected and display the different content on the webpage.

How could I accomplish this? Anyone has any idea to keep me started? I researched online but couldn't find anything useful.

This is my codes:

//Aspx file:
            <asp:Dropdownlist ID="SelectionDropDownList" runat="server" Width="136px" EnableViewState="True" AppendDataBoundItems="true">
            </asp:Dropdownlist>

//Cs file:
//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();
}



Appreciate if someone can help me on this, thanks a lot!!

解决方案

Check the two links below on how to change page content based on the drop down select change. You need to use jQuery ajax


.Get methods to get data from the server.

http://stackoverflow.com/questions/6164507/change-the-content-of-a-div-based-on-selection-from-drop-down-menu[^]

https://api.jquery.com/jquery.get/[^]


这篇关于用户在DropDown列表中单击不同选项时更改页面内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆