使用C#

时间:2017-03-21 14:08:02

标签: c# asp.net

我试图阻止用户在下拉值为MBID时在搜索条件文本框中输入数值代码有效我只需要有关如何解决验证问题的帮助

enter image description here

C#代码

protected bool searchData()
{
      string val = DropDownList1.SelectedItem.Value;
 switch (val)
  {
      case "MBID":

       using (SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["Molecular"].ConnectionString))
         {
             con.Open();
               using (SqlCommand cmd = new SqlCommand(@"SELECT  count(*)

                        from Patient  where MBID = @SearchCriteria ", con))
                {

                        DataTable dt = new DataTable();
                        SqlDataAdapter da = new SqlDataAdapter(cmd);
                        int userCount = (Int32)cmd.ExecuteScalar();
                        da.Fill(dt);
           }
       }

    }

  }

“搜索”按钮调用下面的代码

protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (FormValidation()) if (searchData())
         {

            BindGrid();
            inputUpdateFornData();

        }

    }

HTML代码

<asp:DropDownList ID="DropDownList1" runat="server"  OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
      <asp:ListItem Value="MBID">MBID</asp:ListItem>
      <asp:ListItem Value="HPNumber">HP Number</asp:ListItem>
      <asp:ListItem Value="NHSNumber">NHS Number</asp:ListItem>

我无法使用下面的代码,因为可以为下拉列表的其他选项输入数值

<asp:TextBox ID="txtSearchCriteria" runat="server />
<asp:CompareValidator ID="cv" runat="server"     ControlToValidate="txtSearchCriteria" Type="Integer"
  Operator="DataTypeCheck" ErrorMessage="Value must be an integer!" />

3 个答案:

答案 0 :(得分:0)

你好,请尝试以下代码进行javascript验证 下面是javascript代码

 <script>
        function validateSearch()
        {
            var dropdown = document.getElementById("<%= ddlSearch.ClientID %>");
            var txtSearch = document.getElementById("<%= txtSearch.ClientID %>");
            if (dropdown.value == "MBID")
            {
                var reg = /^\d+$/;
                if (!reg.test(txtSearch.value)) {
                    alert("Please enter proper value");
                    txtSearch.focus();
                    return false;
                }
            }
        }

            </script>

及以下是下拉列表和文本框的aspx代码。

 <asp:DropDownList runat="server" ID="ddlSearch">
            <asp:ListItem Text="Select" Value="-1"></asp:ListItem>
            <asp:ListItem Text="MBID" Value="MBID"></asp:ListItem>
            <asp:ListItem Text="Name" Value="Name"></asp:ListItem>
        </asp:DropDownList>
        <asp:TextBox runat="server" ID="txtSearch"></asp:TextBox>
        <asp:Button runat="server" ID="btnSearch" Text="Search" OnClientClick="return validateSearch();" />

答案 1 :(得分:0)

这是怎么做的:

        try
        {
            int d = int.Parse(txtSearchCriteria.Text);
            //if works str is a number
        }
        catch (Exception e)
        {
            Console.WriteLine("It isn't a number!");
        }

答案 2 :(得分:0)

在您的CompareValidator中添加ClientValidationFunction="validateFunction"

编写验证函数如下 -

function validateFunction(s,args){
   if (yourdropdown.value == "MBID")
   {
        // Your code
   }
}

如果要进行服务器验证,请在服务器端添加OnServerValidate属性和处理函数。