Answered by:
dropdown value changed on another dropdown selection in asp.net

Question
-
User355715116 posted
I have a dropdownList called Institution and another two dropdownLists are Position and Department. And I want to change dropdown value of Position and department based on the selected value of Institution DropDownList.
Ex: If I select Not Available on Position then It should reflect on other two drop down Position and Department like Automatically Not available will be select for Position and Department.
Note : In my existing project I am using asp.net. I am confused that how to do this. Whether I should do it thru backend or handle in frontend.
It would be nice If I can have help with an code example as well.
Monday, August 31, 2020 9:31 PM
Answers
-
User1535942433 posted
Hi mazharul007,
Accroding to your codes,I suggest you could use DataValueField and DataTextField.
Just like this:
ddlPosition.DataValueField= positionId.ToString(); ddlPosition.DataTextField= positionName;
More details,you could refer to below article:
Best regards,
Yijing Sun
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 7, 2020 8:35 AM
All replies
-
User1535942433 posted
Hi mazharul007,
Accroding to your description,I'm guessing that you need to create cascading dropdownlist.You could select sqlquery accroding to one of the dropdownlist in code-behind.
More details,you could refer to below codes:
<table> <tr> <td>Institution :</td> <td><asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList></td> </tr> <tr> <td>Department :</td> <td><asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"></asp:DropDownList></td> </tr> <tr> <td>Position :</td> <td><asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="true"></asp:DropDownList></td> </tr> </table>
Code-Behind:
protected void bind() { string str, strSql; str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString; SqlConnection conn = new SqlConnection(str); strSql = "select * from Institution"; SqlDataAdapter da = new SqlDataAdapter(strSql, str); DataSet ds = new DataSet(); da.Fill(ds, "Institution"); conn.Close(); DropDownList1.DataSource = ds; DropDownList1.DataTextField = "InstitutionName"; DropDownList1.DataValueField = "InstitutionId"; DropDownList1.DataBind(); DropDownList1.Items.Insert(0, new ListItem("--Select--", "0")); DropDownList2.Items.Insert(0, new ListItem("--Select--", "0")); DropDownList3.Items.Insert(0, new ListItem("--Select--", "0")); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bind(); } } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { int InstitutionId = Convert.ToInt32(DropDownList1.SelectedValue); string strcon = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString; SqlConnection con = new SqlConnection(strcon); con.Open(); SqlCommand cmd = new SqlCommand("select * from Department where InstitutionId=" + InstitutionId, con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); con.Close(); DropDownList2.DataSource = ds; DropDownList2.DataTextField = "DepartmentName"; DropDownList2.DataValueField = "DepartmentID"; DropDownList2.DataBind(); DropDownList2.Items.Insert(0, new ListItem("--Select--", "0")); } protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) { int DepartmentId = Convert.ToInt32(DropDownList2.SelectedValue); string strcon = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString; SqlConnection con = new SqlConnection(strcon); con.Open(); SqlCommand cmd = new SqlCommand("select * from Position where DepartmentId=" + DepartmentId, con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); con.Close(); DropDownList3.DataSource = ds; DropDownList3.DataTextField = "PositionName"; DropDownList3.DataValueField = "PositionId"; DropDownList3.DataBind(); DropDownList3.Items.Insert(0, new ListItem("--Select--", "0")); }
Result:
More details,you could refer to below article:
https://forums.asp.net/t/1983936.aspx?Need+three+Linked+Drop+Down+Lists
Best regards,
Yijing Sun
Tuesday, September 1, 2020 2:28 AM -
User355715116 posted
Dear Yij Sun,
This DB that I am currently working on where Institution Table contains
[InstituteID] [int] IDENTITY(1,1) NOT NULL, [InstituteTypeID] [int] NOT NULL, [InstituteName] [nvarchar](210) NOT NULL, [Address] [nvarchar](200) NULL, [Town] [nvarchar](50) NULL, [County] [nvarchar](50) NULL, [CountryId] [int] NULL, [StateId] [int] NULL, [ZipCode] [varchar](100) NULL, [NoOfBeds] [int] NULL CONSTRAINT [DF_Companies_NoOfBeds] DEFAULT ((0)), [Website] [varchar](200) NULL, [Comments] [ntext] NULL, [ContractType] [nvarchar](50) NULL, [ExpirationDate] [datetime] NULL, [PaymentType] [nvarchar](50) NULL, [Percentage] [float] NULL CONSTRAINT [DF_Institute_Percentage] DEFAULT ((0)), [FlatRate] [money] NULL CONSTRAINT [DF_Institute_FlatRate] DEFAULT ((0)), [Perpetuity] [bit] NULL CONSTRAINT [DF_Institute_Perpetuity] DEFAULT ((0)), [Telephone] [varchar](50) NULL, [state] [varchar](50) NULL, [isActive] [bit] NULL CONSTRAINT [DF__Institute__isAct__3BD5CA13] DEFAULT ((1)), [isClient] [bit] NULL, [isBad] [bit] NULL, [ParentID] [int] NULL, [CreatedBy] [int] NULL, [CreatedDate] [datetime] NULL, [UpdatedBy] [int] NULL, [UpdatedDate] [datetime] NULL,
these fields.
My departments table contains
[DepartmentID] [int] IDENTITY(1,1) NOT NULL, [DepartmentName] [varchar](100) NULL, [Description] [ntext] NULL, [CreatedBy] [int] NULL, [CreatedDate] [datetime] NULL, [UpdatedBy] [int] NULL, [UpdatedDate] [datetime] NULL,
And my position table contains[PositionID] [int] IDENTITY(1,1) NOT NULL, [PositionName] [nvarchar](200) NOT NULL, [Description] [nvarchar](max) NULL, [PositionLicenseRequirementID] [bigint] NULL, [Acronym] [nvarchar](1024) NULL, [CreatedBy] [int] NULL, [CreatedDate] [datetime] NULL, [UpdatedBy] [int] NULL, [UpdatedDate] [datetime] NULL, [DepartmentID] [int] NULL,
As I am new to asp.net I am still confused about your solution. So, I am adding my Existing ddlAddPosition_OnSelectedIndexChanged
code behind.protected void ddlAddPosition_OnSelectedIndexChanged(object sender, EventArgs e) { try { if (ddlAddPosition.SelectedValue.Trim().Length > 0) { LoadBillandPayRateDropDownListData(); RadComboBox ddlAddPosition = ((RadComboBox)sender); PageBase basePage = new PageBase(); TextBox txtAddClinicalArea = (TextBox)ddlAddPosition.Parent.FindControl("txtAddClinicalArea"); RadComboBox ddlAddEmployer = (RadComboBox)ddlAddPosition.Parent.FindControl("ddlAddEmployer"); TextBox txtLocationAddNumber = (TextBox)ddlAddPosition.Parent.FindControl("txtLocationAddNumber"); TextBox numberTextBox = (TextBox)ddlAddPosition.Parent.FindControl("numberTextBox"); //naumy int InstitutionId = Convert.ToInt32(ddlAddPosition.SelectedValue); DataTable dtPosition = PositionManager.GetPosition(Convert.ToInt32(ddlAddPosition.SelectedValue.ToString())); if (dtPosition != null && dtPosition.Rows.Count > 0 && dtPosition.Rows[0]["DepartmentID"].ToString() != "") { int departmentId = Convert.ToInt32(dtPosition.Rows[0]["DepartmentID"].ToString()); DataTable dtDepartment = DepartmentManager.GetDepartment(departmentId); if (dtDepartment != null && dtDepartment.Rows.Count > 0) { string departmentName = dtDepartment.Rows[0]["DepartmentName"].ToString(); txtAddClinicalArea.Text = departmentName; } } else { txtAddClinicalArea.Text = string.Empty; } //DataList extensionNumberAddDataList = (DataList)ddlAddPosition.Parent.FindControl("extensionNumberAddDataList"); basePage.txtLocationName_TextChanged(txtAddClinicalArea.Text.Trim(), txtLocationAddNumber, ddlAddEmployer.SelectedValue, ddlAddPosition.SelectedValue, numberTextBox); if (ddlAddPosition.Text.ToLower().Contains(" day") || ddlAddPosition.Text.ToLower().Contains("day ") || ddlAddPosition.Text.ToLower().Contains("day")) { cboAddShift.SelectedValue = "Day"; } else if (ddlAddPosition.Text.ToLower().Contains(" evening") || ddlAddPosition.Text.ToLower().Contains("evening ") || ddlAddPosition.Text.ToLower().Contains("evening")) { cboAddShift.SelectedValue = "Evening"; } else if (ddlAddPosition.Text.ToLower().Contains(" night") || ddlAddPosition.Text.ToLower().Contains("night ") || ddlAddPosition.Text.ToLower().Contains("night")) { cboAddShift.SelectedValue = "Night"; } else { cboAddShift.SelectedValue = "Day"; } } } catch (Exception ex) { AddWorkHistoryRadAjaxPanel.Alert("An Error has occured"); } }
Seeing that can you tell exactly what should be my approach of solving it?
And yes as I mentioned in my problem I want both Position and Department populated with Not Available If Not Available selected in Institution.
Note: Also mistakenly I said that Department is also a dropdown. But Position is dropdown field and department is a textarea field.GetPosition and GetDepartment method:
public DataTable GetPosition(int positionId) { string sql = @"SELECT * FROM [Position] Where PositionID=@PositionID"; QueryParamList paramList = new QueryParamList(); paramList.Add(new QueryParamObj() { ParamName = "PositionID", ParamValue = positionId, DBType = DbType.Int32 }); string errorStr = String.Empty; DataTable dt = LoadDataTable(sql, paramList, "Position", ref errorStr); return dt; } public DataTable GetDepartment(int departmentID) { string sql = @"SELECT [DepartmentID],[DepartmentName],[Description] FROM [Departments] WHERE DepartmentID=@DepartmentID"; QueryParamList paramList = new QueryParamList(); paramList.Add(new QueryParamObj() { ParamName = "DepartmentID", ParamValue = departmentID, DBType = DbType.Int32 }); string errorStr = String.Empty; DataTable dt = LoadDataTable(sql, paramList, "Departments", ref errorStr); return dt; }
Best Regards,Mazharul
Wednesday, September 2, 2020 9:39 AM -
User1535942433 posted
Hi mazharul007,
As far as I think,you could add 'Not Available' as data in database.Accroding to your description,I'm guessing that you get department value based on Position's selected value.
In the database,the table Position have PositionID which is same with the table Department 's PositionID. When you change select the position dropdownlist ,you could select Department table which they have same PositionID.And then,you could bind the data to Department textarea .
My database:
Position table:
Department Table:
More details,you could refer to below article:
Best regards,
Yijing Sun
Thursday, September 3, 2020 7:39 AM -
User355715116 posted
Hi, yii sun,
I am almost done. But unfortunately stuck in ending. I am successfully fetching the result of position based on the selection of institution.
but cant set value and text of ddlPosition.
here is my code.DataTable dtPosition = PositionManager.GetPositionByName(institutionName); if(dtPosition != null && dtPosition.Rows.Count > 0) { int positionId = Convert.ToInt32(dtPosition.Rows[0]["PositionID"].ToString()); string positionName = dtPosition.Rows[0]["PositionName"].ToString(); ddlPosition.SelectedValue = positionId.ToString(); ddlPosition.Text = positionName; }
Here in positionId and positionName variable id and name is populating. but when I am trying to set the value and text of ddlPosition it is not working.
I want positionId and positionName to be the value and text of ddlPosition. will you tell me how to to this?
Best Regards,Mazharul
Thursday, September 3, 2020 7:51 AM -
User1535942433 posted
Hi mazharul007,
Accroding to your codes,I suggest you could use DataValueField and DataTextField.
Just like this:
ddlPosition.DataValueField= positionId.ToString(); ddlPosition.DataTextField= positionName;
More details,you could refer to below article:
Best regards,
Yijing Sun
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 7, 2020 8:35 AM