根据在另一个文本框中键入的文本,使用 AJAX 更新面板自动填充 ASP.NET 中的文本框

Auto Fill Textbox in ASP.NET using AJAX updatepanel depending on the text typed in another textbox

在 ASP.net 4.5 C# 中使用 AJAX UpdatePanel,

我正在尝试根据在文本框 inputOwner 中输入的文本从数据库中填充文本框 outputOwnDept。另外,我试图在用户开始在 inputOwner 中输入时触发此搜索。

我做错了什么?

这是我的 aspx:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">   
<ContentTemplate>
<div class="col-lg-6">
<div class="well bs-component">
   <fieldset>
    <legend>Ownership and Support</legend>
    <div class="form-group">
      <label for="inputOwner" class="col-lg-2 control-label">IT Owner's Network ID:</label>
      <div class="col-lg-10">
          <asp:TextBox ID="inputOwner" runat="server" OnTextChanged="inputOwnerTextChanged"></asp:TextBox> 
      </div>
    </div> 
    </fieldset>
</div>
</div>
<div class="col-lg-6">
<div class="well bs-component">
   <fieldset>
    <div class="form-group">
      <label for="outputOwnDept" class="col-lg-2 control-label">Owner's Department:</label>
      <div class="col-lg-10">
        <asp:TextBox ID="" runat="server" ReadOnly="true"></asp:TextBox> 
      </div>
    </div>
    </fieldset>
</div>
</div>
</ContentTemplate>
<triggers><asp:AsyncPostBackTrigger ControlID="inputOwner" EventName="TextChanged" /></triggers>
</asp:UpdatePanel>

下面是我的代码:

public void inputOwnerTextChanged(object sender, EventArgs e)
        {

            DataTable owndept = new DataTable();
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["PEOPLESOFT"].ConnectionString))
            {

                try
                {
                    string NetworkID = inputOwner.Text;
                    SqlDataAdapter adapter = new SqlDataAdapter("select max(vw.DEPTNAME) as DEPTNAME from sysadm.PS_PH_ACTIVE_EE_VW vw where vw.NETWORK_ID like '%" + NetworkID + "%';", con);
                    adapter.Fill(owndept);

                    outputOwnDept.Text = (string)DataBinder.Eval(owndept, "DEPTNAME");
                    outputOwnDept.DataBind();
                }
                catch (Exception ex)
                {
                    // Handle the error
                }
            }
        }

问题是: -我需要autopostback = "True"

-因为我试图连接到 oracle 数据库,所以我需要将以下内容添加到我的代码中: 使用 Oracle.ManagedDataAccess.Client; 使用 Oracle.ManagedDataAccess.Types;

-我还需要使用不同的函数来连接到 oracle 数据库

-我需要在 Web.config for Oracle

中更改我的连接字符串

-此外,我需要删除“;”来自我的 SQL 行 Oracle

-aspx:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">   
<ContentTemplate>
<div class="col-lg-10">
<div class="well bs-component">
   <fieldset>
    <legend>Ownership and Support</legend>
    <div class="form-group">
      <label for="inputOwner" class="col-lg-2 control-label">IT Owner:</label>
      <div class="col-lg-10">
          <asp:TextBox ID="inputOwner" runat="server" OnTextChanged="inputOwnerTextChanged" autopostback="True" placeholder="IT     Owner's Network ID"></asp:TextBox> 
      </div>
    </div> 
    <div class="form-group">
      <label for="outputOwnDept" class="col-lg-2 control-label">Owner's     Department:</label>
      <div class="col-lg-10">
          <asp:Label ID="outputOwnDept" runat="server" Text="" BackColor="#F0F0F0"></asp:Label>
      </div>
    </div>
    </fieldset>
</div>
</div>
</ContentTemplate>
<triggers><asp:AsyncPostBackTrigger ControlID="inputOwner" EventName="TextChanged" /></triggers>
</asp:UpdatePanel>  

代码隐藏:

protected void inputOwnerTextChanged(object sender, EventArgs e)
    {

        DataTable owndept = new DataTable();
        using (OracleConnection con = new OracleConnection(ConfigurationManager.ConnectionStrings["EMPLOYMENTEXP.HRPRD"].ConnectionString))
        {

            try
            {
                string NetworkID = inputOwner.Text;
                OracleDataAdapter adapter = new OracleDataAdapter("select max(vw.DEPTNAME) as DEPTNAME from sysadm.PS_PH_ACTIVE_EE_VW vw where vw.NETWORK_ID = '" + NetworkID + "'", con);

                adapter.Fill(owndept);

                outputOwnDept.Text = owndept.Rows[0].Field<String>(0);
            }
            catch (SqlException ex)
            {
                outputOwnDept.Text = ex.ToString();
            }
        }
    }

Web.config:

<add name="EMPLOYMENTEXP.HRPRD" connectionString="DATA SOURCE=host:port/database;USER ID=xxx;PASSWORD=xxx;" providerName="Oracle.ManagedDataAccess.Client" />