Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Auto fill textbox

    I have a form that includes 2 text input. I'd like the second text input to fill with the value selected from database based on the first textbox value, by default when the first text input loses focus. What methods should I look into to learn to do this?please help me as soon as possible....

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Northwind basic example, Connection string defined in web.config, .NET 2.0 C#, add error checking for no rows, etc.

    .aspx
    Code:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:SqlDataSource ID="sda_customers" runat="server" ConnectionString='<%$ ConnectionStrings:NORTHWIND %> ' 
            SelectCommand="SELECT CompanyName FROM Customers where CustomerID=@cust_id" 
            ProviderName="System.Data.SqlClient" OldValuesParameterFormatString="original_{0}" DataSourceMode="DataSet">
            <SelectParameters>
            <asp:ControlParameter ControlID="txtCustomer" PropertyName="Text" Name="cust_id" />
            </SelectParameters>
            </asp:SqlDataSource>
                
            <asp:TextBox ID="txtCustomer" runat="server" AutoPostBack="true" OnTextChanged="txtCustomer_TextChanged"></asp:TextBox><br />
            <asp:TextBox ID="txtCompName" runat="server" ></asp:TextBox></div>
        </form>
    </body>
    </html>
    .aspx.cs
    Code:
    using System;
    using System.Data;
    using System.Data.Sql;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class Default6 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void txtCustomer_TextChanged(object sender, EventArgs e)
        {
            DataView dataView = (DataView)sda_customers.Select(new DataSourceSelectArguments());
            DataSet dataSet = dataView.Table.DataSet;
            string result = dataSet.Tables[0].Rows[0]["CompanyName"].ToString();
            txtCompName.Text = result;
        }
    }

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •