ASP datagrid template textbox question
I want to be able to change the backcolor of a textbox in a datagrid dynamically by using the following onItemDataBound subroutine, but I am not sure of what the correct syntex is. Can someone help?
When I run this code as is, I get the following error:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
It highlights this section of code as the error
tbox.BackColor = Color.Aqua
Public Sub DG_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dg.ItemDataBound
if e.item.cells(0).text="5" then
Dim tbox As TextBox = CType(e.Item.Cells(1).FindControl("Textbox_payment "), TextBox)
tbox.BackColor = Color.aqua
end if
End Sub
This is my html of the datagrid
<ASPATAGRID id="dg" runat="server" GridLines="Both" AutoGenerateColumns="false" onItemDataBound="dg_itemdatabound">
<Columns>
<asp:BoundColumn DataField="order_no" HeaderText="Order Number"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Payment">
<ItemTemplate>
<asp:TextBox Runat="server" id="Textbox_payment" Text='<%#DataBinder.Eval (Container.DataItem, "payment") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</ASPATAGRID>
|