1- ドロップダウンリスト:
CausesValidation="True"
2- Page_Load では、string.Empty を最初の項目の値に使用します。
DropDownList1.Items.Add(new ListItem("Select...", string.Empty));
ASPX:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
CausesValidation="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="DropDownList1" Display="Dynamic" ErrorMessage="Empty"></asp:RequiredFieldValidator>
<br />
<asp:Label ID="Label1" runat="server" EnableViewState="False"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
コードビハインド:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = DropDownList1.SelectedItem.Text;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.Items.Add(new ListItem("Select...", string.Empty));
DropDownList1.Items.Add(new ListItem("mehdi", "1"));
DropDownList1.Items.Add(new ListItem("ali", "2"));
}
}