验证下载asp.net

时间:2013-01-29 09:34:55

标签: javascript asp.net .net validation

我有下拉

        <li><span>State: <asp:DropDownList ID="ddlAppState" runat="server">
                        </asp:DropDownList></span></li>

使用以下函数

填充页面load()
          private void ddlAppState_DataBind()
{
    string myXMLfile = Server.MapPath("~/App_Data/ActiveStates.xml");
    System.Data.DataSet dsStudent = new System.Data.DataSet();

    dsStudent.ReadXml(myXMLfile);
    ddlAppState.DataSource = dsStudent;
    ddlAppState.DataValueField = "StateID";
    ddlAppState.DataTextField = "StateName";
    ddlAppState.DataBind();
}

初始下拉值中的值是“--------”但是在后端它映射到“FD”我想验证是否选择了“-----”,如果我得到了它的值做ddlAppState.SelectedValue.ToString()是一个字符串“FD”然后我不想调用按钮的onlick功能。

按钮OnClick

 <asp:Button Style="left: 0px; position: relative" ID="btnSearch" runat="server" CausesValidation="False"
                CssClass="lbSearch closeform" Text="Search" Width="80px" OnClick="btnSearch_Click"  >
            </asp:Button></div>

 protected void btnSearch_Click(object sender, EventArgs e)
{
    try
    {
            if (ddlAppState.SelectedValue.ToString() == "FD" && ddlAppMode.SelectedValue.ToString() == "S")
      {
          SelectState.Text = " Please Select State ";
          return;
            }

            SelectState.Text = "";
        BindGridViewZipConfirmation1();

// clearFields();         }

知道如何做到这一点。谢谢

2 个答案:

答案 0 :(得分:0)

您可以检查SelectedIndex是否大于零(默认值),然后使用SelectedValue(如果是)。否则只需点击处理程序中的return

答案 1 :(得分:0)

如果您想在客户端进行验证。请试试这个

<asp:DropDownList ID="ddlName" runat="server">
    <asp:ListItem Text="Select" value="-1"></asp:ListItem>
  </asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"    ErrorMessage="Please select"  ControlToValidate="ddlName" ValidationGroup="btnSave" Display="Dynamic" InitialValue="-1">*</asp:RequiredFieldValidator>