Kaan Öztürk
Kaan Öztürk

Reputation: 33

Class function usage on html page

I have a class as x.cs and there is a function

 public string telefonReplace(string tel)
    {
        tel = tel.Replace("(", "");
        tel = tel.Replace(")", "");
        tel = tel.Replace(" ", "");

        return tel;
    }

when i called it on page.aspx.cs

x func = new x();

func.telefonReplace("string value");

I can use this but i want to use it on html page like;

<div><%# func.telefonReplace(Eval["someting"])%> <div>

But i can't call this function. How can i make it ?

Upvotes: 0

Views: 38

Answers (1)

Armando Bracho
Armando Bracho

Reputation: 729

The reason this isn't working is because HTML works on the clients side while your functions written in C# run in the server, so you need to link your html function call to the code behind, and this can be done using an event in html like the click of a button or something similar, here is an example:

<div id="Replace" OnMouseOver="<%# func.telefonReplace(Eval["someting"]) %>" runat="server">

<a href="#" runat="server" onServerClick="MyFuncion" />

This should make the call to your function. Another approach if you do not want to link the call of your function to a user event, is to use javaScript to run your functionality when the page loads, there is lots of documentation on the web about this.

Hope this helps

Upvotes: 1

Related Questions