Quantcast
Channel: CodeProject Latest postings for ASP.NET
Viewing all articles
Browse latest Browse all 3938

How do you have a DropDownList populated without default selected item?

$
0
0
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
<asp:DropDownList ID="RequestorDropDownList" runat="server" Font-Size="8.25pt"
     Height="20px" ToolTip="Requestor" Width="160px" SelectMethod="GetRequestorEmail"
     DataTextField="DisplayName" DataValueField="Email"
     OnSelectedIndexChanged="RequestorDropDownList_SelectedIndexChanged"></asp:DropDownList>


Code behind that is populating the dropdownlist
#region Requestor dropdownlistpublicasync Task<IEnumerable<GetRequestorInfoModel>> GetRequestorEmail()
{                             try
   {var requestors = await FTACaseReseting.Controllers.RequestorInfoController.GetAllRequestorInfoes();//Show first name as selectedreturn 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("<script>alert(" + HttpUtility.JavaScriptStringEncode(errorMsg, true) + ")</script>");return Enumerable.Empty<GetRequestorInfoModel>();
   }
}#endregion


Here is code for RequestorDropDownList_SelectedIndexChanged
protectedvoid 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;
     }

}

Viewing all articles
Browse latest Browse all 3938

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>