How do you have a DropDownList populated without default selected item?
-
I have a RequestorDropDownList which is populated with a list of names from a GetRequestorInfoModel. I need help to make a change so that when this list is populated, the first name is not automatically selected until a user selects it. How do I do this based on my code below? ASP code for the dropdownlist
Code behind that is populating the dropdownlist
#region Requestor dropdownlist
public async Task> GetRequestorEmail()
{
try
{
var requestors = await FTACaseReseting.Controllers.RequestorInfoController.GetAllRequestorInfoes();
//Show first name as selected
return requestors.OrderBy(x => x.DisplayName);
}
catch (Exception ex)
{
string errorMsg = string.Format("An error has occured in {0}. \nException:\n{1}", "PopulateRequestorComboBox()", ex.Message);
Response.Write("alert(" + HttpUtility.JavaScriptStringEncode(errorMsg, true) + ")");
return Enumerable.Empty();
}
}
#endregionHere is code for RequestorDropDownList_SelectedIndexChanged
protected void RequestorDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
if (RequestorDropDownList.SelectedItem ==null)
{
ListItem requestorItem = new ListItem();
requestorItem = (ListItem)RequestorDropDownList.SelectedItem;if (requestorItem.Text.Length < 0) { string selectRequestor = "Please select a Requestor."; ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + selectRequestor + "');", true); } this.EmailButton.Enabled = false; RequestorDropDownList.SelectedValue = RequestorDropDownList.SelectedItem.Value; }
}
-
I have a RequestorDropDownList which is populated with a list of names from a GetRequestorInfoModel. I need help to make a change so that when this list is populated, the first name is not automatically selected until a user selects it. How do I do this based on my code below? ASP code for the dropdownlist
Code behind that is populating the dropdownlist
#region Requestor dropdownlist
public async Task> GetRequestorEmail()
{
try
{
var requestors = await FTACaseReseting.Controllers.RequestorInfoController.GetAllRequestorInfoes();
//Show first name as selected
return requestors.OrderBy(x => x.DisplayName);
}
catch (Exception ex)
{
string errorMsg = string.Format("An error has occured in {0}. \nException:\n{1}", "PopulateRequestorComboBox()", ex.Message);
Response.Write("alert(" + HttpUtility.JavaScriptStringEncode(errorMsg, true) + ")");
return Enumerable.Empty();
}
}
#endregionHere is code for RequestorDropDownList_SelectedIndexChanged
protected void RequestorDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
if (RequestorDropDownList.SelectedItem ==null)
{
ListItem requestorItem = new ListItem();
requestorItem = (ListItem)RequestorDropDownList.SelectedItem;if (requestorItem.Text.Length < 0) { string selectRequestor = "Please select a Requestor."; ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + selectRequestor + "');", true); } this.EmailButton.Enabled = false; RequestorDropDownList.SelectedValue = RequestorDropDownList.SelectedItem.Value; }
}
You can add an empty item, such as one that has text "Please select..." or something like that.
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.
-
You can add an empty item, such as one that has text "Please select..." or something like that.
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.
I actually have tried to add <--Please Select a Requestor--> but I do not know how to add that text. Do I add that text message in ASP code for the DropDownList or code behind that is populating dropdownlist? I really do not know how to add it but I want to add it with your help. When I debug, requestorItem = (ListItem)RequestorDropDwonList.SelectedItem is showing first name on the list. I want it to show null or nothing.
-
I actually have tried to add <--Please Select a Requestor--> but I do not know how to add that text. Do I add that text message in ASP code for the DropDownList or code behind that is populating dropdownlist? I really do not know how to add it but I want to add it with your help. When I debug, requestorItem = (ListItem)RequestorDropDwonList.SelectedItem is showing first name on the list. I want it to show null or nothing.
Member 11403304 wrote:
but I do not know how to add that text.
Whatever code is creating the list just have it add in the blank one.
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.
-
I actually have tried to add <--Please Select a Requestor--> but I do not know how to add that text. Do I add that text message in ASP code for the DropDownList or code behind that is populating dropdownlist? I really do not know how to add it but I want to add it with your help. When I debug, requestorItem = (ListItem)RequestorDropDwonList.SelectedItem is showing first name on the list. I want it to show null or nothing.
You insert a new object into the first position in the list populating the drop down. You now know what the content of that item is so when you process the selection you check for the inserted item and deal with that.
Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP