• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3511
  • Last Modified:

html link calls c# function

I am trying to call a C# function through an html link.

Heres the code:
HTML FILE
 
<a id="link1" runat="server" onserverclick="showInfo_Click" href="#">Click here to see more</a>
<a id="link2" runat="server" onserverclick="showOtherInfo_Click" href="#">Click here to see more</a>
 
aspx.cs FILE
 
public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }
 
        protected void showInfo_Click(object sender, EventArgs e)
        {
            contactMainLabel.Text = "<div>Here is the Info</div>";
        }
 
        protected void showOtherInfo_Click(object sender, EventArgs e)
        {
            contactMainLabel.Text = "<div>Here is some other Info</div>";
        }
    }

Open in new window

0
no158
Asked:
no158
  • 10
  • 5
  • 5
3 Solutions
 
GuitarRichCommented:
Not sure what the question is - but if its not working try using a HyperLink control rather than a straight anchor tag. Like this:
<asp:HyperLink id="link1" runat="server" onclick="showInfo_Click" href="#">Click here to see more</asp:HyperLink>
<asp:HyperLink id="link2" runat="server" onclick="showOtherInfo_Click" href="#">Click here to see more</asp:HyperLink>

Open in new window

0
 
no158Author Commented:
I thought the question was pretty straight forward but let me explain a little more detail.

I have a page where I am trying to display information depending on which link the user clicks. All I'm trying to do is find a way to access the server side through an html link so that I can repopulate the label (which holds all the information) with the new information.

I tried using the asp:HyperLink but the information still wont change when the html link is clicked.
0
 
KelevraCommented:
Hi,

there's an especially made type of link in asp, called asp:LinkButton, it' a link, that you can use like a button, therefore it 'll be easy to acces server side.

N.B  you 'll see in a regular asp:HyperLink, no onClick event!

Hope this'll help.
        <asp:LinkButton ID="LinkButton1" OnClick="mafonction_click" runat="server">LinkButton</asp:LinkButton>

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
no158Author Commented:
The LinkButton does work like a button but it still is not calling the C# functions.
I can call JavaScript just fine but I'm trying to call C# functions directly.
0
 
GuitarRichCommented:
if you put a break point on the C# functions does it stop there? i.e is the code getting called and the label is just not changing or does the code not get called at all?
0
 
no158Author Commented:
The code is not being called.
Something is wrong with the call to the C# side.
0
 
GuitarRichCommented:
if you drag a linkbutton or hyperlink control onto the form in the visual studio designer and double click on it to get to the click event - does that code fire?
0
 
no158Author Commented:
I opened desiner view, double clicked the control and was sent to the code in C# (as it should).
Yet it does not fire when the code is rendered in a browser.

I've tried using onclick as well as onserverclick to get the code to fire but no luck.
0
 
no158Author Commented:
I think the problem I am having is that the href tag fires off before the backend code has a chance to execute. Is there a simple work around for this?
0
 
GuitarRichCommented:
I don't think you can put a href tag in a linkbutton - so that should be working fine? can you show me the page code and code behind with the link button? Are you calling any clientside javascript too?
0
 
KelevraCommented:
That's strange,

I did paste your snippet code, in an empty solution, in visual studio 2008,

It works perfectly.

when you click the link, it goes server side on my stop point!
0
 
KelevraCommented:
yeah the whole thing ( the page code and code behind) would be pretty much appreciated,
 since, there has to be an error somewhere in your code.
0
 
no158Author Commented:
Back end:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
 
namespace site.contact
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "Page Title";
 
            contactMainLabel.Text = "Info 1";
        }
 
        protected void showOffice_Click(object sender, EventArgs e)
        {
            contactMainLabel.Text = "Info 1";
        }
 
        protected void showDistributors_Click(object sender, EventArgs e)
        {
            contactMainLabel.Text = "Info 2";
        }
    }
}

Open in new window

0
 
no158Author Commented:
Front end:
<%@ Page Language="C#" MasterPageFile="page.Master" AutoEventWireup="true" CodeFile="default.aspx.cs" Inherits="site.contact._default" %>
 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
                                  
                            <form runat="server">
                            <div style="float:right;">
                            <asp:LinkButton id="offLoc" runat="server" onserverclick="showOffice_Click" href="#">Office Locations</asp:LinkButton> | 
                            <asp:LinkButton id="distrib" runat="server" onserverclick="showDistributors_Click" href="#">Distributors</asp:LinkButton>
                            </div>
                            </form>
                            
                            <asp:label id="contactMainLabel" runat="server" ></asp:label>
                                                    
</asp:Content>

Open in new window

0
 
KelevraCommented:
You just have to remove the href="#" attributes,

then you can always do a response.redirect in the code behind if you need the page to be redirected!
                            <asp:LinkButton id="offLoc" runat="server" onClick="showOffice_Click" >Office Locations</asp:LinkButton> | 
                            <asp:LinkButton id="distrib" runat="server" onClick="showDistributors_Click">Distributors</asp:LinkButton>

Open in new window

0
 
GuitarRichCommented:
hmm - not sure about that form element. The masterpage should have the form element on it. and as far as I'm aware you can only have 1 serverside form element? try removing that part and see if that makes any difference?
0
 
no158Author Commented:
I removed the # and now when I load the page it defaults to itself.

If you want to see what I'm talking about go here:

http://www.mc.com/testing/ab001/atca/contact
0
 
no158Author Commented:
Scratch that link. Would be too much work to move the site to that location.
0
 
KelevraCommented:
<asp:LinkButton id="offLoc" runat="server" onClick="showOffice_Click" >Office Locations</asp:LinkButton>

I just removed the( heref="#") and it worked perfectly.

after, if visual studio, does'nt hassel you with warnings for you <form> tag you should be just fine.
0
 
no158Author Commented:
That was it leaving in the href="" took higher priority on my setup and reloaded the page before the backend could run killing the process. I removed the entire href="" and it works great. Thanks for the help.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 10
  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now