Web程序 如何将用sql语句查到的数据绑定到文本框,下拉框.各位高手帮帮忙!谢谢!

解决方案 »

  1.   

    DataReader Reader;
    然后 txtBox.Text  = Reader["id"].ToString()
      

  2.   

    <%@ Page Language="C#" %><script runat=server>void Page_Load(Object sender , EventArgs e)

    lblMessage.DataBind();
    }string  GetTime() {
    return DateTime.Now.ToString( "T" );
    }</Script><html>
    <head><title>SimpleBind.aspx</title></head>
    <body>
    <form Runat="Server"><asp:Label
      ID="lblMessage"
      Text='<%# GetTime() %>'
      Runat="Server" /></form>
    </body>
    </html>===================================================<%@ Page Language="C#" %>
    <%@ Import Namespace="System.Data.SqlClient" %><script runat=server>void Page_Load(Object sender , EventArgs e) 
    {
    if (! IsPostBack ) {
    SqlConnection conPubs;
    SqlCommand cmdSelect;
    SqlDataReader dtrAuthors; conPubs = new SqlConnection( @"Server=localhost;Integrated Security=SSPI; Database=Pubs" );
    conPubs.Open();
    cmdSelect = new SqlCommand( "Select au_lname From Authors", conPubs );
    dtrAuthors = cmdSelect.ExecuteReader(); dropAuthors.DataSource = dtrAuthors;
    dropAuthors.DataTextField = "au_lname";
    dropAuthors.DataBind(); dtrAuthors.Close();
    conPubs.Close();
    }
    }</Script><html>
    <head><title>DropDownList.aspx</title></head>
    <body>
    <form Runat="Server"><asp:DropDownList
      ID="dropAuthors"
      Runat="Server" /><asp:Button
      Text="Pick Author!"
      Runat="Server" /></form>
    </body>
    </html>
      

  3.   

    //DropDownList1 你所谓的下拉框
    try
    {
    Conn.Open();
    cmd=new SqlCommand("Select cInvCName from InventoryClass where LEN(cInvCCode)=2",Conn);
    reader=cmd.ExecuteReader();
    DropDownList1.Items.Insert(0,new ListItem("请选择大类","0"));
    DropDownList1.SelectedIndex=0;
    while(reader.Read())
    {
    ListItem item=new ListItem(reader.GetValue(0).ToString(),reader.GetValue(0).ToString());
    DropDownList1.Items.Add(item);
    }
    }
    catch(Exception exc)
    {
    this.Response.Write(exc.Message);
    }
    finally
    {
    reader.Close();
    Conn.Close();
    }
    //文本框建议你使用listBox
    //写法跟上面的是类似的
    try
    {
    cmd=new SqlCommand("select distinct cInvName from Inventory where cInvCCode='"+code+"'",Conn);
    reader=cmd.ExecuteReader();
    while(reader.Read())
    {
    ListItem item=new ListItem(reader.GetValue(0).ToString(),reader.GetValue(0).ToString());
    ListBox1.Items.Add(item);
    }
    }
    catch(Exception exc)
    {
    this.Response.Write(exc.Message);
    }
    finally
    {
    reader.Close();
    Conn.Close();
    }//如果你要在页面加载的时候初始化,你就写道Page_Load里,类似楼上老兄写的