How do I call a function from a hyperlink ?

  • Thread starter Thread starter Fresh Air Rider
  • Start date Start date
F

Fresh Air Rider

Could anyone please tell me how I can call a function from a hyperlink
in C# ?

Examples of scenarios where this would be useful include :-

Loop through a set of records in a repeater and have a hyperlink to
edit each record by passing it's record id to another page.

Loop through a set of files on a server and have a hyperlink to
download a file via a stream on another page by passing its filename
(this is so that the full path of the file is not revealed to the
user).

ie

I have a function within "Codebehind" (which works fine).

public void DownloadFile( string fname,bool forceDownload)
{
.......
.......
.......
}

I have tried calling the function from the ASP page in various ways
such as :-

<asp:HyperLink NavigateUrl="<%#DownloadFile("test.doc",true)%>">Download
file</asp:HyperLink>

This method results in the error "The best overloaded method match for
'System.Convert.ToString(object)' has some invalid arguments"

I have tried various permutations of single quotes, double quotes etc.

I also considered using <asp:LinkButton> but this only seems to allow
one parameter to be passed to the function.

Surely there must be an very simple solution to this in .Net as it was
extremely easy to do in Classic ASP.

If anyone could propose a solution to this problem I would be very
grateful indeed.

Regards
John
 
Hello Frank

Does a LinkButton allow me call a function with more than one parameter ?

Could you please give me an example of the syntax ?

Many thanks
John
 
Fresh said:
Does a LinkButton allow me call a function with more than one parameter ?

HyperLinks don't let you do anything server-side since they're just ordinary
HTML tags. LinkButtons, OTOH, offer a Click event. You can handle this
event just like any other and perform any preprocessing you require prior
to calling Response.Redirect or Server.Transfer.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
Hi Frank

So I pass the values to each of the parameters of my function through
the click event of the <asp:LinkButton>

I'll have a hunt around the internet for an example of the syntax.

Many thanks for that advice.

Regards
John
 
Fresh said:
Hi Frank

So I pass the values to each of the parameters of my function through
the click event of the <asp:LinkButton>

private void MyLinkButton_Click(object sender, EventArgs e) {

// do whatever you need such as the following.
someObject.SomeMethod(someParam1, someParam2);
Response.Redirect(newUrl);

}

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
Back
Top