DeveloperBarn Forums

DeveloperBarn

Programming & IT forum

Call Serverside Function from A Tag

This is a discussion on Call Serverside Function from A Tag within the .Net Development forums, part of the Programming & Scripting category; hi guys, I am busy creating my first .Net website and I have another problem. I have a catalogue page, ...

Go Back   DeveloperBarn Forums > Programming & Scripting > .Net Development

  #1  
Old June 29th, 2009, 05:16 AM
Barn Regular
 
Join Date: Apr 2008
Posts: 84
Rep Power: 2
noFriends is on a distinguished road
Default 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
File Type: jpg pearson_cat.jpg (63.8 KB, 1 views)

Last edited by noFriends; June 29th, 2009 at 05:19 AM. Reason: add image
Reply With Quote
  #2  
Old June 29th, 2009, 05:27 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,312
Blog Entries: 5
Rep Power: 8
richyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to all
Default

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.
__________________
Join the folding team
Reply With Quote
  #3  
Old June 29th, 2009, 05:40 AM
Barn Regular
 
Join Date: Apr 2008
Posts: 84
Rep Power: 2
noFriends is on a distinguished road
Default

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?
Reply With Quote
  #4  
Old June 29th, 2009, 05:51 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,312
Blog Entries: 5
Rep Power: 8
richyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to all
Default

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...
Reply With Quote
  #5  
Old June 29th, 2009, 06:00 AM
Barn Regular
 
Join Date: Apr 2008
Posts: 84
Rep Power: 2
noFriends is on a distinguished road
Default

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
Reply With Quote
  #6  
Old June 29th, 2009, 06:06 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,312
Blog Entries: 5
Rep Power: 8
richyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to all
Default

Try adding an ID to your linkbutton.

You shouldn't see .net code in your HTML source code.
Reply With Quote
  #7  
Old June 29th, 2009, 06:06 AM
Barn Regular
 
Join Date: Apr 2008
Posts: 84
Rep Power: 2
noFriends is on a distinguished road
Default

have a look here:

--Pearson Education--

its the temp link

ID did not change anything

Last edited by noFriends; June 29th, 2009 at 06:10 AM.
Reply With Quote
  #8  
Old June 29th, 2009, 06:16 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,312
Blog Entries: 5
Rep Power: 8
richyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to all
Default

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)
Reply With Quote
  #9  
Old June 29th, 2009, 06:26 AM
Barn Regular
 
Join Date: Apr 2008
Posts: 84
Rep Power: 2
noFriends is on a distinguished road
Default

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.
Reply With Quote
  #10  
Old June 29th, 2009, 06:31 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,312
Blog Entries: 5
Rep Power: 8
richyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to all
Default

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

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

  DeveloperBarn Forums > Programming & Scripting > .Net Development

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
How to call a .js file in my form? dtz JavaScript Programming 9 July 28th, 2009 06:28 AM
Help with JS function Wolffy JavaScript Programming 1 June 9th, 2009 09:52 AM
[Functions] Help using function Rebelle ASP Development 11 October 13th, 2008 12:13 PM
without function guddu Microsoft SQL Server 1 July 15th, 2008 04:02 PM


All times are GMT -4. The time now is 02:04 AM.


Copyright ©2008-2009, DeveloperBarn

Content Relevant URLs by vBSEO 3.3.2