set div data attribute using a code behind value i

2019-08-15 05:50发布

问题:

On an .aspx page I need to set the data-attribute on a addthis div. The value is an URL that is calculated in the code behind.

<div class="addthis_sharing_toolbox" data-url="CodeBehindValue"></div>

Is there a way to do this?

回答1:

You have a couple options.

Using a property

<div class="addthis_sharing_toolbox" data-url="<%= PropertyFromCodeBehind %>"></div>

Using a method

<div class="addthis_sharing_toolbox" data-url="<%= MethodFromCodeBehind() %>"></div>


回答2:

If you choose your div to run at the server (runat="server") such as:

<div id="divMyID" class="addthis_sharing_toolbox" data-url="CodeBehindValue" runat="server"></div>

divMyID.Attributes("data-url") = "http://www.test.com"