判断DropDownList是否选中的几种方法

1. 后台cs代码判断:

DropDownList ddl=new DropDownList();
绑定数据源时:
ddl.DataSource=yourdatasource;
ddl.Items.Insert(0,new ListItem( "请选择 ", "0 "));

提交时:

if(ddl.SelectedValue== "0 "){
Response.Write( " <script> alert( '请您选择 ');window.history.back(); </script> ");
Response.End();
}

 

2.JS判断

function checkSelected() {
if (document.getElementById("DropDownList1").selectedIndex == 0) {
alert("请先选择要删除的项!");
return false;
}
else {
return (confirm('您确定要删除此类吗?'));
}
}

aspx:

<asp:Button ID="btnDelete" runat="server" Text="删除此类" OnClientClick="return checkSelected();"
onclick="btnDelete_Click" />

aspx.cs:

protected void btnDelete_Click(object sender, EventArgs e)
{
EasyReplyInfo er = new EasyReplyInfo();
er.ID = Convert.ToInt32(DropDownList1.SelectedItem.Value);

if (DirectorEmails.BLL.EasyReply.delete2(er))
{
string value = "alert('删除成功!');";
this.ClientScript.RegisterStartupScript(this.GetType(), "key", value, true);
}
}

 

3.验证控件判断

<asp:dropdownlist id="shouquan" runat="server" Width="104px">
<asp:ListItem Value="请选择">请选择</asp:ListItem>
<asp:ListItem Value="共享软件">共享软件</asp:ListItem>
<asp:ListItem Value="免费软件">免费软件</asp:ListItem>
</asp:dropdownlist>
<asp:comparevalidator id="CompareValidator3" runat="server" ControlToValidate="shouquan" ErrorMessage="授

权方式没有选择,请选择授权方式"
Operator="NotEqual" ValueToCompare="请选择"></asp:comparevalidator>

posted on 2012-02-14 10:02  zhangtao1212  阅读(563)  评论(0编辑  收藏  举报

导航