Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to add an onclick javascript event to a LinkButton 1

Status
Not open for further replies.

cesark

Programmer
Dec 20, 2003
621
0
0
Hi,

I am trying to add some javascript functionality (an onclick event) to a LinkButton column generated by a repeater control, but I receive this error:

Object reference not set to an instance of an object.
Line 76: CType(offers_list.FindControl("of_detail"), LinkButton).Attributes.Add("onclick","return function2();")


This is my code:
Code:
Sub Page_Load(Sender As Object, E As EventArgs)

 CType(offers_list.FindControl("of_detail"), LinkButton).Attributes.Add("onclick","return function2();")  

End Sub

And this is the repeater control:
Code:
          [b]<asp:repeater ID="offers_list" runat="server">[/b] 
            <itemtemplate> 
              <tr> 
                <td> 
                  <%# DataBinder.Eval(Container.DataItem, "theDate", "{0:d}") %></td>
                <td><asp:linkbutton ID="user_page" OnClick="us_pag" Text='<%# Server.HtmlEncode(DataBinder.Eval(Container.DataItem, "company_name")) %>' runat="server"></asp:linkbutton></td>
                <td>[b]<asp:linkbutton ID="of_detail" OnClick="details" Text='<%# Server.HtmlEncode(DataBinder.Eval(Container.DataItem, "Offer_title")) %>' runat="server"></asp:linkbutton>[/b]</td>
                <td> 
                  <%# DataBinder.Eval(Container.DataItem, "City_name") %></td>
              </tr>
            </itemtemplate>
          </asp:repeater>


What is wrong?

Thank you
 
Yes, it won't be able to find the control as it is within a repeater (and as that repetear can generate lots of rows, the LinkButton may get generated several times).

You'll have to use the OnItemDataBound event of the repeater, find the control in there and then add the onclick event.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi ca8msm,

I added the OnItemDataBound event handler to repeater control as you said, and it does the same:
Code:
Sub add_js(Sender As Object, e As RepeaterItemEventArgs)

 CType(e.Item.FindControl("of_detail"), LinkButton).Attributes.Add("onclick","return function2();")  

End Sub


<asp:repeater ID="offers_list" runat="server" OnItemDataBound="add_js"> 
   <itemtemplate> 
         ... 
   </itemtemplate>
</asp:repeater>

 
Try making sure the item has actually been found e.g. if you step through this, you should see if "myLinkButton" actually returns an object or not
Code:
        Dim myLinkButton As New LinkButton
        myLinkButton = e.Item.FindControl("of_detail")
        myLinkButton.Attributes.Add("onclick", "return function2();")




____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
If I try this, the error is the same but pointing to this line:

Object reference not set to an instance of an object.
Line 323: myLinkButton.Attributes.Add("onclick", "return function2();")

So, the problem seems to be when trying to add the attribute...
 
Yes, it will be the same error if the control has not been found. Did you step through the code and see if it returned an object or not?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I don' t understand what you mean with 'if it returned an object or not'. And how can I check this?
 
I mean does myLinkButton equal Nothing or is it an object?

Are you using an IDE with debugging capabilities?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I am not using an IDE, I program all my code in every aspx page (inline)
 
It will make it a lot easier to debug projects if you do use an IDE to debug.

If you want to use something that doesn't have debugging capabilites (such as Notepad for example) then the easiest way is probably write "If" and "Response.Write" statements to check whether the object is Nothing.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
cesark - not to interrupt your discussion but I have a single example of creating a javascript hyperlink during the databound event of a grid and thought perhaps there might be a subtle insight here. This code is set up to call a pop up calendar next to a date textbox in the grid:
Code:
Sub Grid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) 
If e.Item.ItemType = ListItemType.EditItem Then
'prepare link for calendar popup, access textbox's client ID, use it in link
Dim sTextBoxName As String = e.Item.Cells(0).FindControl("txtwkDate").ClientID()
CType(e.Item.FindControl("lnkCalendar"), HyperLink).NavigateUrl = "javascript:calendar_window=window.open('wecalendar.aspx?formname=Form1." & sTextBoxName & "','Pick_A_Date','width=154,height=210');calendar_window.focus();"
End If
Just as shot in the dark but it does involve the creation of a javascript function during the OnDataBind event - so thought I'd share this with you.
 
