我正试图从下拉列表中选择Emp Name来获取地址,但我没有得到 [英] I Am Trying To Get Address On Select Of Emp Name From Dropdownlist But I Am Not Getting

查看:116
本文介绍了我正试图从下拉列表中选择Emp Name来获取地址,但我没有得到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我有一个下拉列表,我正在尝试获取empname选择的地址..帮助meplease我是编程的新手

hello i am having a dropdown list and i am trying to get the address on selection of empname ..help meplease i am new to programming

推荐答案

Poste the代码?

将下拉列表设置为autopostback并将事件添加到onselectedindexchanged并使用所选索引挖出值,这是最可靠的方法



例如:

网页



Poste the code?
Set the dropdown to autopostback and add event to onselectedindexchanged and use the selected index to dig out the value, that's the most reliable approach

for example:
Web page

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Dropdown dynamics sample</title>
</head>
<body>
    <form id="DemoForm" runat="server">
    <div>
        <asp:DropDownList id="CustomerSelect" runat="server" AutoPostBack="true"

            onselectedindexchanged="CustomerSelect_SelectedIndexChanged">
            <asp:ListItem Value="0">Please select</asp:ListItem>
        </asp:DropDownList>
         <label id="CustomerActive" runat="server"></label>
    </div>
    </form>
</body>
</html>





和代码背后





and code behind

public partial class DropDownSample : System.Web.UI.Page
    {
        protected struct CustomerData
        {
            public int CustomerID;
            public string Name;
            public string StreetAndNumber;
        }

        private Dictionary<int, CustomerData> _dictionary = new Dictionary<int, CustomerData>();

        protected void Page_Init(object sender, EventArgs e){
            _dictionary.Add(1, new CustomerData { CustomerID = 1, Name = "Bill Spectre", StreetAndNumber = "Looneystreet 1" });
            _dictionary.Add(2, new CustomerData { CustomerID = 2, Name = "Charlie P", StreetAndNumber = "Yazzlane 2" });
            _dictionary.Add(3, new CustomerData { CustomerID = 3, Name = "Dick Di Spenser", StreetAndNumber = "Whatyerroad 3" });
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //CustomerSelect.Items.Add(new ListItem("Please select", "0"));
                foreach (var customer in _dictionary)
                {
                    CustomerSelect.Items.Add(new ListItem(customer.Value.Name, customer.Value.CustomerID.ToString()));
                }
            }
        }

        protected void CustomerSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            int idx = CustomerSelect.SelectedIndex;
            if (idx == 0)
            {
                CustomerActive.InnerText = "Please select from dropdown, one of the names";
                return;
            }
            var cust = _dictionary[idx];
            CustomerActive.InnerText = cust.StreetAndNumber;
        }
    }


这会在下拉列表中加载员工姓名。

This would load Employee Names in the dropdown list.
empDropDownList.DataSource = empDt;      //empDt is a DataTable that contains all the data of Employee, 
empDropDownList.DataValueField = "EmpID";      // your unique key
empDropDownList.DataTextField = "EmpName";
empDropDownList.DataBInd();



现在按照以下方式编写查询,根据名称获取员工的数据,


now write the query following way to get the data of the employee based on the name,

string query  = "SELECT * FROM Employee WHERE EmpID=" + int.parse(empDropDownList.SelectedValue);
SqlDataAdapter da = new SqlAdapter(query, connnectionObject);
DataTable dt = new DataTable();
da.Fill(dt);
addressLbl.Text = dt.Rows[0]["Address"].ToString();





-KR



-KR


这篇关于我正试图从下拉列表中选择Emp Name来获取地址,但我没有得到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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