Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

html link calls c# function

Posted on 2009-05-18
20
Medium Priority
?
3,419 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Find out what you should include to make the best professional email signature for your organization.
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

610 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