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?
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.