How do I set a jquery tooltip from a database on server side?

安稳与你 提交于 2019-12-11 18:19:33

问题


I have a label on my asp.net page, it looks like this:

more info

When I hover over it, I'd like one of those jquery tooltips to display. The data will come from a database from my code behind. I've look at qtip, jquery tools, and the telerik tooltip. I'm just not sure how to set the content of the tooltip from server side.


回答1:


The simplest implementation using tooltip plugin (http://docs.jquery.com/Plugins/Tooltip) will be just:

$("a").tooltip();

The above use title as the default tooltip content.

Suppose you have X items to be tooltipped with X custom content in a sequence, here's one way

In your code-behind, string up the tooltips like this:

protected string tooltips;
//perform your data retrieval and create a string like this:
tooltips = "'tip1','tip2','tip3'";

In your javascript, to assign the tooltip string array to a javascript array:

var myToolTips = new Array(<%=mytooltips%>);

Then iterate through the items using JQuery's each():

$(".someclass a").each(function(i){
    $(this).tooltip({
       showBody: myToolTips[i];
    });
})



回答2:


I usually set a literal control to print out the value from the server I want the jQuery code to have.

So something like this:

$("div").html("<asp:literal id='litServerData" runat="server"></asp:literal>");

Then in your codebehind you set the literal to the data you want your jQuery code to have.

Of course this only works if your jQuery is on the aspx page. If not you can have a script block at the top of the page that sets a variable to the literal control value and then call your external script.



来源:https://stackoverflow.com/questions/1653009/how-do-i-set-a-jquery-tooltip-from-a-database-on-server-side

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!