简体   繁体   中英

Reach code behind from regular div ASP.net

I am trying to reach my code behind from a regular div onMouseOver event. I actually think it's impossible. I considered many possibility, the first one was :

<div style="float: left; position:absolute; left:-60px; top:120px;" onmouseover="<% div_MouseOver(); %>">

It does reach the code behind, but just while page load instead of Mouse Over...

I also considered javascript, but then again, i don't think any javascript function can call a C# function.

There is not a single < asp:* /> tool that contain a mouse Over event, seriuously i don't know why, wouldn't it be convinient ?

Anyway, if any one know how to create a mouse over event that reaches code behind, that's all i want...

You need to get a solid grasp of how client and server are separated in web applications. No, you can't directly call into the code behind in ASP.NET from JavaScript. You can cause a postback, but the page will be reloaded. That leads to a bad user experience.

It's better to stick with JavaScript on the client. Depending on what you need to do, you may not need server side involvement at all. And if you do, it's usually cleanest to implement AJAX, web sockets, or similar technologies into your applications.

Server controls can have onMouseOver , but it will only run client side code. It'd be ridiculous to have it cause a postback due to mousing over something.

You need to use JavaScript and have it call __doPostBack, which simulates the behavior of a server-side control.

Note that if you don't want the whole page to refresh as soon as the mouse goes over that div, you'll need to put it inside a server-side UpdatePanel control.

Antoine's EDIT : I found out, thanks to Paul :

<div style="float: left; position:absolute; left:-60px; top:120px;" onmouseover="div_MouseOver();">

<script type="text/javascript">
    function div_MouseOver() {
    __doPostBack();
    }

</script>

Paul again: you may want to pay further attention to the arguments to __doPostBack. I suggest googling for some articles on the topic, as it's complex enough so that one-paragraph answers aren't ideal.

If you know the data before hand you can render the code coming from the code behind in a javascript variable and then just show / or act on that data as you need it.

If the data is dynamic Ajax is your only option.

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