使用AJAX updatepanel在ASP.NET中自动填充文本框,具体取决于在另一个文本框中键入的文本

时间:2015-03-19 19:22:04

标签: c# asp.net plsql asp.net-ajax updatepanel

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

我正在尝试从数据库中根据textbox inputOwner中输入的文本填充textbox 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
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

问题是: - 我需要autopostback =“True”

- 因为我试图连接到oracle db我需要将以下内容添加到我的代码后面: 使用Oracle.ManagedDataAccess.Client; 使用Oracle.ManagedDataAccess.Types;

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

- 我需要在Web的Web配置中更改我的连接字符串

- 另外,我需要删除';'来自我的Oracle SQL行

-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" />