View Full Version : Auto fill textbox

03-29-2007, 12:58 PM
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....

03-29-2007, 03:53 PM
Northwind basic example, Connection string defined in web.config, .NET 2.0 C#, add error checking for no rows, etc.


<%@ 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>
<form id="form1" runat="server">
<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">
<asp:ControlParameter ControlID="txtCustomer" PropertyName="Text" Name="cust_id" />

<asp:TextBox ID="txtCustomer" runat="server" AutoPostBack="true" OnTextChanged="txtCustomer_TextChanged"></asp:TextBox><br />
<asp:TextBox ID="txtCompName" runat="server" ></asp:TextBox></div>


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;