Solved

html link calls c# function

Posted on 2009-05-18
20
3,098 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now