我有两个下拉列表。
public void DrpDwn_Cuntry()
{
if (!Page.IsPostBack)
{
MySqlCommand sql_country = new MySqlCommand("SELECT DISTINCT(Country) FROM Animals", cs);
cs.Open();
MySqlDataReader ddlvalue;
ddlvalue = sql_country.ExecuteReader();
ddlcountry.DataSource = ddlvalue;
ddlcountry.DataValueField = "Country";
ddlcountry.DataTextField = "Country";
ddlcountry.DataBind();
ddlcountry.Items.Insert(0, "Choose A Sanctuary");
cs.Close();
cs.Dispose();
}
}
和
public void DrpDwn_Res()
{
if (!Page.IsPostBack)
{
MySqlCommand sql_residents = new MySqlCommand("SELECT DISTINCT(Country) FROM Animals", cs);
cs.Open();
MySqlDataReader ddlvalue_residents;
ddlvalue_residents = sql_residents.ExecuteReader();
ddlcountry_Res.DataSource = ddlvalue_residents;
ddlcountry_Res.DataValueField = "Country";
ddlcountry_Res.DataTextField = "Country";
ddlcountry_Res.DataBind();
ddlcountry_Res.Items.Insert(0, "Choose Your Country");
cs.Close();
cs.Dispose();
}
}
我想要一个消息框来显示所选的两个是否不匹配。 例如,如果选择的国家/地区a来自第一个,则国家/地区b来自第二个消息框 我知道我要使用If Else语句,我只是不确定如何写它?
答案 0 :(得分:2)
您可以使用SelectedItem
控件的DropDownList
属性来实现此目的。
1。获取第一个SelectedItem
的{{1}}
2。获取第二个DropDownList
的{{1}}
3。使用SelectedItem
方法比较两个DropDownList
值
4。如果项目不匹配,请使用SelectedItem
显示Equals()
框,因为alert
不是Webforms中的默认设置(在webforms中使用是好的) javascript javascript
)使用以下语法:
MessageBox
完整代码:(代码背后)
试试这个:
alert
答案 1 :(得分:1)
WinForms中提供了消息框,WebForms中的等效内容将通过javascript进行提醒(您需要在jQuery库中添加引用)
这就是:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js "
type="text/javascript"></script>
<script>
function compareAndAlert(){
var value1 = $('#dropDownId').val(); // get the selected value of first dropdown
var value2 = $('#dropDownId2').val(); // get the selected value of second dropdown
if (value1 != value2){
alert("The selected items do not match !"); // If the selected values are not equal display an Alert.
return false;
}
return true;
}
</script>
现在点击按钮
调用此方法<asp:Button ID="Button1" onclientclick="javascript:if(compareAndAlert()){}else{return false;}" runat="server" OnClick="Button1_Click" Text="Adopted Pet" Height="31px" Width="150px"/>
更新:这基本上是验证,虽然Sudhakar的答案是正确的,但即使选定的值不匹配,也会导致不必要的回发。这些类型的验证最适合客户端。请检查更新,我已更改按钮的onclientclick事件中的内容