4

I have a GridView on a website, that have different controls in each row (eg.: textbox, label, dropdownlist). I need to find all textboxes and set the enabled property to false, so the user won't be able to edit them. I tried the code below, but it dosn't work, 'c' never recognised as a textbox, so it never changes the property.

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (a)
    {
        foreach (Control c in e.Row.Controls)
        {
            if (c is TextBox)
            {
                ((TextBox)(c)).Enabled = false;
            }
        }
    }
}
User6667769
  • 745
  • 1
  • 7
  • 25
Adam Miklosi
  • 764
  • 5
  • 18
  • 28
  • What's wrong with disabling them in the markup? – Andrei Apr 25 '17 at 09:29
  • All textboxes must be enabled by default, I have to disable them later, when 'a' is true... – Adam Miklosi Apr 25 '17 at 09:40
  • What is `a`???? – Ullas Apr 25 '17 at 09:46
  • Are there many textboxes? And can you expand on what condition "a" stands for? – Andrei Apr 25 '17 at 09:51
  • 'a' is just a simple bool variable. I have 34 texboxes, 2 dropdownlists and 8 labels. – Adam Miklosi Apr 25 '17 at 09:51
  • @Adam, is "a" a page class property? When is it calculated? Does it change from row to row? You got to share important details if you want people to help you – Andrei Apr 25 '17 at 09:52
  • The problem is not with the 'a' variable, it's true, it's not relevant for this problem. The problem is in the foreach loop, because 'c' is never recognised as a textbox. – Adam Miklosi Apr 25 '17 at 09:55
  • 1
    have you checked control list in e.Row.Controls ? – Komal Apr 25 '17 at 10:01
  • 1
    @Adam, it is not a problem, I agree, but it may be a key to an easier working solution. I gave some suggestions in the answer – Andrei Apr 25 '17 at 10:07
  • @Komal Yes, and I just found, there is no Texbox control in it. There are "{System.Web.UI.WebControls.DataControlFieldCell}" controls that have controls. Where the texbox is there is only "{System.Web.UI.LiteralControl}", "{System.Web.UI.UpdatePanel}" and "{Text = "test"}" controls only and not textbox. I found something in the MSDN network, but it does the same, not recognise my control as a textbox. [MSDN](https://msdn.microsoft.com/en-us/library/yt340bh4.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1) – Adam Miklosi Apr 25 '17 at 10:21
  • 1
    Here e.Row.Controls will get the controls of table generated only. By doing debugging you can see one of the properties as 'TagName' listed which show 'td'. Textbox is inside of that 'td'. So as per suggested the solution by @Andrei - TextBox txtName = (e.Row.FindControl("txtName") as TextBox); -- gives you textbox control. – Komal Apr 25 '17 at 11:05
  • @Komal Yes, I know there is a way like this, to find controls... I hoped there is an easier way, due I have lots of controls on my gridview. But anyway, thank you both for your help Komal and Andrei. – Adam Miklosi Apr 25 '17 at 11:29

4 Answers4

3

I think you should try like this:

TextBox tb = e.Row.FindControl("textbox_name") as TextBox;
tb.Enabled = false;
  • 1
    Thank you, it works fine. I just found, it's better for me to use the readonly property instead of enabled. – Adam Miklosi Apr 25 '17 at 12:45
  • 1
    Yes, in the context of a textbox, readonly allows the user to set focus to and select and copy the text but not modify it. A disabled TextBox does not allow any interaction whatsoever. [MSDN](https://social.msdn.microsoft.com/Forums/en-US/e3999a28-9923-457f-b52a-96c91059e455/readonly-vs-enabled?forum=Vsexpressvcs) – József Kökény Apr 25 '17 at 12:51
  • Glad I helped you.If you have any problem write. – József Kökény Dec 01 '17 at 13:10
1

Your textboxes must be nested within other controls, most likely cells inside the row. That is why you cannot find them just iterating through immediate children.

If you have a list of IDs of the text boxes, you should use FindControl:

((TextBox)e.Row.FindControl("TextBoxID")).Enabled = false;

Otherwise you will need to recursively find your controls of necessary type. See this thread for code sample.

One more option, if a is relatively easy to calculate, is to use in the markup directly, like so:

<asp:TextBox ... Enabled='<%# a %>' />

This depends a lot on the details of how a is derived. If it is a protected or public field of the page class, just the code above should work. If it is calculated based on row, you may need to turn it into protected method and pass params into it:

Enabled='<%# GetEnabled(Eval("Prop1"), Eval("Prop2")) %>'
Community
  • 1
  • 1
Andrei
  • 55,890
  • 9
  • 87
  • 108
1

And also want to put some updation. There are different types of rows in gridview (header,footer,datarow,etc)

so for making little faster for the control find.

Try below (check the if condition)

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //Find the TextBox control.
                TextBox txtName = (e.Row.FindControl("txtName") as TextBox);
                txtName.Enabled = false;

                //or
                TextBox txtName1 = (TextBox)e.Row.FindControl("txtName");
                txtName1.Enabled = false;
            }
        }
Komal
  • 304
  • 1
  • 7
0

You should search controls inside the cell instead of Row.

foreach (TableCell cell in e.Row.Cells)
{
     foreach (Control c in cell.Controls)
     {
           if (c is TextBox)
           {
                  ((TextBox)(c)).Enabled = false;
           }
     }
}
sachindaK
  • 31
  • 5