本文介绍了如何获取下拉列表项的值.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的页面中有2个下拉列表,例如1.Location:[]
2.Sublocation:[]和

我的数据库中有2个表,例如Location_Lookup和Sublocation_Lookup,
在Location_Lookup中具有"LocationID","Location"和
之类的列在Sublocation_Lookup的havin列中,例如"SublocationID","LocationID"和"Sublocation".

然后,我创建了一个存储过程,以使用LocationID
来获取子位置.就像

Hi,

I am having a 2 drop-down list in my page like 1.Location :[ ]
2.Sublocation : [ ] and

I am having 2 tables in my database like Location_Lookup and Sublocation_Lookup,
in Location_Lookup having columns like ''LocationID'' ''Location'' and
in Sublocation_Lookup havin columns like ''SublocationID'' ''LocationID'' and ''Sublocation''.

Then I have created a stored Procedure to fetch the sublocation using the LocationID
like

create procedure PopSublocation @location int as
begin
Select Sublocation from Sublocation_LookUp where LocationID = @location



现在,为了将位置项目提取到位置"下拉列表中,我编写了如下代码:.



now for fetching the location items to the Location dropdown list i have written the code like..

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和Location,并在程序中传递了ddlLocation.DataValueField ="LocationId".

现在,要在Sublocation中获取相关的子位置列表:[]下拉菜单,我将代码写在"protected void ddlLocation_SelectedIndexChanged(object sender,EventArgs e)"中,该代码为



Here i am using stored procedure for getting locationID and Location and in the program for ddlLocation.DataValueField= "LocationID" i am passing.

Now for fetching the related sublocation list in the Sublocation:[ ] dropdown i am writting the code in the "protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)" and the code is

protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
      {
          SqlConnection conn = new SqlConnection(connstr);
          SqlCommand cmd = new SqlCommand();
          cmd.Connection = conn;
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.CommandText = "PopSublocation";
          cmd.Parameters.AddWithValue("@location",ddlLocation.SelectedItem.Value);
          ddlSublocation.Items.Insert(0, new ListItem("Select", "-1"));
          SqlDataAdapter da = new SqlDataAdapter(cmd);
          DataSet ds = new DataSet();
          da.Fill(ds);
          ddlSublocation.DataSource = ds;
          ddlSublocation.DataTextField = "Sublocation";
          ddlSublocation.DataValueField = "SublocationID";
          ddlSublocation.DataBind();

      }



我在这里传递LocationID的参数,
但是这里我的问题是我没有在子位置"下拉列表中得到子位置的详细信息..请为此帮助我...谢谢



Here i am passing the parameter of LocationID,
but here my problem is I am not getting the sublocation details in the Sublocation dropdown list.. please help me out for this... thanks

推荐答案

create procedure PopSublocation @location int as
begin
Select Sublocation from Sublocation_LookUp where LocationID = @location
end





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

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

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


protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
       {
           SqlConnection conn = new SqlConnection(connstr);
           SqlCommand cmd = new SqlCommand();
           cmd.Connection = conn;
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.CommandText = "PopSublocation";
           cmd.Parameters.AddWithValue("@location",ddlLocation.SelectedValue);
           ddlSublocation.Items.Insert(0, new ListItem("Select", "-1"));
           SqlDataAdapter da = new SqlDataAdapter(cmd,conn);
           DataSet ds = new DataSet();
           da.Fill(ds);
           ddlSublocation.DataSource = ds;
           ddlSublocation.DataTextField = "Sublocation";
           ddlSublocation.DataValueField = "SublocationID";
           ddlSublocation.DataBind();

       }


ddlSublocation.DataSource = ds.Table[0];


cmd.Parameters.AddWithValue("@location",ddlLocation.SelectedItem.Value);


试试这个


Try this

cmd.Parameters.AddWithValue("@location",Convert.ToInt32(ddlLocation.SelectedValue));


这篇关于如何获取下拉列表项的值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 12:26