OnRowCreated - Setting the properties of a column control
-
Hi. I have a GridView bound to a DataTable. The columns of the GridView are TemplateColumns with a TextBox in it. The textbox columns appear with the values of the DataTable. What I am trying to do is setting the TextBox to ReadOnly if certain conditions are met. I tried to do this within a handle to OnRowCreated (I get the created row in the eventargs e.Row). The problem is that I don't know how to get the TextBox object of a specific column in that row and set it's properties to ReadOnly. Any help? Thanks!
-
Hi. I have a GridView bound to a DataTable. The columns of the GridView are TemplateColumns with a TextBox in it. The textbox columns appear with the values of the DataTable. What I am trying to do is setting the TextBox to ReadOnly if certain conditions are met. I tried to do this within a handle to OnRowCreated (I get the created row in the eventargs e.Row). The problem is that I don't know how to get the TextBox object of a specific column in that row and set it's properties to ReadOnly. Any help? Thanks!
To get reference to the TextBox, you simply use the
FindControl
method in the event handler of the RowCreated event:TextBox textBox = e.Row.FindControl('TextBoxId') as TextBox;
You now can set the ReadOnly property of the TextBox. However, you'd better use the
RowDataBound
event to set the ReadOnly property as theRowCreated
event always happens, and you might not need that on postback. In addition, you can also use the databinding expression to set theReadOnly
property of the TextBox in the control declaration:... ReadOnly='<%# ... %>' ...