?
Solved

html link calls c# function

Posted on 2009-05-18
20
Medium Priority
?
3,335 Views
Last Modified: 2012-05-07
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
Comment
Question by:no158
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 5
  • 5
20 Comments
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24413456
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
 

Author Comment

by:no158
ID: 24413625
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
 
LVL 3

Accepted Solution

by:
Kelevra earned 1600 total points
ID: 24414068
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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

Author Comment

by:no158
ID: 24414168
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
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24414236
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
 

Author Comment

by:no158
ID: 24414277
The code is not being called.
Something is wrong with the call to the C# side.
0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24414304
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
 

Author Comment

by:no158
ID: 24414408
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
 

Author Comment

by:no158
ID: 24414428
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
 
LVL 12

Assisted Solution

by:GuitarRich
GuitarRich earned 400 total points
ID: 24414513
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
 
LVL 3

Expert Comment

by:Kelevra
ID: 24414558
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
 
LVL 3

Expert Comment

by:Kelevra
ID: 24414589
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
 

Author Comment

by:no158
ID: 24414612
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
 

Author Comment

by:no158
ID: 24414639
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
 
LVL 3

Expert Comment

by:Kelevra
ID: 24414743
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
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24414948
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
 

Author Comment

by:no158
ID: 24414990
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
 

Author Comment

by:no158
ID: 24415031
Scratch that link. Would be too much work to move the site to that location.
0
 
LVL 3

Assisted Solution

by:Kelevra
Kelevra earned 1600 total points
ID: 24415068
<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
 

Author Comment

by:no158
ID: 24415120
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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question