datagrid problem

J

Jack Tonk

Hello. I have a simple datagrid with a column containing the last names of
authors. When a condition is met, I'd like the column to contain a textbox
which allows the user to change the last name. Otherwise, the cell will
contain a label which displays the last name but won't allow any editing.

The code is below. I keep getting Object reference not set to an instance
of an object errors. It seems like this should work. Any ideas?
Thanks in advance.

The .aspx snippet:

<asp:DataGrid ID="dgLastNames" Runat="server" AutoGenerateColumns="false"
DataKeyField=UnProcessedID>
<Columns>
<asp:TemplateColumn HeaderText="Authors">
<ItemTemplate>
<asp:Label ID="lblLastName" Runat="server">last name</asp:Label>
<asp:TextBox id="tbLastName" runat="server">last
name</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

The .aspx.vb snippet:

Private Sub dgLastNames_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
dgLastNames.ItemDataBound

Dim lblLastName As Label
lblLastName= CType(e.Item.FindControl("lblLastName"), Label)
Dim tbLastName As TextBox
tbLastName = CType(e.Item.FindControl("tbLastName"), TextBox)

if somecondition = true then
lblProductType.Visible = False
tbLastName.Visible = true
else
lblProductType.Visible = true
tbLastName.Visible = false
end if
End Sub
 
G

Girish Prabhu

Hi Jack,
Try it this way...

Private Sub dgLastNames_ItemDataBound(sender As Object, e As
DataGridItemEventArgs)
Dim lblLastName As Label
lblLastName= CType(e.Item.cells(0).FindControl("lblLastName"), Label)
Dim tbLastName As TextBox
tbLastName = CType(e.Item.cells(0).FindControl("tbLastName"), TextBox)

if somecondition = true then
lblProductType.Visible = False
tbLastName.Visible = true
else
lblProductType.Visible = true
tbLastName.Visible = false
end if
End sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top