I have a drop down list like this in my asp.net application,
<asp:DropDownList runat="server" ID="ddlChanges" CssClass="selectstyle" onchange="javascript:ddlChangesChanged();">
<asp:ListItem Text="-- Select -- " Value="-1"></asp:ListItem>
<asp:ListItem Text="Status Change" Value="1"></asp:ListItem>
<asp:ListItem Text="Product Name Change" Value="2"></asp:ListItem>
<asp:ListItem Text="Category Change" Value="3"></asp:ListItem>
</asp:DropDownList>
So, when user selects "Status Change" from the list, another dropdown list with following values appears,
<asp:DropDownList runat="server" ID="ddlStatus" CssClass="selectstyle" Style="visibility: hidden;">
<asp:ListItem Text="-- Select -- " Value="-1"></asp:ListItem>
<asp:ListItem Text="Pending" Value="1"></asp:ListItem>
<asp:ListItem Text="Fixed" Value="2"></asp:ListItem>
<asp:ListItem Text="Cancelled" Value="3"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnUpdate" CssClass="selectstyle" runat="server" Text="Update" OnClick="btnUpdate_Click"></asp:Button>
If user does not select any value from the drop down list (ddlStatus), I am displaying an alert message ("You must select a status.") using JavaScript in the btnUpdate click event,
if (this.ddlStatus.SelectedItem.Value.Trim() == "-1")
{
string script = "<script type=\"text/javascript\">alert('You must select a status.');</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script);
}
But after this, my page refreshes and drop down list ddlChanges has "Status Change" (previously selected item) as selected item and drop down list (ddlStatus) gets hidden. I want ddlChanges drop down list to have -- Select -- as the selected item and ddlStatus should be visible.
How can I do that?
ddl
s to the required state inside thebtnUpdate
click?ddlStatus.Style= "visibility: hidden";
andddlChanges.SelectedItem = ...
– Eugene Podskal