Object reference not set to an instance of an object

  • Thread starter Thread starter Ben
  • Start date Start date
B

Ben

Hi,

i try to change the display value of a radiobuttonlist embedded in a
gridview, but only in normal mode. In edit mode, the radiobuttonlist must
appears.

With this code below, i get what i want in normal mode (displayed values
(yes/no) are ok), but when i try to edit the row, i get:

"Object reference not set to an instance of an object."
at line: lbl.Text = "yes"

So, what's wrong here?
By the way, i also tried with:
If (e.Row.RowState And DataControlRowState.Normal) =
DataControlRowState.Normal Then
....

and with:
'If (e.Row.RowState And DataControlRowState.Normal) = 0 Then
.....

but same error ..
Thanks
Ben

<asp:TemplateField>
<EditItemTemplate>
<asp:RadioButtonList ID="r1" SelectedValue='<%# Bind("myfield") %>'
runat="server">
</asp:RadioButtonList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Labelvp" runat="server" Text='<%# Bind("myfield")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim mf As Boolean = DataBinder.Eval(e.Row.DataItem, "myfield")
Dim lbl As Label = e.Row.FindControl("Labelvp")

If myfield = True Then
lbl.Text = "yes"
Else
lbl.Text = "no"
End If
End If
End Sub
 
You need to detect what the item type is because you have ItemTemplate,
AlternateItem, Footer, Header etc. You have put this in the ItemTemplate,
but there are other Template
types. I cant remember the exact sytax for the event args, but its
something like.


if( e.TemplateType == TemplateItem.ItemTemplate ) // Do Somtehng
 
Thanks for replying. but

In the sub GridView1_RowDataBound, i only can find: e.Row.TemplateControl =
....

I couldn't find e.TemplateType nor TemplateItem.ItemTemplate in the
intellisense.
 
Sorry, wrong operation.

You may want to try something like.

e.Row.Cells[0].FindControl('id');
 
But i did that: i tried with:

Dim lbl As Label = e.Row.FindControl("Labelvp")

and with this:

Dim lbl As Label = e.Row.Cells(5).FindControl("Labelvp")

but both gives the same error ...



Rain said:
Sorry, wrong operation.

You may want to try something like.

e.Row.Cells[0].FindControl('id');


Ben said:
Thanks for replying. but

In the sub GridView1_RowDataBound, i only can find:
e.Row.TemplateControl = ...

I couldn't find e.TemplateType nor TemplateItem.ItemTemplate in the
intellisense.
 
Back
Top