I saw an article that explains (among many other things) how to achieve exactly what I want:
...
Adding a confirmation popup dialog is simple—it just needs some JavaScript in response to the hyperlink’s onClick event. This is done by adding an entry in the control’s Attributes collection when the link is created—that is, when the Repeater’s item is created. I just need to handle the Repeater’s ItemCreated event for both the odd and even items, get a reference to the Delete LinkButton, and add the JavaScript confirmation popup:
Code:
Private Sub Blog_ItemCreated(...) Handles Blog.ItemCreated
If e.Item.ItemType <> ListItemType.AlternatingItem AndAlso _
e.Item.ItemType <> ListItemType.Item Then Exit Sub
Dim lnkDelete As LinkButton = CType( _
e.Item.FindControl(“DeleteMessage”), LinkButton)
lnkDelete.Attributes.Add(“onclick”, _
“return confirm(‘Are you sure you want to delete this” & _ 
“message?’);”)
End Sub

.....

Complete article:


But I don’ t understand why my code it doesn’ t work..:
Code:
Sub add_js(Sender As Object, e As RepeaterItemEventArgs)

 CType(e.Item.FindControl(“of_detail”), LinkButton).Attributes.Add(“onclick”,”return function2();”)  

End Sub


<asp:repeater ID=”offers_list” runat=”server” [b]OnItemCreated=”add_js”[/b]> 
   <itemtemplate> 
         ... 
   </itemtemplate>
</asp:repeater>

Is it not the same as it is in the article?
 
Did you do what I suggested?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Did you do what I suggested?
If you really know how to do what you suggested, please, post a code sample. For example:
Code:
Sub Page_Load(Sender As Object, E As EventArgs)

 Dim myLinkButton As New LinkButton
 myLinkButton = e.Item.FindControl("of_detail")

 If myLinkButton = Object Then
  Response.Write(“Hello! I am an object called myLinkButton.”)
End If

End Sub

I am sure this is not the way to check it, but most probably you know it, so, please post a simple example
 
How about:
Code:
If myLinkButton = Nothing Then
 Response.Write(“I'm not an object so we have a problem...”)
End If



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I tried:
Code:
Sub add_js(Sender As Object, e As RepeaterItemEventArgs)

  Dim myLinkButton As New LinkButton
  myLinkButton = e.Item.FindControl("of_detail")

 If myLinkButton Is Nothing Then
  Response.Write(“I am not an object”)
 Else
  Response.Write(“I am an object”)
 End If

End Sub


<asp:repeater ID=”offers_list” runat=”server” [b]OnItemCreated=”add_js”[/b]> 
   <itemtemplate> 
         ... 
   </itemtemplate>
</asp:repeater>

And the results are:

I am not an object.
I am an object.
I am an object.
I am an object.
I am an object.
I am an object.
I am an object.
Etc,..

I suppose this is for every item created in the repeater list. And for the first one says that it isn’ t an object, but in my repeater I have a ‘<headertemplate>’ to show titles for every column, so in this first row there isn’ t any linkbutton.

So, myLinkButton is well recognized as an object for every item created in the page..
 
Ahh, your code above didn't show that you had a header template. In that case the problem is that it can't find the LinkButton onject in the HeaderTemplate. I presume there isn't one, so in your ItemDataBound event use:
Code:
        If e.Item.ItemType = ListItemType.AlternatingItem OrElse e.Item.ItemType = ListItemType.Item Then

        End If
and place your current code within that If statement.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 

Yes! Finally works :), it was that header..

This is the code I implemented:
Code:
Sub add_js(Sender As Object, e As RepeaterItemEventArgs)

 If e.Item.ItemType = ListItemType.AlternatingItem OrElse e.Item.ItemType = ListItemType.Item Then
    Ctype(e.Item.FindControl("of_detail"), LinkButton).Attributes.Add("onclick","return function2();") 
 End If  

End Sub


<asp:repeater ID=”offers_list” runat=”server” OnItemCreated=”add_js”> 
   <itemtemplate> 
         ... 
   </itemtemplate>
</asp:repeater>

Thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top