0

I'm working on an ASP.Net project, with C#.

Usually, when I need to put Buttons that will execute some methods, I will use the ASP Controller (Button) inside a runat="server" form.

But I feel that this really limits the capabilities of my website, because when I used to work with JSP, I used jquery to reach a servlet to execute some codes and return a responseText.

I did not check yet how this is done in ASP.Net, but my question concerns controllers and the famous runat="server".

When I add a runat="server" to any HTML Element, I'm supposed to be able to manipulate this HTML element in C# (Server-Side), and this actually works, I can change the ID, set the InnerText or InnerHtml, but the thing that I can't get, is why can't I execute a method by clicking on this element?

The "onclick" attribute is for JavaScript I think, and OnServerClick doesn't seem to work as well. Is it something wrong with my codes? or this doesn't work at all?

user1665700
  • 512
  • 2
  • 13
  • 28

6 Answers6

2

You will have to handle the click in the div using the Jquery and call server-side methods through JQuery

Bhushan Firake
  • 9,338
  • 5
  • 44
  • 79
1

You can execute a method from jquery click in server, using __doPostBack javascript function, see this threat for more details How to use __doPostBack()

Community
  • 1
  • 1
Alberto León
  • 2,879
  • 2
  • 25
  • 24
1

There are several way to execute server side methods by clicking on a div or anything on your page. The first is mentioned __dopostback, second is handling the click in javascript or with jQuery and calling a function in a handler or a page method in a webservice or a page method in your page behind code. Here is the handler version:

 $("#btn1").click(function() {
    $.ajax({
            url: '/Handler1.ashx?param1=someparam',
            success: function(msg, status, xhr) {
                //doSomething, manipulate your html
            },
            error: function() {  
             //doSomething
            }
        });
 });

I think the second version is better, because you can make a partial postback without any updatepanel, asyncronously. The drawback is, the server side code is separated from your page behind code.

Handler:

public class Handler1: IHttpHandler
{
   public void ProcessRequest(HttpContext context)
   {
      context.Response.ContentType = "application/json";
      var param1= context.Request.QueryString["param1"];
      //param1 value will be "someparam"

      // do something cool like filling a datatable serialize it with newtonsoft jsonconvert

      var dt= new DataTable();
      // fill it
      context.Response.Write(JsonConvert.SerializeObject(dt));
   }
}

If everything is cool, you get the response in the ajax call in the success section, and the parameter called "msg" will be your serialized JSON datatable.

speti43
  • 2,886
  • 1
  • 20
  • 23
1

Add this code in your jquery on div onclick and pass DIv id whcih call click

__doPostBack('__Page', DivID);

On page load add this code

if (IsPostBack)
            {
    //you will get id of div which called function
    string eventargs = Request["__EVENTARGUMENT"];

                    if (!string.IsNullOrEmpty(eventargs))
                    {
                       //call your function 
                    }
}
sp_m
  • 2,647
  • 8
  • 38
  • 62
1

Make the div runat="server" and id="divName"

in page_Load event in cs:

if (IsPostBack)
{
if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "divClick")
{
    //code to run in click event of divName
}
}

divName.Attributes.Add("ondivClick", ClientScript.GetPostBackEventReference(divName, "divClick"));

Hope it helps :)

Avishek
  • 1,896
  • 14
  • 33
-1

if you are referring to divs with runat="server" attributes, they don't have onserverclick events, that's why it doesn't work

mdcuesta
  • 1,739
  • 1
  • 15
  • 17