How do I set a jquery tooltip from a database on server side?
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.
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];
});
})
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.
精彩评论