I have following drop down
<li><span>State: <asp:DropDownList ID="ddlAppState" runat="server">
</asp:DropDownList></span></li>
It gets populated on page load () using below function
private void ddlAppState_DataBind()
{
string myXMLfile = Server.MapPath("~/App_Data/ActiveStates.xml");
System.Data.DataSet dsStudent = new System.Data.DataSet();
dsStudent.ReadXml(myXMLfile);
ddlAppState.DataSource = dsStudent;
ddlAppState.DataValueField = "StateID";
ddlAppState.DataTextField = "StateName";
ddlAppState.DataBind();
}
Inititally the value in drop down is “——–” but on backend it maps to “FD” i want to validate if the “—–” is selected whose value if i get by doing ddlAppState.SelectedValue.ToString() is a string “FD” then i do not want to call the onlick function of button .
Button OnClick
<asp:Button Style="left: 0px; position: relative" ID="btnSearch" runat="server" CausesValidation="False"
CssClass="lbSearch closeform" Text="Search" Width="80px" OnClick="btnSearch_Click" >
</asp:Button></div>
protected void btnSearch_Click(object sender, EventArgs e)
{
try
{
if (ddlAppState.SelectedValue.ToString() == "FD" && ddlAppMode.SelectedValue.ToString() == "S")
{
SelectState.Text = " Please Select State ";
return;
}
SelectState.Text = "";
BindGridViewZipConfirmation1();
// clearFields();
}
Any idea of how it can be done . Thank You
You could check whether the
SelectedIndexis greater than zero (the default value), then useSelectedValueif it is. Otherwise justreturnfrom the click handler.