Enable and disable Textbox C #

0

I have a form with the following fields, the problem is that I can not find what is wrong because it does not block the textbox of the first button and those that are in the second button if it blocks them, it is supposed to do the same thing for In addition, the data is the same in the two sections.

  protected void btncomprobar2_Click(object sender, EventArgs e)
    {
     if (txtnom.Text != "")
        {
            cmd = new SqlCommand("select d_direccion,d_edad,d_ciudad from Personal where d_nombres='" + txtnom.Text + "'", conn);
            conn.Open();
     try
            {
                dr = cmd.ExecuteReader();

                if (dr.Read() == true)
                {
                    lblmensaje.Text = "";
                    txtdir.Enabled = true;
                    txtedad.Enabled = true;
                    txtciud.Enabled = true;
                    txtdir.Text = dr["d_direccion"].ToString();
                    txtedad.Text = dr["d_edad"].ToString();
                    txtciud.Text = dr["d_ciudad"].ToString();
                }
                else
                {
                    lblmensaje.Text = "Nombre invalido";
                }
            }
            finally
            {
                conn.Close();
            }
}
  protected void btncomprobar_Click(object sender, EventArgs e){
    if (txtnom.Text != "")
        {
            cmd = new SqlCommand("select d_direccion,d_edad,d_ciudad from Personal where d_nombres='" + txtnom.Text + "'", conn);
            conn.Open();

     try
            {
                dr = cmd.ExecuteReader();

                if (dr.Read() == true)
                {
                    lblmensaje.Text = "";
                    txtdir.Enabled = true;
                    txtedad.Enabled = true;
                    txtciud.Enabled = true;
                    txtdir.Text = dr["d_direccion"].ToString();
                    txtedad.Text = dr["d_edad"].ToString();
                    txtciud.Text = dr["d_ciudad"].ToString();
                }
                else
                {
                    lblmensaje.Text = "Nombre invalido";
                }
            }
            finally
            {
                conn.Close();
            }
 }
    
asked by KlonDTS 12.01.2017 в 19:41
source

1 answer

3

It's quite simple because it blocks you only a few fields:

                lblmensaje.Text = "";
                txtdir.Enabled = true;
                txtedad.Enabled = true;
                txtciud.Enabled = true;
                txtdir.Text = dr["d_direccion"].ToString();
                txtedad.Text = dr["d_edad"].ToString();
                txtciud.Text = dr["d_ciudad"].ToString();

You have those code lines in both events clic , you are always blocking the same controls, I guess you should call the text above and the ones below, you have to use the name that corresponds to them.

On the screen you have 8 textbox , even if they look the same, for ASP are different objects, each one must have its own id (specific name) ...

In the click events that you put with the code botonComprobar and botonComprobar2 you always modify the same 4 objects ( textbox ), check well the name of textbox and modify with the correct name.

In the botonComprobar2 event you should have something like:

                lblmensaje2.Text = "";
                txtdir2.Enabled = true;
                txtedad2.Enabled = true;
                txtciud2.Enabled = true;
                txtdir2.Text = dr["d_direccion2"].ToString();
                txtedad2.Text = dr["d_edad2"].ToString();
                txtciud2.Text = dr["d_ciudad2"].ToString();
    
answered by 12.01.2017 в 20:03