从下拉列表中动态填充文本框无法正常工作

时间:2014-05-24 19:28:13

标签: c# asp.net

当我在下拉列表中选择了值时,我想要填充两个文本框(我希望在没有页面重新加载的情况下更改选择时文本框中的数据会发生变化),I写了一些代码来编译我从其他类似问题中收集到的内容,但由于某种原因,它只是在我这里工作的是CS。

public partial class UsersFormPage : Page
{
    protected void userddlistedit_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillBoxes(userddlistedit.SelectedValue);
    }

    private void FillBoxes(string HR_ID)
    {
        // Create a new dataset object
        DataSet dt = new DataSet();
        // Create SqlConnection
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = "Data Source=SQL2008R2SRV;Initial Catalog=employeetrainingtracking;Integrated Security=True";
            conn.Open();
            using (SqlCommand cmd = new SqlCommand())
            {
                // Set the connection on the sql command object
                cmd.Connection = conn;
                cmd.CommandText = "select * from users where HR_ID='" + HR_ID + "'";
                using (SqlDataAdapter adap = new SqlDataAdapter(cmd))
                {
                    adap.Fill(dt);
                }
            }
        }
        if (dt.Tables[0].Rows.Count > 0)
        {
            usernameedit.Text = dt.Tables[0].Rows[0]["Username"].ToString(); //Where column name us the Fields for your Table that you wanted to display in the TextBoxes
            passwordedit.Text = dt.Tables[0].Rows[0]["Password"].ToString();
        }
    }
}

这是影响我的页面的一部分:

  <li class="form-row text-row">
    <label>User:</label>
      <asp:DropDownList ID="userddlistedit" runat="server" CssClass="text-input-dds" DataSourceID="personnelsql" DataTextField="HR_ID" DataValueField="HR_ID" AutoPostBack="True" OnSelectedIndexChanged="userddlistedit_SelectedIndexChanged" />
  </li>
  <li class="form-row text-input-row">
    <label>Username:</label>
    <asp:TextBox name="usernameedit" type="text" class="text-input-lg required" id="usernameedit" runat="server" AutoPostBack="True" />                     
  </li>
  <li class="form-row text-input-row">
    <label>Password:</label>
    <asp:TextBox name="passwordedit" type="text" class="text-input-lg required" id="passwordedit" runat="server" AutoPostBack="True" />
  </li>

我似乎无法弄清楚为什么它不起作用。

这是我的下拉列表的数据源:

                      <asp:SqlDataSource ID="personnelsql" runat="server" 
                      ConnectionString="<%$ ConnectionStrings:employeetrainingtrackingConnectionString %>" 
                      SelectCommand="SELECT * FROM personnel ORDER BY HR_ID">
                  </asp:SqlDataSource>

1 个答案:

答案 0 :(得分:2)

我弄明白我错过了什么,所以我发布了下面的代码,以便人们可以使用它:

以下是CS页面上的代码:

protected void userddlistedit_SelectedIndexChanged(object sender, EventArgs e)
{
    string selectSQL;
    selectSQL = "SELECT * FROM users ";
    selectSQL += "WHERE HR_ID='" + userddlistedit.SelectedItem.Value + "'";
    SqlConnection con = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand(selectSQL, con);
    SqlDataReader reader;

    try
    {
        con.Open();
        reader = cmd.ExecuteReader();
        reader.Read();

        usernameedit.Text = reader["Username"].ToString();
        passwordedit.Text = reader["Password"].ToString();
        accessleveledit.SelectedValue = reader["AccessLevel"].ToString();
        reader.Close();
        lblResults.Text = "";
    }
    finally
    {
        con.Close();
    }
}

这是实际aspx页面中的代码,我添加了两件事: - 我添加了DropDownExtender - 我添加了触发器(在标签之后但是

之前)
<li class="form-row text-row">
                <label>User:</label>
                  <asp:DropDownList ID="userddlistedit" runat="server" CssClass="text-input-dds" AutoPostBack="True" OnSelectedIndexChanged="userddlistedit_SelectedIndexChanged"></asp:DropDownList>
              </li>
              <li class="form-row text-input-row">
                <label>Username:</label>
                <asp:TextBox name="usernameedit" type="text" class="text-input-lg required" id="usernameedit" runat="server"></asp:TextBox>
                <asp:DropDownExtender ID="ExtenderUserEdit" DropDownControlID="userddlistedit" Enabled="true" TargetControlID="usernameedit" runat="server"></asp:DropDownExtender>
                <asp:RequiredFieldValidator runat="server" ID="ValidateUsernameEdit" ControlToValidate="usernameedit" ErrorMessage="Username is required" Display="dynamic" ValidationGroup="EditSection">*</asp:RequiredFieldValidator>
              </li>
              <li class="form-row text-input-row">
                <label>Password:</label>
                <asp:TextBox name="passwordedit" type="text" class="text-input-lg required" id="passwordedit" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator runat="server" ID="ValidatePasswordEdit" ControlToValidate="passwordedit" ErrorMessage="Password is required" Display="dynamic" ValidationGroup="EditSection">*</asp:RequiredFieldValidator>
              </li>  

.....

    <Triggers>
   <asp:AsyncPostBackTrigger ControlID="userddlistedit" EventName="SelectedIndexChanged" />
</Triggers>

我还改变了将下拉列表的数据改为以下内容的方式(在CS页面中添加):

private void UsersListDD()
{
    userddlist.Items.Clear();
    userddlistedit.Items.Clear();
    userddlistdelete.Items.Clear();
    string selectSQL = "SELECT * FROM personnel";

    SqlConnection con = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand(selectSQL, con);
    SqlDataReader reader;

    try
    {
        con.Open();
        reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            ListItem newItem = new ListItem();
            newItem.Text = reader["FirstName"] + " " + reader["LastName"];
            newItem.Value = reader["HR_ID"].ToString();
            userddlist.Items.Add(newItem);
            userddlistedit.Items.Add(newItem);
            userddlistdelete.Items.Add(newItem);
        }
        reader.Close();
    }
    finally
    {
        con.Close();
    }
}

希望这有助于某人。