使用 asp.net 和 c# 显示列表中的数据

本文关键字:列表 数据 显示 asp net 使用 | 更新日期: 2023-09-27 18:30:26

 <asp:DropDownList ID="DropDownList1" runat="server" Width="128px" Height="32px" 
            onselectedindexchanged="DropDownList1_SelectedIndexChanged" 
            style="position: relative; top: 3px; left: 4px">
            <asp:ListItem>.......SELECT.......</asp:ListItem>
            <asp:ListItem>Membership</asp:ListItem>
            <asp:ListItem>Publication</asp:ListItem>
            <asp:ListItem>Journal</asp:ListItem>
            <asp:ListItem>Additional Activity</asp:ListItem>
            <asp:ListItem>Guide Details</asp:ListItem>
            <asp:ListItem>Project</asp:ListItem>
            <asp:ListItem>Workshop</asp:ListItem>

   //c# code to call the list items
 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedIndex == Convert.ToInt32(0))
        {
        }
    }

使用 asp.net 和 c# 显示列表中的数据

试试这个:

<asp:DropDownList ID="DropDownList1" runat="server" Width="128px" Height="32px" Style="position: relative;
            top: 3px; left: 4px" >
            <asp:ListItem>.......SELECT.......</asp:ListItem>
            <asp:ListItem>Membership</asp:ListItem>
            <asp:ListItem>Publication</asp:ListItem>
            <asp:ListItem>Journal</asp:ListItem>
            <asp:ListItem>Additional Activity</asp:ListItem>
            <asp:ListItem>Guide Details</asp:ListItem>
            <asp:ListItem>Project</asp:ListItem>
            <asp:ListItem>Workshop</asp:ListItem>
        </asp:DropDownList>

C#:在按钮单击事件中,您可以添加相同的

 protected void btnView_Click(object sender, EventArgs e)
{
        if (DropDownList1.SelectedItem.Text == "Membership")// here you can add selectedindex as well
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringName"].ToString());
            con.Open();
            SqlDataAdapter adapter = new SqlDataAdapter("select columname from tablename where itemanme=Membership", con);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            con.Close();
            gridview1.DataSource=dt; // assign dt to the datasource of grid
            gridview1.DataBind();
        }
    }

在 drpdownlist 事件DropDownList1_SelectedIndexChanged编写代码

if (DropDownList2.SelectedIndex == Convert.ToInt32(0))
    {  
        your display code;
    }
else if(DropDownList2.SelectedIndex == Convert.ToInt32(1))
    {  
        your display code;
    }

等等... 使用 SQLDaReader 显示记录列表 对于显示记录

string qry="select * from your table name where youcolumforddl='" + DropDownList2.SelectedValue.ToString() + "'";
 SqlCommand cmd = new SqlCommand(qry, yourconnection);
        SqlDataReader dr=cmd.ExecuteReader();
        if(dr.HasRows)
        {
            yourcontrols=dr[0].ToString();
            ....
            ....
            if contorlis int type=Convert.ToInt32(dr[3]);
        }
        dr.Close();
        cmd.Dispose();

从数据库或集合中获取数据。将下拉列表的数据源属性设置为该源

List<strig> data = // set list from db or from any collection.
DropdownList1.DataSource=data;
DropdownList1.DtataBind();

这会将内容从源加载到下拉列表中。源可以是任何集合。

您还可以设置下拉列表的 DataTextField 和 DataValueFiling,以指定在选择下拉项时将集合的哪个属性显示为文本以及将集合的哪个属性显示为值。

DropDownList1.DataTextFiled="Name";
DropDownList1.DataValueField="id";