如何根据asp.net中的dropdownlist绑定文本框? [英] How to bind textboxes based on selection of dropdownlist in asp.net?

查看:183
本文介绍了如何根据asp.net中的dropdownlist绑定文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为tbl_Employee的表,其中有Employee_name,Emp_ID,Emp_Salary,Emp_State,Emp_Mobile Number列.

I have table named tbl_Employee with columns Employee_name,Emp_ID,Emp_Salary,Emp_State,Emp_Mobile Number.

我有一个aspx页面,我在其中使用了一个下拉列表并与dtabase绑定,当我们单击该数据库时,将从该下拉列表中获取所有员工姓名.

I have an aspx page where I used a dropdownlist and binded with the dtabase which when we click we get all Employee name from that dropdownlist.

这是代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT EmpId, Name FROM tblEmployee"))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                ddlEmployee.DataSource = cmd.ExecuteReader();
                ddlEmployee.DataTextField = "Name";
                ddlEmployee.DataValueField = "EmpId";
                ddlEmployee.DataBind();
                con.Close();

            }
        }
        ddlEmployee.Items.Insert(0, new ListItem("--Select Employee--", "0"));
    }
}

在同一页面上,我有3个文本框,分别名为txtname,txtMobile和txtState.因此,我希望在选择特定雇员到该下拉列表时将数据库中的值绑定到那些文本框.

On the same page I have 3 textbox named txtname, txtMobile, and txtState. So I want to bind the value from database to those textbox on selection of particular employee to that drop downlist.

例如如果选择了Empname'ABC',则这3个文本框应与ABC名称,ABC移动电话及其所在州的值绑定. 我怎么做?我必须使用Jquery还是javascript?

For Ex. If have selected Empname 'ABC' those 3 textboxes should be bind with the value of ABC name, ABC mobile and his State. How do I do that? Do I have to use Jquery or javascript?

推荐答案

您要在ddlEmployee选择变更时显示员工详细信息,所以假设您有ddlEmployee,如:

You want to show employee details while ddlEmployee select change, so suppose you have ddlEmployee like :

<asp:DropDownList ID="ddlEmployee" runat="server" AutoPostBack="True" 
                onselectedindexchanged="ddlEmployee_SelectedIndexChanged">
 </asp:DropDownList> 

因此,可以在您的DropDown的onselectedindexchanged中完成该操作,

So it be done in onselectedindexchanged of your DropDown Like:

protected void ddlEmployee_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT Mobile,Name,State FROM tblEmployee where EmpId= @empId"))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                cmd.Parameters.AddWithValue("@empId", ddlEmployee.SelectedValue);
                SqlDataReader reader = cmd.ExecuteReader();
                if(reader.Read())
                {
                    lblName.Text = reader["Name"].ToString();
                    lblEmail.Text = reader["Mobile"].ToString();
                    lblState.Text = reader["State"].ToString();
                }
                con.Close();
            }
        }
    }
    catch (Exception s)
    {
        HttpContext.Current.Response.Write("Error Occured " + s.Message);
    }
}

这篇关于如何根据asp.net中的dropdownlist绑定文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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