locked
ASP - check if a record exists from aspx form text box RRS feed

  • Question

  • User389883181 posted

    Hi

    Let say I have a database table (myTable) like this:
    ID
    Field1
    Field2

    and I have a form like this

    <form id="form1" runat="server">
    	Box 1
    	<asp:TextBox id="txt1" runat="server"></asp:TextBox>
    	<br />
    	Box 2
    	<asp:TextBox id="txt2" runat="server"></asp:TextBox>
    	<br/>
    	<span id="span1"></span>
    </form>
    

    How can I check if the inputted value into txt1 exists in MyTable - field1
    and if it does to add the value of field2 (of the same record in my table) into txt2
    if it doesn't exist span1 should display and error message (e.g. value not found)

    This is very simple with a dropdown and some javascript but I don't want to display the records in a dropdown.
    I need to check if the user kows the value of one of the records (field1) and has input it into the form.


    Thank you

    PS.
    This would be much simpler to explain with pictures but I can't work out how to add pictures to this post

    Sunday, October 21, 2018 11:10 AM

All replies

  • User1120430333 posted

    OnBlur() on the textbox that calls a JavaScript function that calls a function on the codebehind C#. The onblur() function would show an alert message based on a return value from the C# function.

    https://www.w3schools.com/tags/att_onblur.asp

    https://stackoverflow.com/questions/28827196/how-to-call-a-codebehind-function-from-javascript-in-asp-net

    Sunday, October 21, 2018 6:33 PM
  • User839733648 posted

    Hi reflo2,

    According to your description, I suggest that the key point of your requirement is to create a arraylist to return the datas from database and use ajax to check the datas.

    I've made a sample on my side, and for more details, you could refer to the code below.

    .Aspx

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $("#Button1").click(function () {
                    var txtValue1 = $("#TextBox1").val();
                    $.ajax({
                        url: "CheckTextBox.aspx/GetInfo",
                        type: "POST",
                        data: "{}",
                        contentType: "application/json;charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            var jsonObj = JSON.parse(data.d);
                            for (var i = 0; i < jsonObj.length; i++) {
                                if (jsonObj[i].Field1 == txtValue1) {
                                    console.log(jsonObj[i].Field1);
                                    $("#TextBox2").val(jsonObj[i].Field2);
                                    return false;
                                }
                                else {
                                    $("#infoname").text("Field1 not exist");
                                }
                            }
                        }
                    })
                })
            })
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                Field1:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /><br />
                Field2:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br /><br />
                <span id="infoname"></span><br /><br />
                <input type="button" id="Button1" value="Test"/>
            </div>
        </form>
    </body>
    </html>

    Code-behind.

    using System.Data.SqlClient;
    using System.Configuration;
    using System.Web.Services;
    using System.Web.Script.Serialization;
    
            [WebMethod]
            public static string GetInfo()
            {
                List<FieldInfo> fieldlist = new List<FieldInfo>();
                string constr = ConfigurationManager.ConnectionStrings["EmployeeManagementConnectionString"].ConnectionString;
                using(SqlConnection con=new SqlConnection(constr))
                {
                    string query = "SELECT * FROM tb_txtField";
                    SqlCommand cmd = new SqlCommand(query, con);
                    con.Open();
                    SqlDataReader sdr = cmd.ExecuteReader();
                    while (sdr.Read())
                    {
                        FieldInfo fieldInfo = new FieldInfo();
                        fieldInfo.ID = Convert.ToInt32(sdr["ID"]);
                        fieldInfo.Field1 = sdr["Field1"].ToString();
                        fieldInfo.Field2 = sdr["Field2"].ToString();
                        fieldlist.Add(fieldInfo);
                    }
                }
                JavaScriptSerializer js = new JavaScriptSerializer();
                return js.Serialize(fieldlist);
            }
    
            public class FieldInfo
            {
                public int ID { get; set; }
                public string Field1 { get; set; }
                public string Field2 { get; set; }
            }

    result:

    Best Regards,

    Jenifer

    Tuesday, October 23, 2018 2:31 AM