+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 19

Thread: Call Serverside Function from A Tag

  1. #1
    Barn Frequenter noFriends is on a distinguished road noFriends's Avatar
    Join Date
    Apr 2008
    Posts
    176
    Rep Power
    4

    Call Serverside Function from A Tag

    hi guys, I am busy creating my first .Net website and I have another problem.

    I have a catalogue page, see attached, the feature title on the right is always the first title of the results, but when I click on another title, I would like that feature title on the right to change, I have tried a tags, linkbutton, asp:hyperlink, and not getting it to work, here is the a tag example:
    Code:
    <a id="a_changeFeature" onclick="change_Feature('<%#Container.DataItem("ISBN13")%>')" runat="server"><%#Container.DataItem("rm_TL")%></a>
    
    it comes out in the HTML as:
    Code:
    <a id="a_changeFeature" onclick="change_Feature('9781919855295')" runat="server">Turning Points in History</a>
    
    the code behind function looks like this:
    Code:
        Public Sub change_Feature(ByRef str_ISBN As String)
            Dim con As New SqlConnection("****")
            Dim strSQL As String = "SELECT * FROM intranet_EdtPublication WHERE ISBN13 = '" & str_ISBN & "'"
    
            Dim cmd As New SqlCommand(strSQL, con)
    
            con.Open()
    
            Dim sqlReader As SqlDataReader = cmd.ExecuteReader()
    
            rpt_Feature_Title.DataSource = sqlReader
            rpt_Feature_Title.DataBind()
        End Sub
    
    any ideas why this is not working?
    Attached Images
    Last edited by noFriends; June 29th, 2009 at 06:19 AM. Reason: add image

  2. #2
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    If your sub is in code behind then you need to use a .NET control to call it. I would suggest a <asp:linkbutton> control. You wouldn't pass a value to the sub, you normally have something like:-
    Code:
    public sub change_feature(ByVal s as object, ByVal e as EventArgs)
     
     
    end sub
    
    Is it in a repeater or datalist or something?

    The way you have it at the moment is more the way you'd call a JS function.

  3. #3
    Barn Frequenter noFriends is on a distinguished road noFriends's Avatar
    Join Date
    Apr 2008
    Posts
    176
    Rep Power
    4

    Hi RR, it is in a asp:Repeater, I need to pass the value of the ISBN to the function so that I know which one to load.
    Code:
    			    <asp:Repeater ID="rpt_Catalogue" runat="server">
    			        <ItemTemplate>
    			            <div id="catalogue_row">                                        
                                    <a id="a_changeFeature" onclick="change_Feature('<%#Container.DataItem("ISBN13")%>')" runat="server"><%#Container.DataItem("rm_TL")%></a><br />		                			                    
    			                </div>
    			            </div>  
    			        </ItemTemplate>
    			    </asp:Repeater>
    
    how would I use a linkbutton without passing the value to the sub?

  4. #4
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    OK. Had a quick recap on one of my apps and I think this is the way to handle it.
    Code:
    <asp:repeater id="rep1" runat="server" onitemcommand="rep1OnItemCommand">
    <itemtemplate>
    <asp:linkbutton id="lnk1" runat="server" CommandName="link" CommandArgument="<%#Container.DataItem("ISBN13")%>" />
    </itemtemplate>
    </asp:repeater>
    
    code behind
    Code:
    Public Sub rep1OnItemCommand(ByVal s as Object, ByVal e as RepeaterCommandEventArgs)
    if e.CommandName="link" then
    Dim value as string = e.CommandArgument
    end if
    end sub
    
    I think that should do it. That's from a bit of a distant memory though so might need a bit of adjusting...

  5. #5
    Barn Frequenter noFriends is on a distinguished road noFriends's Avatar
    Join Date
    Apr 2008
    Posts
    176
    Rep Power
    4

    mmm, strange, it doesn't show on my page?
    even though when I look at the source code it shows:
    Code:
    <asp:LinkButton CommandName="change_Feature" CommandArgument="9781919855295" runat="server" Text="Turning Points in History" /><br />
    
    here is the aspx page:
    Code:
    			    <asp:Repeater ID="rpt_Catalogue" runat="server" OnItemCommand="Cat_OnItemCommand">
    			        <ItemTemplate>                                        
                                    <asp:LinkButton CommandName="change_Feature" CommandArgument="<%#Container.DataItem("ISBN13")%>" runat="server" Text="<%#Container.DataItem("rm_TL")%>" /><br />                                
    			                    <%#Container.DataItem("strDesc")%>
    			            </div>  
    			        </ItemTemplate>
    			    </asp:Repeater>
    
    and the code behind
    Code:
        Public Sub Cat_OnItemCommand(ByVal s As Object, ByVal e As RepeaterCommandEventArgs)
    
            If e.CommandName = "change_Feature" Then
                Dim con As New SqlConnection("****")
                Dim strSQL As String = "SELECT * FROM intranet_EdtPublication WHERE sap_IS13 = '" & e.CommandArgument & "'"
    
                Dim cmd As New SqlCommand(strSQL, con)
    
                con.Open()
    
                Dim sqlReader As SqlDataReader = cmd.ExecuteReader()
    
                rpt_Feature_Title.DataSource = sqlReader
                rpt_Feature_Title.DataBind()
            End If
        End Sub
    
    why won't it show? Its not giving any errors though

  6. #6
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Try adding an ID to your linkbutton.

    You shouldn't see .net code in your HTML source code.

  7. #7
    Barn Frequenter noFriends is on a distinguished road noFriends's Avatar
    Join Date
    Apr 2008
    Posts
    176
    Rep Power
    4

    have a look here:

    --Pearson Education--

    its the temp link

    ID did not change anything
    Last edited by noFriends; June 29th, 2009 at 07:10 AM.

  8. #8
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Hmmm....Something is definitely not right with that...

    Not entirely sure why the repeater seems to be doing it's job but then it's not rendering the linkbutton correctly...

    Could you post the whole .aspx page (and code behind if you have it)

  9. #9
    Barn Frequenter noFriends is on a distinguished road noFriends's Avatar
    Join Date
    Apr 2008
    Posts
    176
    Rep Power
    4

    ok, here is the aspx page:
    Code:
    <%@ Page Language="VB" Debug="true" AutoEventWireup="true" CodeFile="catalogue.aspx.vb" Inherits="catalogue" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <title>--Pearson Education--</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <link href="include/p_l1.css" type="text/css" rel="stylesheet">
    <script language="javascript">
    <!--
    sfHover = function() {
    var sfEls = document.getElementById("cat_nav_items").getElementsByTagName("LI");
    for (var i=0; i<sfEls.length; i++) {
    	sfEls[i].onmouseover=function() {
    		this.className+=" sfhover";
    	}
    	sfEls[i].onmouseout=function() {
    		this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
    	}
    }
    }
    if (window.attachEvent) window.attachEvent("onload", sfHover);
    -->
    </script>
    </head>
    <body onLoad="javascript:call startList;">
    <div id="wrapper">
    	<div id="header"><img src="images/header.jpg"></div>
    	<div id="maincontent">
    		<!--#include file="include/l1_header.aspx"-->
    		<asp:image runat="server" id="img_Header" ></asp:image>
    		<%=Session("strMenu")%>
    		<div id="catalogue_section">
    		    <form id="frmCat" runat="server">
    			<br/>
    			    <br/>
    			    <asp:image runat="server" id="img_CatHeader" ></asp:image><br />	
    			    <asp:Repeater ID="rpt_Catalogue" runat="server" OnItemCommand="Cat_OnItemCommand">
    			        <ItemTemplate>
    			            <div id="catalogue_row">
    			                <div id="catalogue_row_image">
    			                    <asp:image Width="62px" CssClass="catalogue_image" BorderColor="Black" BorderWidth="1px" runat="server" ImageUrl='<%#check_Image(Databinder.Eval(Container.DataItem, "ISBN13"))%>' ></asp:image>
                                </div>		
                                <div id="catalogue_row_text">                                            
                                    <asp:LinkButton ID="lb_Change" CssClass="body_text" CommandName="change_Feature" CommandArgument="<%#Container.DataItem("ISBN13")%>" runat="server" Text="<%#Container.DataItem("rm_TL")%>" /><br />                                
    			                    <%#Container.DataItem("strDesc")%>
    			                </div>
    			            </div>  
    			        </ItemTemplate>
    			    </asp:Repeater>
                </form>
    		</div>
    		<div id="feature_title_right">
    		    <asp:image style="float:left;" runat="server" id="img_khari_buy" ImageUrl="images/kalahari_ENG.gif" ></asp:image>
    		    <asp:image style="float:right; padding-right:10px;" runat="server" id="img_look_inside" ImageUrl="images/inside_ENG.gif" ></asp:image>
    		    <br /><br /><br />
    		    <asp:Repeater ID="rpt_Feature_Title" runat="server">
    		        <ItemTemplate>
    		            <asp:image Width="173px" CssClass="catalogue_image" BorderColor="Black" BorderWidth="1px" runat="server" ImageUrl='<%#check_Image(Databinder.Eval(Container.DataItem, "ISBN13"))%>' ></asp:image>
    		            <br /><br />
    		            <strong><%#DataBinder.Eval(Container.DataItem, "rm_TL")%></strong><br />
    		            <%#check_Display_Data("Author", Databinder.Eval(Container.DataItem, "rm_AU"))%><br />
    		            <%#check_Display_Data("ISBN", DataBinder.Eval(Container.DataItem, "ISBN13"))%><br />
    		                <br />
    		            <%#check_Display_Data("Imprint", DataBinder.Eval(Container.DataItem, "rm_IM"))%><br />
    		            <%#check_Display_Data("Publication Date", DataBinder.Eval(Container.DataItem, "rm_PDSAF"))%><br />
    		            <%''#check_Display_Data("Age Group", DataBinder.Eval(Container.DataItem, "rm_RM"))%><br />
    		                <br />
    		            <%#check_Display_Data("Extent", DataBinder.Eval(Container.DataItem, "rm_PP"))%>pp<br />
    		            <%''#check_Display_Data("Format", DataBinder.Eval(Container.DataItem, "rm_PS"))%><br />
    		            <%#check_Display_Data("Mass", DataBinder.Eval(Container.DataItem, "rm_WT"))%>gm<br />
    		                <br />
    		            <%#check_Display_Data("Description", DataBinder.Eval(Container.DataItem, "rm_DE"))%><br />
    		                <br />
    		            <%''#check_Display_Data("Price (incl. VAT)", DataBinder.Eval(Container.DataItem, "rm_PRRND"))%>
    		        </ItemTemplate>
    		    </asp:Repeater>
    		</div>
    		<div id="footer_blueline"></div>
    	</div>	
    	    
    	<div id="footer">
    	    <div id="footer_maintext">Copyright Pearson Education. All rights reserved. Legal and Privacy Notice. Prices subject to change</div>
    	    <div id="footer_sitemap">Site map</div>
    	</div>	
    </div>
    </body>
    </html>
    
    code behind
    Code:
    Imports System.Data.SqlClient
    Partial Class catalogue
        Inherits System.Web.UI.Page
        Dim strCatType As String
        Sub Page_Load()
            strCatType = Request.QueryString("c")
    
            Fill_Catalogue(strCatType)
            Fill_Feature(strCatType)
        End Sub
        Public Sub Fill_Catalogue(ByVal strCatType As String)
            Dim con As New SqlConnection("*****")
            Dim strSQL As String = "SELECT top 5 sap_IS13 as ISBN13, rm_TL, left(rm_DE,150) + '...' as strDesc FROM intranet_EdtPublication WHERE rm_DE IS NOT NULL"
    
            Dim cmd As New SqlCommand(strSQL, con)
    
            con.Open()
    
            Dim sqlReader As SqlDataReader = cmd.ExecuteReader()
    
            rpt_Catalogue.DataSource = sqlReader
            rpt_Catalogue.DataBind()
        End Sub
        Public Function check_Image(ByVal inISBN13 As String) As String
            If Right(inISBN13, 1) = "5" Then
                check_Image = "images/covers/001.jpg"
            Else
                check_Image = "images/covers/000.jpg"
            End If
        End Function
        Public Sub Fill_Feature(ByVal strCatType As String)
            Dim con As New SqlConnection("*****")
            Dim strSQL As String = "SELECT top 1 sap_IS13 as ISBN13, * FROM intranet_EdtPublication WHERE rm_DE IS NOT NULL"
    
            Dim cmd As New SqlCommand(strSQL, con)
    
            con.Open()
    
            Dim sqlReader As SqlDataReader = cmd.ExecuteReader()
    
            rpt_Feature_Title.DataSource = sqlReader
            rpt_Feature_Title.DataBind()
        End Sub
        Public Function check_Display_Data(ByVal str_Label As String, ByVal str_Value As String) As String
            Dim str_Display As String = ""
            If str_Value <> "" Then
                If str_Label = "Publication Date" Then
                    str_Display = "<strong>" & str_Label & ":</strong>&nbsp;" & Day(str_Value) & "/" & Month(str_Value) & "/" & Year(str_Value)
                Else
                    str_Display = "<strong>" & str_Label & ":</strong>&nbsp;" & str_Value
                End If            
            End If
                check_Display_Data = str_Display
        End Function
        Public Sub Cat_OnItemCommand(ByVal s As Object, ByVal e As RepeaterCommandEventArgs)
    
            If e.CommandName = "change_Feature" Then
                Dim con As New SqlConnection("*****")
                Dim strSQL As String = "SELECT * FROM intranet_EdtPublication WHERE sap_IS13 = '" & e.CommandArgument & "'"
    
                Dim cmd As New SqlCommand(strSQL, con)
    
                con.Open()
    
                Dim sqlReader As SqlDataReader = cmd.ExecuteReader()
    
                rpt_Feature_Title.DataSource = sqlReader
                rpt_Feature_Title.DataBind()
            End If
        End Sub
    End Class
    
    this is test data, so some of the functions dont do much atm, like check_image for example.

    Thanks for all the help, really appreciate it.

  10. #10
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    You need to add OnItemCommand="Cat_OnItemCommand" to your repeater.

    Also, try removing AutoEventWireup="true" from the Page declaration.

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. How to call a .js file in my form?
    By dtz in forum JavaScript Programming
    Replies: 9
    Last Post: July 28th, 2009, 07:28 AM
  2. Help with JS function
    By Wolffy in forum JavaScript Programming
    Replies: 1
    Last Post: June 9th, 2009, 10:52 AM
  3. Help using function
    By Rebelle in forum ASP Development
    Replies: 11
    Last Post: October 13th, 2008, 01:13 PM
  4. without function
    By guddu in forum Microsoft SQL Server
    Replies: 1
    Last Post: July 15th, 2008, 05:02 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO