0

I have a web form in which I am trying to set the textbox control property to false on textchanged event. I have multiple textbox's and I have taken these into a panel. Now I am checking a condition within the text changed event of the textbox. If the condition matches then there will be no change but if not then I will set the enable property of the textbox's within panel control to false. This is what I am doing-

<asp:TextBox ID="TextBox1" runat="server" Width="150px" AutoPostBack="True" 
        ontextchanged="TextBox1_TextChanged"></asp:TextBox>
<asp:Panel ID="Panel1" runat="server">
            <asp:TextBox ID="txt1" runat="server"></asp:TextBox>
            <asp:TextBox ID="txt2" runat="server"></asp:TextBox>
            <asp:TextBox ID="txt3" runat="server"></asp:TextBox>
</asp:Panel>

my cs code-

protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        SqlConnection cons1 = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
        cons1.Open();
        SqlCommand scmd1 = new SqlCommand("select name from tbl_names where name='"+TextBox1.Text+"'", cons1);
        SqlDataReader sdr1 = scmd1.ExecuteReader();
        if (sdr1.HasRows)
        {
            while (sdr1.Read())
            {
                Panel1.Visible = true;
                Control ctrl = new Control();
                foreach (Control c in ctrl.Controls)
                {
                    if (c is TextBox && c.ID.StartsWith("txt"))
                        ((TextBox)c).ReadOnly = false;
                }
            }
        }
        else
        {
            Panel1.Visible = true;
            Control ctrl1 = new Control();
            foreach (Control c in ctrl1.Controls)
            {
                if (c is TextBox && c.ID.StartsWith("txt"))
                    ((TextBox)c).ReadOnly = true;
            }
        }
        cons1.Close();
    }

Please guide me why this is not working?

Omi
  • 427
  • 7
  • 21
  • 42
  • 2
    Why are you creating a new empty control and then trying to loop round its controls (which will be empty)? Have you debugged this? – Kevin Main May 08 '14 at 11:42

1 Answers1

2

I think your

Control ctrl = new Control();
foreach (Control c in ctrl.Controls)

Should be

foreach (Control c in Panel1.Controls)

Also, you're talking about Enabled property but you do not use it in your code. But from what you're saying, I don't think there will be textchanged events thrown if the textbox is be disabled.

Tarec
  • 3,268
  • 4
  • 30
  • 47
  • 2
    FYI: There is a subtle difference between the Enabled and ReadOnly properties (see http://stackoverflow.com/a/3089258/1191903) – Kevin Main May 08 '14 at 11:50
  • The title says "Unable to get back textbox enable property", so that's why I mentioned it. Unless you're adressing those words to OP, then I agree, the difference is meaningful. – Tarec May 08 '14 at 11:57
  • Yeah sorry, meant that to be for the OP - to clarify which one they are using as they are different properties and not interchangeable. – Kevin Main May 08 '14 at 12:00
  • I am extremely sorry I was trying to set readonly property. By mistake I have written Enable. – Omi May 08 '14 at 12:37
  • @Tarec Hello Tarec, Actually the modification you suggested works but there is a problem which I am facing is, Since I am trying to set the controls readonly property to false on text changed event of the textbox, in this event I am trying match the input string of the textbox with that of my database record, and if it matches then the readonly property will be false and if not then it will be true. I am successful in setting this readonly property to true when record does not match but then the next time when I try to give the matching string then this readonly property is not changing. – Omi May 09 '14 at 05:40
  • Please guide me what else I need to do in this text changed event. – Omi May 09 '14 at 05:42