Asp.net pulling form field values from a matching entered value

TonyReba
TonyReba used Ask the Experts™
on
I need to use a texbtox that when the user enters a numerical value, the other form fields are automatically populated from a select statement into another table,,,

Do I need ajax?  
What are the steps to achieve this??
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
are you pulling data from the database?

Author

Commented:
yes,  I am. basically looks like this

enter Id    ______   textbox    

name     dob           gender          should get populated dynamically ,,  

preferebly id needs to be a texbox and not a dropdown list
I would say that yes you would need an ajax call to get the information since you are pulling it from a database.
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Do I need to create a web service for this?
yes, I would use AJAX. Do you need help with coding?

Author

Commented:
yes,  please , can you post a sample snippet , steps I need ?
Let's say you have two aspx pages, Default.aspx and GetData.aspx

On your Default.aspx page, add jquery script and the following function:

<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript">
<!--
  $(document).ready(function() {
 
    
    
     $('#txtID').change(function() {
        var xhReq = createXMLHttpRequest();
        xhReq.open("GET", "GetMyData.aspx?ID=" + $('#txtID').val(), true);
        xhReq.onreadystatechange = function() {
            if (xhReq.readyState != 4) { return; }
           
          var divElement = document.getElementById('MyDetails');
        
          divElement.innerHTML=xhReq.responseText
        };
        xhReq.send(null);

             
    });
    
    });
    
    function createXMLHttpRequest() {
   try { return new XMLHttpRequest(); } catch(e) {}
   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
   alert("XMLHttpRequest not supported");
   return null;
 }
  
      
//-->
</script>

Open in new window


And add the following to your html:

 
<form id="form1" runat="server">
     <asp:ScriptManager ID="ScriptManager1" runat="server" />
       
   
     <div id="MyInput">
     <asp:TextBox ID="txtID" runat="server"></asp:TextBox>
    </div>
    <div id="MyDetails">
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        <asp:TextBox ID="txtDOB" runat="server"></asp:TextBox>
    </div>
    
    </form>

Open in new window


Then on your GEtData.aspx add something like that:
 
protected void Page_Load(object sender, EventArgs e)
    {
        string strConn = "your connection string here";
        string query = "SELECT Name, DOB FROM Table1 WHERE ID = @ID";
        string id = Request.QueryString["ID"];
        SqlDataReader rdr = null;
        SqlParameter[] parms = new SqlParameter[1];
       
        if (id != null)
        {
            parms[0] = new SqlParameter("@ID", Request.QueryString["ID"]);


           
            .....
            Response.End();
        }

    }

Open in new window

Author

Commented:
for sql data reader,

which directives do I need to add?
using System.Data.SqlClient;

Author

Commented:
Something is not working, I dont get errors but Is not filling any field

how do I field for exmaple

<asp:TextBox ID="txtName" runat="server"></asp:TextBox>

Author

Commented:
fill sorry,  how do i bound to this textbox  to fill it out?
you still need to fill in the code for GetData.aspx

Author

Commented:
So, so how to the data is transfered to Default, Do I need to open GetData First?

I need it when I only open default.aspx, and enter and Id, then the pages  dynamically pulls the name and dob fills from the database ,  I could do using a postback but I wonder how to do this on AJAX?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial