Object reference not set to an instance of an object

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
 
R

Rain

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
 
B

Ben

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.
 
R

Rain

Sorry, wrong operation.

You may want to try something like.

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

Ben

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.
 

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,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top