简体   繁体   中英

How to call a C# method from asp.net side?

This seems to be a common question but I could not find a relevant answer

I have a aspx page and in it, I've made a list of "a href" tags that is generated from my C# code behind. Now i want to send data based on which of the "a href" tag i have selected and pass it into my C# code which will then populate my popup with required information:

The process

"a href click" -> call c# method -> populate popup with data

I am stuck at how to pass data from the "a href click" to the c#.

I tried using .OnServerClick but my popup didn't even pop up.

For starters, i would like to try: When i Click a "a href" tag it would call my changeTitle() method from c# which will change the title of the pop up. If i can get this working, i should be able to manage the rest

弹出

c# method:

public void changeTitle()
{
    this.modalTitle.InnerHtml = "Goodbye world";
}

Do tell me if you need more information please, I really hope to get this working

You should really look into learning more about how jQuery and ASP.NET can work together. I highly recommend starting at Dave Ward's site .

Here is a blog entry about using jQuery to call ASP.NET server methods (page methods are a great way to get quick hooks into server-side logic that can pass data back to the client-side): Using jQuery To Directly Call ASP.NET AJAX Page Methods

I have used this technique in many projects over the years and I think once you start learning the power of jQuery you will want to use this approach over strictly server-side controls.

If ASP.NET WebForms is being used (remember to specify which "flavor" of ASP.NET), a LinkButton control may be a suitable approach 1 .

A LinkButton works like aa normal Button that looks like a hyperlink and causes a PostBack when clicked. (This is different from "normal" hyperlinks that change the browser location.)

The OnClick attrtibute specifies the server callback handler and the Command and CommandArguments can be used to associate specific data with the control that are available on the server during the LinkButton's click callback.


1 While my current preferred form of development is a thick client with a thin backend (eg not WebForms), switching to use "Page Methods" or "AJAX" requires rewriting the front-end/HTML to act on the response as appropriate. A LinkButton on the other hand, simply "works" with the normal ASP.NET WebForms lifecycle/infrastructure without any additional work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM