Wrox Programmer Forums
ASP.NET 1.x and 2.0 Application Design Application design with ASP.NET 1.0, 1.1, and 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.x and 2.0 Application Design section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old December 24th, 2003, 09:40 AM
Registered User
Join Date: Dec 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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">

<asp:BoundColumn DataField="order_no" HeaderText="Order Number"></asp:BoundColumn>

<asp:TemplateColumn HeaderText="Payment">
    <asp:TextBox Runat="server" id="Textbox_payment" Text='<%#DataBinder.Eval (Container.DataItem, "payment") %>'></asp:TextBox>


Old December 29th, 2003, 04:01 PM
planoie's Avatar
Friend of Wrox
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

Sounds like it's not finding the control correctly. You could try this instead seeing as you know exactly where the control is in the datagrid item cell:

Dim tbox As TextBox = CType(e.Item.Cells(1).Controls(0), TextBox)

Work smarter, not harder.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Change Text of Textbox inside Template DarkForce ASP.NET 2.0 Basics 3 September 14th, 2012 07:11 AM
Datagrid Item Template question dparsons ASP.NET 1.0 and 1.1 Professional 3 March 14th, 2007 09:08 AM
DataGrid Updates asp:textbox rjirak BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 2 May 1st, 2006 05:36 AM
ASP.NET DataGrid Question desmond047 General .NET 1 August 25th, 2004 01:51 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.