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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling a javascript function with .net

Status
Not open for further replies.

belle9

Programmer
Nov 3, 2004
87
US
I'm trying to call a javascript function that will display a subcategory when the user clicks on a main category. MY JS works fine outside of this project...The second datalist had an ID of "A", so that when I click on an item in the first datalist I'd like to display the data from the second.

Here is my code:

---------------------> The JS Call:
Code:
<asp:datalist id="dlCategories" Runat="server">
<ItemTemplate>
<p class="wht_14" style="margin:8px 14px 8px 0px"><b>
<asp:LinkButton ID="lnkButton" [b]OnClick="javascript:show('A')"[/b] Runat="server" CommandArgument='<%# Container.DataItem("categoryID") %>'>
<%#Databinder.Eval(Container.Dataitem, "CategoryName")%>
</asp:LinkButton>
</b>
</p>
</ItemTemplate>
</asp:datalist>

--------------------------> The data to display

Code:
	<tr>
										 <td [b] id="A" [/b] style="DISPLAY: none">
												<asp:datalist id="dlProducts" runat="server">
													<ItemTemplate>
														<P class="wht_11" style="MARGIN: 0px 18px 8px 0px">
															<asp:LinkButton [b] id="A" [/b] Runat="server" CommandArgument='<%# Container.DataItem("productsID") %>'>
																<%#Container.DataItem("ProductName")%>
															</asp:LinkButton></P>
													</ItemTemplate>
												</asp:datalist>
											</td>
										</tr>

Does my question make sense?
 
If I'm not mistaking, OnClick is a server-side event. Maybe you should attach your onClick event to the <p> element, like this:
Code:
<p class="wht_14" style="margin:8px 14px 8px 0px" onClick="javascript:show('A')"><b>
<asp:LinkButton ID="lnkButton" Runat="server" CommandArgument='<%# Container.DataItem("categoryID") %>'>
<%#Databinder.Eval(Container.Dataitem, "CategoryName")%>
</asp:LinkButton>
</b>
</p>

[morning]
 
lnkButton.Attributes.Add("onclick","javascript: show('A');")

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Ok, once I have this, how to I replace 'A' with this datalist?

Code:
<tr>
                                         <td id="A" style="DISPLAY: none">
                                                <asp:datalist id="dlProducts" runat="server">
                                                    <ItemTemplate>
                                                        <P class="wht_11" style="MARGIN: 0px 18px 8px 0px">
                                                            <asp:LinkButton id="A" Runat="server" CommandArgument='<%# Container.DataItem("productsID") %>'>
                                                                <%#Container.DataItem("ProductName")%>
                                                            </asp:LinkButton></P>
                                                    </ItemTemplate>
                                                </asp:datalist>
                                            </td>
                                        </tr>
 
here is something that i've done with a datagrid...this could work the same way with your datalist...

Code:
  Private Sub dgrSearch_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgrSearch.ItemDataBound
    If e.Item.ItemType = ListItemType.Item Or _
       e.Item.ItemType = ListItemType.AlternatingItem Then

      Dim lbtn As LinkButton
      lbtn = e.Item.FindControl("lbtnGo")
      lbtn.Attributes.Add("onclick", "javascript:window.location.href='wfrmCASearch.aspx?CID=" & CStr(e.Item.DataItem(1)) & "&CName=" & Replace(CStr(e.Item.DataItem(2)), "'", "\'") & "';return false;")
      lbtn.Text = e.Item.DataItem(2)
    End If
  End Sub

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Somehow I can't get either of these to work:

Code:
   lnkButton..Attributes("onclick") = "javascript:alert('Hello! Focus lost from text box!!');"
Code:
lnkButton.Attributes.Add("onclick","javascript: show('A');")

Get this error:
System.NullReferenceException: Object reference not set to an instance of an object.


I have the variable defined.
 
did you do this as well?

Dim lbtn As LinkButton
lbtn = e.Item.FindControl("lnkButton")

? post your code...

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Ok, here is what I have:

My 2 datalists:
Code:
<asp:datalist id="dlCategories" Runat="server">
										<ItemTemplate>
											<p class="wht_14" style="margin:8px 14px 8px 0px"><b>
													<asp:LinkButton ID="lnkButton" Runat="server" CommandArgument='<%# Container.DataItem("categoryID") %>'>
														<%#Container.DataItem("CategoryName")%>
													</asp:LinkButton>
												</b>
											</p>
										</ItemTemplate>
									</asp:datalist>
									<!--start sub categories -->
									<table>
										<tr>
											<td id="A" runat=server style="display: none;"> 
												<asp:datalist id="dlProducts" runat="server">
													<ItemTemplate>
														<P class="wht_11" style="MARGIN: 0px 18px 8px 0px">
															<asp:LinkButton id="btnProducts" Runat="server" CommandArgument='<%# Container.DataItem("productsID") %>'>
																<%#Container.DataItem("ProductName")%>
															</asp:LinkButton></P>
													</ItemTemplate>
												</asp:datalist>
											</td>
										</tr>
									</table>
Corresponding code behind:

Code:
 Private Sub dlCategories_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlCategories.ItemCommand
        Dim objButton As LinkButton

        Dim lbtn As LinkButton
        lbtn = e.Item.FindControl("lnkButton")

        lbtn.Attributes.Add("onclick", "javascript:show('A');")


        objButton = CType(e.CommandSource, LinkButton)

        BindDataList(lblMsg, dlProducts, conString, "getProductsbyCategory", "@categoryID", objButton.CommandArgument)

        bottleshot = "/images/spacer.gif"
    End Sub


  Private Sub dlProducts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlProducts.ItemCommand


        myConnection = New SqlConnection(conString)
        myConnection.Open()

        mycommand = New SqlCommand("getSpecificProduct", myConnection)
        mycommand.CommandType = CommandType.StoredProcedure

        Dim objButton As LinkButton

        objButton = CType(e.CommandSource, LinkButton)

        mycommand.Parameters.Add(New SqlParameter("@productsID", SqlDbType.Int))
        mycommand.Parameters("@productsID").Value = objButton.CommandArgument

        myReader = mycommand.ExecuteReader

        Me.Bind_Data(myReader)

        myConnection.Close()
    End Sub


What I want to achieve can be seen here:
click on expand.
 
You need this...

Code:
Dim lbtn As LinkButton
lbtn = e.Item.FindControl("lnkButton")
lbtn.Attributes.Add("onclick", "javascript:show('A');")

in the ItemDataBound Event...

then you shouldn't need a selected index event...

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
I don't have an itemDataBound event. I have a page_load and these 2 selectedindexes...Do i need an ItemDataBound?

Here's the page_load:

Code:
 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Page.IsPostBack = False Then
            bottleshot = "/images/spacer.gif"
        End If

        BindDataList(lblMsg, dlCategories, conString, "getCategories")


    End Sub
 
yes you need it to accomplish what we're doing...

Code:
Private Sub myDataList_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles myDataList.ItemDataBound
    'Do things here...
  End Sub

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top