Answered by:
Select dropdown menu by session user

Question
-
User-1901014284 posted
I am looking to select an option from my dropdown menu by the user session that has signed into the system, please see below the code for my dropdown menu.
SqlConnection sqlConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
SqlCommand cmd = new SqlCommand();
Visit_Call_OtherDropDownList.Items.Add(new ListItem("--Please Select--", ""));
Visit_Call_OtherDropDownList.AppendDataBoundItems = true;
string strQuery = "Select ID, Visit_Call_Other from Visit_Call_Other";
cmd.Connection = sqlConnection;
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
sqlConnection.Open();
Visit_Call_OtherDropDownList.DataSource = cmd.ExecuteReader();
Visit_Call_OtherDropDownList.DataTextField = "Visit_Call_Other";
Visit_Call_OtherDropDownList.DataValueField = "ID";
Visit_Call_OtherDropDownList.DataBind();
sqlConnection.Close();I have been thinking it to be along the lines of:
if (Session["jobRole"].ToString() == "3")
{Dropdownmenu.Text = "2";
}
Any help would be greatly appreciated.
Regards
Jonny
Tuesday, March 28, 2017 2:41 PM
Answers
-
User475983607 posted
Use the SelectedValue property of the DropDownList server control.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, March 28, 2017 3:09 PM -
User2103319870 posted
You could also Try setting the value to Dropdownlist by using FindItembyText method to find the particular item and set the selected index like given below
if (Session["jobRole"].ToString() == "3") { //Set an value in dropdownlist Dropdownmenu.SelectedIndex = Dropdownmenu.Items.IndexOf(Dropdownmenu.Items.FindByText("2")); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, March 28, 2017 3:14 PM -
User-1901014284 posted
Thanks for your help, I have resolved it by using the below code.
if (Session["jobRole"].ToString() == "3")
{
Visit_Call_OtherDropDownList.SelectedValue = "2";
}Again thank you for all your help greatly appreciated.
Kind Regards
Jonny
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, March 29, 2017 9:20 AM
All replies
-
User475983607 posted
Use the SelectedValue property of the DropDownList server control.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, March 28, 2017 3:09 PM -
User2103319870 posted
You could also Try setting the value to Dropdownlist by using FindItembyText method to find the particular item and set the selected index like given below
if (Session["jobRole"].ToString() == "3") { //Set an value in dropdownlist Dropdownmenu.SelectedIndex = Dropdownmenu.Items.IndexOf(Dropdownmenu.Items.FindByText("2")); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, March 28, 2017 3:14 PM -
User-1901014284 posted
Thank you for your quick response, I have tried this and had no luck. I entered the code in the Page_Load of the C# page and amended where required. I have also tried using the ID and the Value as below still with no luck.
Dropdownmenu.Items.FindByText("2"))
Dropdownmenu.Items.FindByText("Call"))
Tuesday, March 28, 2017 4:46 PM -
User-1901014284 posted
Thanks for your help, I have resolved it by using the below code.
if (Session["jobRole"].ToString() == "3")
{
Visit_Call_OtherDropDownList.SelectedValue = "2";
}Again thank you for all your help greatly appreciated.
Kind Regards
Jonny
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, March 29, 2017 9:20 AM