仅当另一个文本框的值为ASP.NET时,才验证下拉列表



在我的web应用程序中,我有一个文本框和一个下拉列表。我的要求是,我想实现验证,如果用户填写了文本框,就必须选择下拉列表,否则就不应该进行验证。

我的代码,

<tr>  
<td>Returned On</td>  
<td>  
<asp:TextBox ID="Txtto" runat="server" AutoCompleteType="Disabled"></asp:TextBox></td>
<td>
<asp:DropDownList ID="ddlcond" runat="server" Height="26px" Width="156px" Visible="false">
<asp:ListItem Text="Working Condition" value="none"></asp:ListItem>
<asp:ListItem Text="Good-Usuable" Value="1"></asp:ListItem>
<asp:ListItem Text="Bad-Need Maintenance" Value="2"></asp:ListItem>
</asp:DropDownList>
</td>   
</tr>  
<asp:Button ID="BtnUpdate" runat="server" Text="Update" Visible="false" OnClick="BtnUpdate_Click" Width="75px" />

有很多方法可以做到这一点。在bt_Update_Click上,检查是否有相应的文本和路线。

if(Txtto.Text.Trim().Length > 0)
{
if(ddlcond.SelectedValue == "none")
{
//call alert (excuse the syntax I'm typing from memory)
ScriptManager.RegisterStartupScript(Page, Page.GetType, "err", "alert('Error You must select a drop down list value')", True);
}
else 
{
// your code to run here, page is valid
}



最新更新