Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have table named tbl_Employee having columns Employee_name,Emp_ID,Emp_Salary,Emp_State,Emp_Mobile Number.

Now 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.
Here is the code:-

C#
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"));
}
}

Now 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.

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??
Please help me.

What I have tried:

I am unable to do so?? pls guide
Posted
Updated 15-Jan-18 0:37am

Hey Set "AutoPostBack" True for ddlEmployee and write code inside
ddlEmployee_SelectedIndexChanged


protected void ddlEmployee_SelectedIndexChanged(object sender, EventArgs e)
  {
     TextBox.Text= // Set your Textbox value
  }


for more please refer :

[^]
 
Share this answer
 
Add event name
C#
ddlEmployee_SelectedIndexChanged
in dropdown list and set dropdownlist
C#
AutoPostBack="True"
to call the event in aspx.cs file.

Example :
<asp:DropDownList ID="ddlEmployee" runat="server" AutoPostBack = "true" OnSelectedIndexChanged = "ddlEmployee_SelectedIndexChanged">
    <asp:ListItem Text="Ram" Value="1" />
    <asp:ListItem Text="Shyam" Value="2" />
    <asp:ListItem Text="Mohan" Value="3" />
    <asp:ListItem Text="Gaurav" Value="4" />
    <asp:ListItem Text="Shohan" Value="5" />
</asp:DropDownList>



C#
protected void ddlEmployee_SelectedIndexChanged(object sender, EventArgs e)
{
    TextBox1.Text = ddlEmployee.SelectedItem.Value;
}


Now you will get the selected value of dropdownlist into your textbox.
 
Share this answer
 
Comments
Pankaj-kumar-chaudhary 15-Jan-18 6:46am    
Nice answer with example
Karthik_Mahalingam 15-Jan-18 8:46am    
5
han sun 6-Feb-18 8:27am    
Thanks!! Worked

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900