如何根据不同页面中存在的第一个下拉列表选择如何填充第二个下拉列表中的项目 [英] how to populate items in second DropDown list, according to the selection of first dropdown list that is present in different page

查看:132
本文介绍了如何根据不同页面中存在的第一个下拉列表选择如何填充第二个下拉列表中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个网页1个患者页面2个医生页面

在患者页面中,我有一个下拉列表,如位置:[]

在医生页面中,我还有另一个下拉列表,例如Area:[]

在这里,我有2个表,例如数据库中的位置和子位置

在位置表中,我有类似"LocationID","Location"的列

在子位置表中,我有"SublocationID","LocationID"和"SubLocation"之类的列

现在,我已经写了代码以填充表格中的位置下拉列表,请检查以下代码:

I am having 2 webpages 1 patient page 2 Doctor Page

In patient page i am having drop down list like Location : [ ]

in doctors page i am having another drop down list like Area : [ ]

Here i am having 2 tables like location and sub location in the database

in location table i am having columns like ''LocationID'' ''Location''

in sub location table i am having columns like ''SublocationID'', ''LocationID'' and ''SubLocation''

now i have written the code to populate the location dropdown list from the table please check the below code:

protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
               LocationBind();
               
           }
       }


protected void LocationBind()
        {
            SqlConnection conn = new SqlConnection(connstr);

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Location";

            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            ddlLocation.DataSource = reader;
            ddlLocation.DataTextField = "Location";
            ddlLocation.DataValueField = "LocationID";
            ddlLocation.DataBind();
            ddlLocation.Items.Insert(0, new ListItem("Select", "-1"));
            conn.Close();
            reader.Close();
        }





现在,我必须使用LocationID来填充医生页面中存在的子位置下拉列表中的相关项目.请帮我解决这个问题... thnks





now by using the LocationID i have to populate the related items in sublocation Dropdown list that is present in doctor page. Please help me out about this... thnks

推荐答案

hi将一页的数据访问到另一页.
根据我的看法,有3种方法:
1)创建Cookies
2)创建会话变量
3)SQL状态变量

使用1& 2它们相对来说比较容易.

这是使用会话变量的示例

使用Facade设计模式管理ASP.NET会话变量 [ ^ ]

http://msdn.microsoft.com/en-us/library/ms178581.aspx [ ^ ]

谢谢
hi to access data of 1 page into another.
there are 3 ways according to me:
1) Creating Cookies
2) Creating Session Variables
3) Sql State Variables

use 1 & 2 they are comparitively easy.

here are examples to use session variables

Manage ASP.NET Session Variables using the Facade Design Pattern[^]

http://msdn.microsoft.com/en-us/library/ms178581.aspx[^]

Thanks


嘿,尝试一下并根据需要在代码中进行更改
private void BindDropDownList1()

    {

        DataTable dt = new DataTable();

        SqlConnection connection = new SqlConnection(GetConnectionString());

        try

        {

            connection.Open();

            string sqlStatement = "SELECT ColumnName * FROM TableName";

            SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);

            SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

            sqlDa.Fill(dt);

            if (dt.Rows.Count > 0)

            {

                DropDownList1.DataSource =dt;

                DropDownList1.DataTextField = "ColumnName"; // the items to be displayed in the list items

                DropDownList1.DataValueField = "ColumnName"; // the id of the items displayed

                DropDownList1.DataBind();

            }

        }

        catch (System.Data.SqlClient.SqlException ex)

        {

            string msg = "Fetch Error:";

            msg += ex.Message;

            throw new Exception(msg);

        }

        finally

        {

            connection.Close();

        }

    }



    private void BindDropDownList2(string field)

    {

        DataTable dt = new DataTable();

        SqlConnection connection = new SqlConnection(GetConnectionString());

        try

        {

            connection.Open();

            string sqlStatement = "SELECT * FROM Table WHERE ColumnName = @Value1";

            SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);

            sqlCmd .Parameters.AddWithValue("@Value1", field) 

            SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

            sqlDa.Fill(dt);

            if (dt.Rows.Count > 0)

            {

                DropDownList2.DataSource =dt;

                DropDownList2.DataTextField = "ColumnName"; // the items to be displayed in the list items

                DropDownList2.DataValueField = "ColumnName; // the id of the items displayed

                DropDownList2.DataBind();

            }

        }

        catch (System.Data.SqlClient.SqlException ex)

        {

            string msg = "Fetch Error:";

            msg += ex.Message;

            throw new Exception(msg);

        }

        finally

        {

            connection.Close();

        }

    }



   protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            BindDropDownList1();

        }

    }



   protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)

   {

   

      BindDropDownList2(DropDownList1.SelectedItem.Text);

   

   }


尝试一下
/> 它会反抗地工作.

您的首页代码很好.
通过状态管理概念将位置下拉列表中所选项目的位置ID发送到第二页.

然后在第二页中执行与第一页代码相同的代码.

只需将下拉名称替换为另一个下拉列表名称,并且过程必须包含诸如"SELECT * FROM Table WHERE locationid = @ Value1"之类的查询

将第二页上的下拉列表作为参数@ Value1绑定时,paas该位置ID
Hi Try this
it will defiantly work.

Your First page code is fine.
send location id of selected item of location dropdown to second page by state management concepts.

then in second page do same code as your first page code.

just replce dropdown name with your another dropdown list name and procdure must contain query like "SELECT * FROM Table WHERE locationid= @Value1"

paas that location id while binding dropdown on second page as parameter @Value1


这篇关于如何根据不同页面中存在的第一个下拉列表选择如何填充第二个下拉列表中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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