简体   繁体   English

在onblur文本框事件asp.net C#上查询数据库

[英]query a database on onblur textbox event asp.net C#

I want to query database on onblur event of a textbox. 我想在文本框的onblur事件上查询数据库。 Simple I want is that when I enter the ID in first textbox and after onblur event occurs, the name of the respective ID from database is shown in another textbox or label. 我想简单的是,当我在第一个文本框中输入ID并在发生onblur事件之后,数据库中各个ID的名称显示在另一个文本框或标签中。

You can try with this code, edit this code in your event 您可以尝试使用此代码,并在活动中编辑此代码

   var yourParameter = ....; //Your Value from your event , the value of textbox
   var mySelectQuery = ....; //Enter name stored procedure
   var myConnectionString = ....; //Enter string connection

   using(var myConnection = new SqlConnection(myConnectionString))
   {
   using(var myCommand = new SqlCommand(mySelectQuery, myConnection))
   {
      myConnection.Open();
      myCommand.CommandType =  CommandType.StoredProcedure;
      myCommand.Parameters.Add(
    new SqlParameter("@YourParameter", yourParameter));
      SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
      while(myReader.Read()) 
      {
        Console.WriteLine(myReader.GetString(0));
      }
   }
   } 

Use ontextboxchanged event for this. 为此,请使用ontextboxchanged事件。

<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged" />

  protected void TextBox1_TextChanged(object sender, EventArgs e)
{
   //call the database stored procedure
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM