从下拉列表中选择一个值,并在Label中显示不同的值 [英] Selecting a value from drop Down list and displaying a different value into the Label

查看:87
本文介绍了从下拉列表中选择一个值,并在Label中显示不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我这里有一个小问题..我有两个下拉列表,称为分区和名称...所以当我点击一个从部门下拉列表中特别划分属于该部门的所有员工姓名都会在名称下拉列表中累积。现在我想要做的是从下拉列表中选择一个特定名称后,我希望显示员工名称在标签中。下面是我试过的代码...我不知道如果我必须为下拉列表启用自动回复....我已启用自动回发分区下拉列表...当前标签是只显示第一个员工的名字....希望你能在这里帮助我一点:))提前完成待办事项



Hi,

I am having a slight problem here..I have two drop down lists called as division and name...So when i click on a particular division from the division drop down list all the employee names belonging to that division gets accumulated in the name drop down list.Now what I want to do is once I select a particular name from the dropdown list I want the employees designation to be displayed in a label..Below is the code I tried...I dont know If i have to enable autopostback for both the dropdown lists....I have enabled Auto post back for Division drop down list...Currently The label is only showing the first employees designation....Hoping you could help me out a bit here :)...Thanx in advance

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            Getdata();

        }

    }

    protected void ddlgroup_SelectedIndexChanged(object sender, EventArgs e)
    {

        int DivisionID = Convert.ToInt32(ddlname.SelectedValue.ToString());

        Fillnames(DivisionID);

        ddlname.SelectedIndex = 0;

    }

    protected void ddlname_SelectedIndexChanged(object sender, EventArgs e)
    {

        int designationID = Convert.ToInt32(ddlState.SelectedValue.ToString());

        Filldesignation(designationID);

    }



    private void Getdata()

    {

        string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlConnection con = new SqlConnection(strConn);

        SqlCommand cmd = new SqlCommand();

        cmd.Connection = con;

        cmd.CommandType = CommandType.Text;

        cmd.CommandText = "SELECT DivisionID,Division_name FROM emp_personal";

        DataSet objDs = new DataSet();

        SqlDataAdapter dAdapter = new SqlDataAdapter();

        dAdapter.SelectCommand = cmd;

        con.Open();

        dAdapter.Fill(objDs);

        con.Close();

        if (objDs.Tables[0].Rows.Count > 0)

        {

            ddlgroup.DataSource = objDs.Tables[0];

           ddlgroup.DataTextField = "Division_name";

            ddlgroup.DataValueField = "DivisionID";

            ddlgroup.DataBind();

           ddlgroup.Items.Insert(0, "--Select--");

       }
    }
    

    private void Fillnames(int divisionID)

    {

        string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlConnection con = new SqlConnection(strConn);

        SqlCommand cmd = new SqlCommand();
	   cmd.Connection = con;

        cmd.CommandType = CommandType.Text;

        cmd.CommandText = "SELECT DivisionID, Empname FROM emp_personal WHERE DivisionID =@CountryID";

        cmd.Parameters.AddWithValue("@DivisionID", divisionID);

        DataSet objDs = new DataSet();

        SqlDataAdapter dAdapter = new SqlDataAdapter();

        dAdapter.SelectCommand = cmd;

        con.Open();

        dAdapter.Fill(objDs);

        con.Close();

        if (objDs.Tables[0].Rows.Count > 0)

        {

            ddlname.DataSource = objDs.Tables[0];

            ddlname.DataTextField = "EmpName";

            ddlname.DataValueField = "DivisionID";

           ddlname.DataBind();

            ddlname.Items.Insert(0, "--Select--");

        }

       

    }



    private void  Filldesignation(int designationid)

    {

        string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlConnection con = new SqlConnection(strConn);

        SqlCommand cmd = new SqlCommand();

        cmd.Connection = con;

        cmd.CommandType = CommandType.Text;

        cmd.CommandText = "SELECT DesignationID,designation_name,EmpName FROM emp_personal WHERE DesignationID =@DesignationID";

        cmd.Parameters.AddWithValue("@DesignationID", designationid);

        DataSet objDs = new DataSet();

        SqlDataAdapter dAdapter = new SqlDataAdapter();

        dAdapter.SelectCommand = cmd;

        con.Open();

       lbldes.Text=cmd.ExecuteScalar().Tostring();

        con.Close();

       

        }
}

推荐答案

这个大纲代码适用于我:DropDownLists都是AutoPostBack = true但不适用于标签。



This outline of your code works for me: both DropDownLists are AutoPostBack=true but not for the label.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestWeb
{
    public partial class _Default : System.Web.UI.Page
    {
        string[] div = { "a", "b", "c", "d" };
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                GetData();
            }
        }

        private void GetData()
        {
            foreach (var d in div)
            {
                this.ddlgroup.Items.Add(d);
            }
        }

        protected void ddlgroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = this.ddlgroup.SelectedIndex;
            FillNames(index);
            this.ddlname.SelectedIndex = 0;
        }

        private void FillNames(int index)
        {
            if (index < 0 || index >= div.Length) return;
            this.ddlname.Items.Clear();
            for (int i = 0; i < 5; i++)
            {
                this.ddlname.Items.Add(div[index] + i.ToString());
            }

        }

        protected void ddlname_SelectedIndexChanged(object sender, EventArgs e)
        {
            string name = this.ddlname.SelectedItem.Text;
            this.lblOut.Text = "Selected Employee: " + name;
        }
    }
}


用这种方法替换Filldesignation。认为它可以帮助你

Replace Filldesignation with this method.Think it helps you
 private void  Filldesignation(int designationid)
 
    {
 
        string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
 
        SqlConnection con = new SqlConnection(strConn);
 
        SqlCommand cmd = new SqlCommand();
 
        cmd.Connection = con;
 
        cmd.CommandType = CommandType.Text;
 
        cmd.CommandText = "SELECT DesignationID,designation_name,EmpName FROM emp_personal WHERE DesignationID =@DesignationID";
 
        cmd.Parameters.AddWithValue("@DesignationID", designationid);
 
        DataSet objDs = new DataSet();
 
        SqlDataAdapter dAdapter = new SqlDataAdapter();
 
        dAdapter.SelectCommand = cmd;

 dAdapter.Fill(objDs); 

if(objDs.Tables[0].Rows.count>0)
{
 
       lbldes.Text=objDs.Tables[0].Rows[0]["designation_name"].tostring();
 

}
       
 
        }
}







------------------->如果您仍然无法将其排序,请检查一次查询




------------------->If you still not able to sort it out please check query once


这篇关于从下拉列表中选择一个值,并在Label中显示不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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