Solved

html link calls c# function

Posted on 2009-05-18
20
3,170 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
  • 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 400 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 100 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 400 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HttpPostedFile 1 26
tableview is not updating 1 9
how can I add blockquote after on my wordpress site 3 10
RegEx with optional part 4 24
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

777 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